team-a / collection
筛选后的集合。
2.1.1
2020-02-05 20:26 UTC
Requires
- php: ^7.4
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8.5
- squizlabs/php_codesniffer: ^3.5
README
TeamA\Collection 是一个用于筛选和排序集合的接口和特性库。
该库解决了性能问题、代码重复,并允许你在不同的类中为使用集合设计上下文。
可以按照许多标准实现排序和筛选。
支持按多个标准进行排序和筛选。筛选器和排序器作为具有一组修饰符的对象实现。可以使用“AND”、“OR”逻辑连接筛选器。
提供检查以匹配集合中包含的类型与实现筛选器的类型。
要求
- php >= 7.4
通过 Composer 安装
composer require team-a/collection:^2.1
方法
Collection
- sort
- sortReverse
- filter
- filterNotMatched
- first
- firstNotMatched
- last
- lastNotMatched
- has
- isAllMatched
- isEmpty
- hasNot
- count
- countNotMatched
- asArray
- asArrayNotMatched
筛选链
- and
- or
- not
示例
有关工作示例,请参阅 tests/Model。
<?php $collection = new CarCollection($cars); $collection = $collection->sort( CarCollectionSorter::new() ->byModel() ->byColor() ); $filter = CollectionFilter::new() ->withColor('blue') ->or( CarCollectionFilter::new()->withModel('408') )->or( CarCollectionFilter::new()->withVendor('VAZ') ) ; $hasAtLeastOneCar = $collection->has($filter); $hasNoCars = $collection->isEmpty($filter); $hasDiscardedCars = $collection->hasNot($filter); $firstCar = $collection->first($filter); $lastCar = $collection->last($filter); $array = $collection->filter($filter)->asArray(); $count = $collection->count($filter);