arete/collection-pipeline
Composer 安装命令:
composer require arete/collection-pipeline
包简介
PHP (partial) implementation of a pipeline collection. Work with a collection of objects without making a bunch of callables, loops, & ifs.
关键字:
README 文档
README
Work with a collection of objects without making a bunch of callables, loops, & ifs.
After reading Martin Fowler on the Collection Pipeline I wanted to use something similar in PHP, thus, this was born. League\Pipeline was used as was Illuminate\Support\Collection (all functions from this Collection are available in the chain.)
Example
This is our example group we will use
use Arete\CollectionPipeline\CollectionPipeline as CP; class MockEntity { public $id; public $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } public function getId() { return $this->id; } public function getName() { return $this->name; } } // ids are just random for testing $array = array( new MockEntity(null, "eric"), #0 new MockEntity(10, "tim"), #1 new MockEntity(111, "beau"), #2 new MockEntity(11, "ross"), #3 new MockEntity(12, "sarah"), #4 new MockEntity(13, "taylor"), #5 new MockEntity(-42, "lea"), #6 new MockEntity("eh", "phil"), #7 new MockEntity(6, "larry"), #8 new MockEntity(10, "frank"), #9 new MockEntity(["eh"], "joe"),#10 new MockEntity(99, "kayla"), #12 new MockEntity(0, "martin"), #11 new MockEntity(1, "brad"), #13 new MockEntity(2, "luke"), #14 new MockEntity(3, "paul"), #15 new MockEntity(4, "ash"), #16 new MockEntity(5, "davey"), #17 new MockEntity(18,"anthony"), #18 new MockEntity(19,"tim"), #19 );
String functions
$result = CP::from($array)->wheres('getId', 'is_string')->all(); # gives: [7 => $array[7]]
! String functions
$result = CP::from($array)->wheres('getId', '!is_string')->all(); # gives: everything in $array except #7
Each
Use ::wheresEach to compare the whole value without using any accessors.
instanceof
$result = CP::from($array)->wheres('instanceof', MockEntity::CLASS)->all(); # gives: everything in $array
! instanceof
$result = CP::from($array)->wheres('!instanceof', MockEntity::CLASS)->all(); # gives: empty array, they all are instances of MockEntity
comparison operators
$result = CP::from($array)->wheres('getId', '>', 110)->all(); # gives: [9 => $array[9]]
chaining
$result = CP::from($array)->wheres('getId', '!is_string')->wheres('getId', '>', 10)->wheres('getName', '===', 'tim')->all(); # gives: [19 => $array[19]]
argument order:
The accessor return value (X) as the first argument, and the value you are using in the comparison (Y).
// one does contain joe, but none contain derek $stringItWouldBeIn = 'joe,derek'; $x = 'getName'; $y = $stringItWouldBeIn; // containsSubString is from arete\support $result = CP::from($array)->wheresYX($x, 'containsSubString', $y)->all(); # gives: [10 => $array[10]]
Laravel Illuminate:
Since it extends Illuminate\Support\Collection, you can use their functions, such as:
$result = CP::from($array)->wheres('id', 'is_string', null, 'property')->keys(); # gives: [7]
Types:
- methods
- properties
- callables (using closure)
- key | index
By default it will first check if it's a
method|property|callable|index. If you want to only check for that particular type, in this case,method:
// will only check for the method `getId` $result = CP::from($array)->wheres('getId', '>', 110, 'method')->all(); # gives: [9 => $array[9]]
Reverse order
// compares 110 < $payload->getId() $result = CP::from($array)->wheres('getId', '<', 110, 'method', 'yx')->all(); # gives: [9 => $array[9]]
callables
$stringItWouldBeIn = 'joe,jonathon'; $result = CP::from($array)->wheresYX('getName', 'containsSubString', $stringItWouldBeIn, 'callable')->all(); $result = CP::from($array)->wheres('getId', function($value) { if ($value == 'tim') return true return false; })->all(); # gives: [10 => $array[10]]
Value:
Value is an optional parameter, so if you want to check say, a property only, but have no value to compare it to:
// will only check for the property `id`, // it could be ['property', 'method'] if you wanted to use a method if the property was not there // or it could be ['property', 'method', 'callable'] (which is default) $result = CP::from($array)->wheres('id', 'is_string', null, 'property')->all(); # gives: [9 => $array[9]]
Specification
use Arete\Specification\Specification; use Arete\Specification\SpecificationTrait; class NameEquals implements Specification { use ParameterizedSpecification; use SpecificationTrait; public function isSatisfiedBy($entity) { if ($entity->getName() == $this->value) return true; return false; } } $result = CP::from($array)->satisfying(new NameEquals('tim')); # gives: [10 => $array[10]]
Installation
It can be installed from Packagist using Composer.
In your project root just run:
$ composer require arete/collection-pipeline
Make sure that you’ve set up your project to autoload Composer-installed packages.
Running tests
Run via the command line by going to arete/collection-pipeline directory and running phpunit
@TODO:
- add ability to get an array with objects method values. Meaning, if I want to just get $objects->getName(); as an array of $objectNames and also maybe set what the key is?
- option to pass in an array with the '!' if you want it to be not?
- move ExpressionBuilder to Constructor()
- optimize the filters so they can be combined and done in one loop when requested as array / all()?
- pass in multiple string functions & comparison operators, such as
'is_string | is_int & >'be able to do('methodName', 'strlen >', 5)(could use some Symfony\ExpressionLanguage optionally if alias are required) when this is done, it will really use the pipeline how it ought to - similar to the last todo, but with chaining method calls
- move examples out of readme (except for 1), and into [examples/]
- add in spaceship comparison operator depending on version (thanks @seldaek)
-
andsusing last method? - refactor
ExendedPipelineso it is less of a God object. - array key in
Specification - array key for matching along with the method, property, and callable
- abstract argsOrderYX & XY
- remove null check from
::wheresComparison - add ability to reverse arguments in expressions
- add casting of accessor
- add ::reduce() similar implementation as ::map()
arete/collection-pipeline 适用场景与选型建议
arete/collection-pipeline 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「collection」 「pipeline」 「arete」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arete/collection-pipeline 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arete/collection-pipeline 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arete/collection-pipeline 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Pipeline
Traits to build collections of specific objects
ItalyStrap Helpers
Froq! Collection.
Model and Collection classes
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 17
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-01