palmtree/collection
Composer 安装命令:
composer require palmtree/collection
包简介
Immutable collections which provide commonly used functionality missing from arrays
README 文档
README
Immutable collections which provide commonly used functionality missing from arrays.
Requirements
- PHP >= 8.1
Installation
Use composer to add the package to your dependencies:
composer require palmtree/collection
Usage
Collections can be used just like arrays for the most part. They implement ArrayAccess, Traversable and Countable:
use function Palmtree\Collection\c; $collection = c(['foo' => 'bar', 'baz' => 'qux']); $foo = $collection['foo']; $collection['baz2'] = 'qux'; isset($collection['baz']); // true unset($collection['baz']); count($collection); foreach ($collection as $key => $value) { // do stuff with value and/or key } // find returns the first matching element $foo = $collection->find(fn ($v) => $v === 'bar'); // filter returns a new filtered collection $quxCollection = $collection->filter(fn ($v) => $v === 'qux');
use function Palmtree\Collection\c; $collection = c([1, 9, 5, 3, 7, 10]) ->sort() ->filter(fn ($i) $i < 10) ->values() // returns true as every element remaining in the collection is odd $isAllOdd = $collection->every(fn ($i) => $i % 2 !== 0); // returns false as it was removed in our filter $collection->contains(10); // returns true as at least one of the elements is false c([true, true, false, true, true])->some(fn ($v) => !$v);
Many other methods are provided. Read through the documented source code to see more.
Generics
The library supports template annotations for use by static analysers such as Psalm and PHPStan:
/** @var Collection<int, Foo> **/ $collection = new Collection(); $collection->add(new Foo()); foreach ($collection as $foo) { // Psalm/PHPStan and PhpStorm know that $foo is an instance of Foo here }
License
Released under the MIT license
palmtree/collection 适用场景与选型建议
palmtree/collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.55k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 palmtree/collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 palmtree/collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 3
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-24