{"id":18879749,"url":"https://github.com/loilo/collection","last_synced_at":"2025-09-01T12:11:36.515Z","repository":{"id":52403679,"uuid":"154544811","full_name":"loilo/Collection","owner":"loilo","description":"An extended version of Laravel Collections","archived":false,"fork":false,"pushed_at":"2021-04-29T23:24:04.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-30T11:38:14.082Z","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/loilo.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":"2018-10-24T17:54:52.000Z","updated_at":"2021-04-29T23:24:05.000Z","dependencies_parsed_at":"2022-08-22T09:30:21.212Z","dependency_job_id":null,"html_url":"https://github.com/loilo/Collection","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/loilo/Collection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2FCollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2FCollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2FCollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2FCollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loilo","download_url":"https://codeload.github.com/loilo/Collection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loilo%2FCollection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273122129,"owners_count":25049539,"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","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-08T06:39:08.813Z","updated_at":"2025-09-01T12:11:36.493Z","avatar_url":"https://github.com/loilo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Collection\n[![Travis](https://img.shields.io/travis/Loilo/Collection.svg)](https://travis-ci.org/Loilo/Collection) [![Packagist](https://img.shields.io/packagist/v/loilo/collection.svg)](https://packagist.org/packages/loilo/collection)\n\u003e An extended version of Laravel's collections\n\nThis is my very personal extension of Laravel's amazing [Collection](https://laravel.com/docs/collections) class (as extracted by [tightenco/collect](https://github.com/tightenco/collect)). It adds some methods that I need regularly.\n\nThe declared aim of this project is to feed its methods back into Laravel by making proposals in [`laravel/ideas`](https://github.com/laravel/ideas).\n\n## Install\n```bash\ncomposer require loilo/collection\n```\n\n## Methods\n\u003e **Note:** In all code examples below, `Collection` refers to `Loilo\\Collection\\Collection`.\n\n**Table of Contents:**\n* [`insertBefore()`/`insertAfter()`](#insertbeforeinsertafter)\n* [`mergeInBefore()`/`mergeInAfter()`](#mergeinbeforemergeinafter)\n* [`extract()`](#extract)\n* [`rearrange()`](#rearrange)\n\n### `insertBefore()`/`insertAfter()`\nInserts items before/after a reference item (modifies the collection).\n\n**Proposal:** [#650](https://github.com/laravel/ideas/issues/650) (declined)\n\n**Details:** [Specification](spec/InsertBeforeAfter.md)\n\n**Examples:**\n\nInsert a list of items after a certain value:\n```php\n$c = Collection::make([ 10, 20, 30, 40 ]);\n$c-\u003einsertAfter(20, [ 24, 25, 26 ]);\n$c-\u003eall() === [ 10, 20, 24, 25, 26, 30, 40 ];\n```\n\nInsert data after a certain key:\n```php\n$c = Collection::make([\n    'name' =\u003e 'loilo/collection',\n    'version' =\u003e '1.0.0'\n]);\n\n$c-\u003einsertAfter(function ($value, $key) {\n    return $key === 'name';\n}, [ 'description' =\u003e \"An extended version of Laravel's collections\" ]);\n\n$c-\u003eall() === [\n    'name' =\u003e 'loilo/collection',\n    'description' =\u003e \"An extended version of Laravel's collections\",\n    'version' =\u003e '1.0.0'\n];\n```\n\n### `mergeInBefore()`/`mergeInAfter()`\nThese are equivalent to `insertBefore()`/`insertAfter()` but do not modify the collection.\n\n### `extract()`\nExtract structures from the collection, kind of an advanced [`pluck()`](https://laravel.com/docs/collections#method-pluck).\n\n**Details:** [Specification](spec/Extract.md)\n\n**Examples:**\n\nTake certain items from a list:\n```php\n$c = Collection::make([ 1, 2, 3 ])-\u003eextract([ 0, 1 ])\n$c-\u003eall() === [ 1, 2 ];\n```\n\nTransform a collection to a certain structure:\n```php\n$c = Collection::make([\n    'a' =\u003e [\n        'd' =\u003e 3,\n        'e' =\u003e 4\n    ],\n    'b' =\u003e [\n        'd' =\u003e 5,\n        'e' =\u003e 6\n    ],\n    'c' =\u003e [\n        'd' =\u003e 7,\n        'e' =\u003e 8\n    ]\n]);\n\n$c-\u003eextract([ 'a' ])-\u003eall() === [\n    'a' =\u003e [\n        'd' =\u003e 3,\n        'e' =\u003e 4\n    ]\n];\n\n$c-\u003eextract([ '*' =\u003e [ 'd' ] ])-\u003eall() === [\n    'a' =\u003e [ 'd' =\u003e 3 ],\n    'b' =\u003e [ 'd' =\u003e 5 ],\n    'c' =\u003e [ 'd' =\u003e 7 ]\n];\n```\n\n### `rearrange()`\nThis method re-orders a collection, according to a predefined order.\n\n```php\nCollection::make([ 'a', 'b', 'c' ]);\n    -\u003erearrange([ 2, 0, 1 ])\n    -\u003eall() === [ 'c', 'a', 'b' ];\n```\n\n#### Behavior of Unarrangeable Items\nWhen a collection item is not matched by the new order, it will be appended to the ordered items:\n\n```php\nCollection::make([ 'a', 'b', 'c' ]);\n    -\u003erearrange([ 1 ])\n    -\u003eall() === [ 'b', 'a', 'c' ];\n```\n\nThis behavior is controlled by the second argument of the `rearrange()` method. The default behavior `$c-\u003erearrange([ ... ])` is equivalent to `$c-\u003erearrange([ ... ], Collection::UNARRANGEABLE_APPEND)`.\n\nPossible values for this parameter are:\n\nClass Constant            | Description\n--------------------------|------------------------\n`UNARRANGEABLE_APPEND`    | Appends unarrangeable items after the rearranged ones.\n`UNARRANGEABLE_PREPEND`   | Prepends unarrangeable items before the rearranged ones.\n`UNARRANGEABLE_PARTITION` | Partitions the return value and maps rearranged and unarrangeable items to the `rearranged` respectively the `unarrangeable` key.\n`UNARRANGEABLE_DISCARD`   | Omits the unarrangeable items from the returned collection.\n`UNARRANGEABLE_THROW`     | Throws an `UnexpectedValueException` when an unarrangeable item is encountered.\n\n#### Map to Reordered Items\nBy default, the new order passed to `rearrange()` will map to the collection's keys. However, you may pass any callable as the 3rd parameter to the method to map the order values yourself:\n\n```php\nCollection::make([ 'a', 'b', 'c' ]);\n    -\u003erearrange(\n        [ 'b', 'a', 'c' ],\n        Collection::UNARRANGEABLE_APPEND,\n        function ($value, $key) {\n            return $value;\n        }\n    ),\n    -\u003eall() === [ 'b', 'a', 'c' ];\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floilo%2Fcollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floilo%2Fcollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floilo%2Fcollection/lists"}