pinkcrab/collection
Composer 安装命令:
composer require pinkcrab/collection
包简介
A flexible and extendable Collection
README 文档
README
Version 1.0.0
This library was extracted from the PinkCrab Plugin Framework (Perique)
Why?
Give access to a basic collection with all expected functionlaity, filtering, mapping, folding, sorting and comparing. But is also extendable for creating custom collections, which can be expanded and typed. A fairly simple, but extendable Collection.
Install
Install via Composer:
composer require pinkcrab/collection
Then include the Composer autoloader in your project:
require_once __DIR__ . '/vendor/autoload.php';
Basic Useage
See
./docsfor more details and examples.
$collection = new Collection(['1',2,'3']); $collection->push(4); $collection->apply(fn($e) => (string) $e); var_dump($collection); // ['1','2','3','4'];
$collection = new Collection([1,2,3,4,5,6,7,8,9,10]); $collection->filter(fn($e) => $e % 2 == 0); var_dump($collection); // [2,4,6,8,10];
Extendable Traits
The Collection package comes with a few Traits which can be used when creating custom collections. These can either be created on the fly using anonymous classes or through defining them as full classes.
$indexed_collection = new class() extends \PinkCrab\Collection\Collection { use \PinkCrab\Collection\Traits\Indexed; }; $indexed_collection->set( 'key1', 'value1' ); $indexed_collection->has( 'key1' ); //true var_dump( $indexed_collection ); // As a full class. class Indexed_Collection extends \PinkCrab\Collection\Collection { use \PinkCrab\Collection\Traits\Indexed; }; $indexed_collection = new Indexed_Collection(); $indexed_collection->set( 'key1', 'value1' ); $indexed_collection->has( 'key1' ); //true var_dump( $indexed_collection );
Typed & Mapped Collections
<?php class Post_Collection extends Collection { // Filter out anything not matching. protected function map_construct( array $data ): array { return array_filter(fn($e): bool => is_a($data, \WP_Post::class)); } } $posts = Post_Collection::from([$post1, null, $post2, false, WP_Error]); var_dump($posts->to_array()); // [$post1, $post2]; $collection->each(function($e){ print $e->post_title . PHP_EOL; }); // Post Title 1 // Post Title 2
License
MIT License http://www.opensource.org/licenses/mit-license.html
Change Log
- 1.0.0 - Raised supported PHP range to 7.1–8.5 and modernised the tooling chain (PHPStan 2.x + phpstan stubs so traits are actually analysed, PHPUnit up to 9.6, WPCS-based phpcs). Interface implementations (
ArrayAccess,Iterator,JsonSerializable) marked with#[\ReturnTypeWillChange]for PHP 8.1+ without dropping PHP 7.x support.Sequence::sum()now filters non-numeric values to stay compatible with PHP 8.3's stricterarray_sum(). Corrected phpdoc types onHas_ArrayAccess::offsetSet(nullable offset) andIs_Iterable::key(nullable return). BC break: public method parameters renamed$function/$callable→$callbackonapply(),each(),filter(),map(),reduce(),sort(),sorted(),group_by()— positional callers unaffected, named-argument callers need to update. - 0.2.0 - Added in option callbacks for diff and intersect, complete with helper functions for checking based on object instance or values/type. Also includes the group_by() method.
- 0.1.0 - Added Has_ArrayAccess and Is_Iterable traits to allow the implementation of the interfaces. Added docs from existing GitBook repo.
- 0.0.0 - Extracted from the PinkCrab Plugin Framework as a standalone package.
pinkcrab/collection 适用场景与选型建议
pinkcrab/collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.42k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pinkcrab/collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pinkcrab/collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-07