{"id":19443787,"url":"https://github.com/aretecode/collection-pipeline","last_synced_at":"2025-04-25T00:32:42.247Z","repository":{"id":62486766,"uuid":"43511293","full_name":"aretecode/collection-pipeline","owner":"aretecode","description":"Filter a collection of objects without making a bunch of loops \u0026 ifs.","archived":false,"fork":false,"pushed_at":"2015-10-27T02:45:17.000Z","size":212,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T13:44:00.593Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aretecode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-01T17:47:27.000Z","updated_at":"2020-09-30T07:37:33.000Z","dependencies_parsed_at":"2022-11-02T10:02:46.836Z","dependency_job_id":null,"html_url":"https://github.com/aretecode/collection-pipeline","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aretecode%2Fcollection-pipeline","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aretecode%2Fcollection-pipeline/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aretecode%2Fcollection-pipeline/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aretecode%2Fcollection-pipeline/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aretecode","download_url":"https://codeload.github.com/aretecode/collection-pipeline/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250733531,"owners_count":21478385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T15:44:09.610Z","updated_at":"2025-04-25T00:32:41.992Z","avatar_url":"https://github.com/aretecode.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Arete\\CollectionPipeline\n[![Build Status](https://secure.travis-ci.org/aretecode/collection-pipeline.svg)](https://travis-ci.org/aretecode/collection-pipeline)\n[![HHVM Status](http://hhvm.h4cc.de/badge/arete/collection-pipeline.svg)](http://hhvm.h4cc.de/package/arete/collection-pipeline)\n[![Author](http://img.shields.io/badge/author-@aretecode-blue.svg)](https://twitter.com/aretecode)\n[![Latest Unstable Version](https://poser.pugx.org/arete/collection-pipeline/v/unstable)](https://packagist.org/packages/arete/collection-pipeline)\n[![License](https://poser.pugx.org/arete/collection-pipeline/license)](http://packagist.org/packages/arete/collection-pipeline)\n[![Codacy Badge](https://api.codacy.com/project/badge/88c8b9f55cf94e2ab16765a7c95be7aa)](https://www.codacy.com/app/aretecode/collection-pipeline)\n\nWork with a collection of objects without making a bunch of callables, loops, \u0026 ifs.\n\nAfter reading [Martin Fowler on the Collection Pipeline](http://martinfowler.com/articles/collection-pipeline/) I wanted to use something similar in PHP, thus, this was born. [League\\Pipeline](https://github.com/thephpleague/pipeline) was used as was [Illuminate\\Support\\Collection](http://laravel.com/api/master/Illuminate/Support/Collection.html) (all functions from this Collection are available in the chain.)\n\n\n# Example\n\n## This is our example group we will use\n```php\nuse Arete\\CollectionPipeline\\CollectionPipeline as CP;\n\nclass MockEntity {\n    public $id;\n    public $name;\n    public function __construct($id, $name) {\n        $this-\u003eid = $id;\n        $this-\u003ename = $name;\n    }\n    public function getId() {\n        return $this-\u003eid;\n    }\n    public function getName() {\n        return $this-\u003ename;\n    }\n}\n\n// ids are just random for testing\n$array = array(\n    new MockEntity(null, \"eric\"), #0\n    new MockEntity(10, \"tim\"),    #1\n    new MockEntity(111, \"beau\"),  #2\n    new MockEntity(11, \"ross\"),   #3\n    new MockEntity(12, \"sarah\"),  #4\n    new MockEntity(13, \"taylor\"), #5\n    new MockEntity(-42, \"lea\"),   #6\n    new MockEntity(\"eh\", \"phil\"), #7\n    new MockEntity(6, \"larry\"),   #8\n    new MockEntity(10, \"frank\"),  #9\n    new MockEntity([\"eh\"], \"joe\"),#10\n\n    new MockEntity(99, \"kayla\"),  #12\n    new MockEntity(0, \"martin\"),  #11\n    new MockEntity(1, \"brad\"),    #13\n    new MockEntity(2, \"luke\"),    #14\n    new MockEntity(3, \"paul\"),    #15\n    new MockEntity(4, \"ash\"),     #16\n    new MockEntity(5, \"davey\"),   #17\n    new MockEntity(18,\"anthony\"), #18\n    new MockEntity(19,\"tim\"),     #19\n);\n```\n\n\n## String functions\n```php\n$result = CP::from($array)-\u003ewheres('getId', 'is_string')-\u003eall();\n\n# gives: [7 =\u003e $array[7]]\n```\n\n## `!` String functions\n```php\n$result = CP::from($array)-\u003ewheres('getId', '!is_string')-\u003eall();\n\n# gives: everything in $array except #7\n```\n\n## Each\nUse `::wheresEach` to compare the whole value without using any accessors.\n\n### instanceof\n```php\n$result = CP::from($array)-\u003ewheres('instanceof', MockEntity::CLASS)-\u003eall();\n\n# gives: everything in $array\n```\n\n### `!` instanceof\n```php\n$result = CP::from($array)-\u003ewheres('!instanceof', MockEntity::CLASS)-\u003eall();\n\n# gives: empty array, they all are instances of MockEntity\n```\n\n## [comparison operators](http://php.net/manual/en/language.operators.comparison.php)\n[comparison operator tests](https://github.com/aretecode/collection-pipeline/blob/master/tests/MathComparisonTest.php)\n\n```php\n$result = CP::from($array)-\u003ewheres('getId', '\u003e', 110)-\u003eall();\n\n# gives: [9 =\u003e $array[9]]\n```\n\n## chaining\n```php\n$result = CP::from($array)-\u003ewheres('getId', '!is_string')-\u003ewheres('getId', '\u003e', 10)-\u003ewheres('getName', '===', 'tim')-\u003eall();\n\n# gives: [19 =\u003e $array[19]]\n```\n\n\n## argument order:\nThe accessor return value (X) as the first argument, and the value you are using in the comparison (Y).\n\n```php\n// one does contain joe, but none contain derek\n$stringItWouldBeIn = 'joe,derek';\n$x = 'getName';\n$y = $stringItWouldBeIn;\n\n// containsSubString is from arete\\support\n$result = CP::from($array)-\u003ewheresYX($x, 'containsSubString', $y)-\u003eall();\n\n# gives: [10 =\u003e $array[10]]\n```\n\n\n\n\n## Laravel Illuminate:\nSince it extends [Illuminate\\Support\\Collection](http://laravel.com/api/master/Illuminate/Support/Collection.html), you can use their functions, such as:\n\n```php\n$result = CP::from($array)-\u003ewheres('id', 'is_string', null, 'property')-\u003ekeys();\n\n# gives: [7]\n```\n\n\n## Types:\n* [methods](https://github.com/aretecode/collection-pipeline/blob/master/tests/MethodTest.php)\n* [properties](https://github.com/aretecode/collection-pipeline/blob/master/tests/PropertyTest.php)\n* [callables (using closure)](https://github.com/aretecode/collection-pipeline/blob/master/tests/CallableTest.php)\n* [key | index](https://github.com/aretecode/collection-pipeline/blob/master/tests/ArrayTest.php)\nBy default it will first check if it's a `method`|`property`|`callable`|`index`.\nIf you want to only check for that particular type, in this case, `method`:\n\n```php\n// will only check for the method `getId`\n$result = CP::from($array)-\u003ewheres('getId', '\u003e', 110, 'method')-\u003eall();\n\n# gives: [9 =\u003e $array[9]]\n```\n\n#### Reverse order\n```php\n// compares 110 \u003c $payload-\u003egetId()\n$result = CP::from($array)-\u003ewheres('getId', '\u003c', 110, 'method', 'yx')-\u003eall();\n\n# gives: [9 =\u003e $array[9]]\n```\n\n## [callables](https://github.com/aretecode/collection-pipeline/blob/master/tests/CallableTest.php)\n```php\n$stringItWouldBeIn = 'joe,jonathon';\n$result = CP::from($array)-\u003ewheresYX('getName', 'containsSubString', $stringItWouldBeIn, 'callable')-\u003eall();\n$result = CP::from($array)-\u003ewheres('getId', function($value) {\n    if ($value == 'tim')\n        return true\n    return false;\n})-\u003eall();\n\n# gives: [10 =\u003e $array[10]]\n```\n\n## Value:\nValue is an optional parameter, so if you want to check say, a `property` only, but have no value to compare it to:\n```php\n// will only check for the property `id`,\n// it could be ['property', 'method'] if you wanted to use a method if the property was not there\n// or it could be ['property', 'method', 'callable'] (which is default)\n$result = CP::from($array)-\u003ewheres('id', 'is_string', null, 'property')-\u003eall();\n\n# gives: [9 =\u003e $array[9]]\n```\n\n\n## Specification\n[arete/specification](https://github.com/aretecode/specification)\n\n```php\n\nuse Arete\\Specification\\Specification;\nuse Arete\\Specification\\SpecificationTrait;\n\nclass NameEquals implements Specification {\n    use ParameterizedSpecification;\n    use SpecificationTrait;\n\n    public function isSatisfiedBy($entity) {\n        if ($entity-\u003egetName() == $this-\u003evalue)\n            return true;\n        return false;\n    }\n}\n\n$result = CP::from($array)-\u003esatisfying(new NameEquals('tim'));\n\n# gives: [10 =\u003e $array[10]]\n```\n\n\n\n## Installation\nIt can be installed from [Packagist](https://packagist.org/packages/arete/collection-pipeline) using [Composer](https://getcomposer.org/).\n\nIn your project root just run:\n\n\n`$ composer require arete/collection-pipeline`\n\n\nMake sure that you’ve set up your project to [autoload Composer-installed packages](https://getcomposer.org/doc/00-intro.md#autoloading).\n\n\n## Running tests\nRun via the command line by going to `arete/collection-pipeline` directory and running `phpunit`\n\n# @TODO:\n* [ ] add ability to get an array with objects method values. Meaning, if I want to just get $objects-\u003egetName(); as an array of $objectNames and also maybe set what the key is?\n* [x] option to pass in an array with the '!' if you want it to be not?\n* [x] move ExpressionBuilder to Constructor()\n* [ ] optimize the filters so they can be combined and done in one loop when requested as array / all()?\n* [ ] pass in multiple string functions \u0026 comparison operators, such as `'is_string | is_int \u0026 \u003e'` be able to do `('methodName', 'strlen \u003e', 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\n* [ ] similar to the last todo, but with chaining method calls\n* [ ] move examples out of readme (except for 1), and into [examples/]\n* [x] add in spaceship comparison operator depending on version (thanks @seldaek)\n* [ ] `ands` using last method?\n* [x] refactor `ExendedPipeline` so it is less of a God object.\n* [ ] array key in `Specification`\n* [x] array key for matching along with the method, property, and callable\n* [x] abstract argsOrderYX \u0026 XY\n* [x] remove null check from `::wheresComparison`\n* [x] add ability to reverse arguments in expressions\n* [ ] add casting of accessor\n* [ ] add ::reduce() similar implementation as ::map()","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faretecode%2Fcollection-pipeline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faretecode%2Fcollection-pipeline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faretecode%2Fcollection-pipeline/lists"}