amo/collection
Composer 安装命令:
composer require amo/collection
包简介
Collection Abstraction library extended
README 文档
README
Collection management, heavily inspired by Doctrine ArrayCollection
Install
composer require amo/collection
Docker (latest PHP)
docker compose build
docker compose run --rm php composer install
docker compose run --rm php vendor/bin/phpunit -c phpunit.xml
Methods: Usage + Outcome
use Amo\Collection\Collection;
make / create
Collection::make([1, 2, 3])-> collection with values[1, 2, 3]Collection::create(['a' => 10])-> collection with key/value['a' => 10]
each (return value ignored, iterates all)
Collection::make([1, 2, 3])->each(fn(int $v) => print($v));-> prints123
map
Collection::make([1, 2])->map(fn(int $v) => $v * 10)->getValues()->[10, 20]
flatMap
Collection::make([[1, 2], [3]])->flatMap(fn(array $x) => $x)->getValues()->[1, 2, 3]
find
Collection::make([1, 3, 5])->find(fn(int $v) => $v > 2)->3Collection::make([1, 3, 5])->find(fn(int $v) => $v > 9)->null
groupBy
Collection::make([['n' => 'a', 't' => 'x'], ['n' => 'b', 't' => 'x'], ['n' => 'c', 't' => 'y']])->groupBy(fn(array $i) => $i['t'])- Outcome: keys
x,y, each value is aCollectionof matching rows
keyBy
Collection::make([['id' => 10], ['id' => 20]])->keyBy(fn(array $i) => $i['id'])->getKeys()->[10, 20]
take / drop
Collection::make([1, 2, 3, 4])->take(2)->getValues()->[1, 2]Collection::make([1, 2, 3, 4])->drop(2)->getValues()->[3, 4]
merge
Collection::make([3, 4])->merge(Collection::make([1, 2]))->getValues()->[1, 2, 3, 4]
append (mutates current collection)
$c = Collection::make([1]); $c->append(Collection::make([2, 3])); $c->getValues()->[1, 2, 3]
copy
$a = Collection::make([1]); $b = $a->copy();-> same values, different instance
usort / sort
Collection::make([2, 1, 3])->sort(fn(int $a, int $b) => $a <=> $b)->getValues()->[1, 2, 3]
flatten
Collection::make([[1, 2], 3, [4]])->flatten()->getValues()->[1, 2, 3, 4]
flip
Collection::make(['a' => 'x', 'b' => 'y'])->flip()->toArray()->['x' => 'a', 'y' => 'b']
unique
Collection::make([1, 1, 2, 2])->unique()->getValues()->[1, 2]
intersect
Collection::make([1, 2, 3])->intersect(Collection::make([2, 4, 3]))->getValues()->[2, 3]
diff
Collection::make([1, 2, 3])->diff(Collection::make([2, 4]))->getValues()->[1, 3]
reduce
Collection::make([1, 2, 3])->reduce(fn(array $acc, int $v) => [...$acc, $v * 2], [])->getValues()->[2, 4, 6]
sum
Collection::make([['amount' => 1.5], ['amount' => 2.5]])->sum(fn(array $i) => $i['amount'], 1)->5.0
slice
Collection::make([1, 2, 3, 4])->slice(1, 2)->getValues()->[2, 3]
amo/collection 适用场景与选型建议
amo/collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 156 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 amo/collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amo/collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 156
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-01