{"id":15027160,"url":"https://github.com/teodoroleckie/collection","last_synced_at":"2026-02-26T05:24:06.532Z","repository":{"id":57070505,"uuid":"363039141","full_name":"teodoroleckie/collection","owner":"teodoroleckie","description":"⚡ Simple and powerful library for collections in PHP","archived":false,"fork":false,"pushed_at":"2021-05-19T12:50:20.000Z","size":44,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-26T02:27:03.317Z","etag":null,"topics":["collection","library","php","php-8","php-collection"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teodoroleckie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["teodoroleckie"],"custom":["https://www.paypal.com/donate?business=ZHYA2MTGA4884\u0026currency_code=USD"]}},"created_at":"2021-04-30T05:46:48.000Z","updated_at":"2021-05-19T12:38:05.000Z","dependencies_parsed_at":"2022-08-24T14:54:22.645Z","dependency_job_id":null,"html_url":"https://github.com/teodoroleckie/collection","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/teodoroleckie/collection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodoroleckie%2Fcollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodoroleckie%2Fcollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodoroleckie%2Fcollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodoroleckie%2Fcollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teodoroleckie","download_url":"https://codeload.github.com/teodoroleckie/collection/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodoroleckie%2Fcollection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269928191,"owners_count":24498385,"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-08-11T02:00:10.019Z","response_time":75,"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":["collection","library","php","php-8","php-collection"],"created_at":"2024-09-24T20:05:52.149Z","updated_at":"2025-10-28T22:03:19.170Z","avatar_url":"https://github.com/teodoroleckie.png","language":"PHP","funding_links":["https://github.com/sponsors/teodoroleckie","https://www.paypal.com/donate?business=ZHYA2MTGA4884\u0026currency_code=USD"],"categories":[],"sub_categories":[],"readme":"# Simple and powerful library for collections in PHP\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/tleckie/collection.svg?style=flat-square)](https://packagist.org/packages/tleckie/collection)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/teodoroleckie/collection/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/teodoroleckie/collection/?branch=main)\n[![Build Status](https://scrutinizer-ci.com/g/teodoroleckie/collection/badges/build.png?b=main)](https://scrutinizer-ci.com/g/teodoroleckie/collection/build-status/main)\n[![Total Downloads](https://img.shields.io/packagist/dt/tleckie/collection.svg?style=flat-square)](https://packagist.org/packages/tleckie/collection)\n[![Code Intelligence Status](https://scrutinizer-ci.com/g/teodoroleckie/collection/badges/code-intelligence.svg?b=main)](https://scrutinizer-ci.com/code-intelligence)\n\n### Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require tleckie/collection\n```\n\n## Usage\n\n# Hash\nHash collections should be used when you need the keys to be strings.\n```php\n\u003c?php\n\nuse Tleckie\\Collection\\Hash;\nuse Tleckie\\Collection\\HashInterface;\n\n// Create your own collection type\nclass UserCollection extends Hash {}\n\n$users = [\n    'user1' =\u003e new User('John'),\n    'user2' =\u003e new User('Pedro'),\n    'user3' =\u003e new User('Mario'),\n    'user4' =\u003e new User('Walter'),\n    'user5' =\u003e new User('Mario')\n];\n\n$userCollection = new UserCollection($users);\n\n/**\n * @param HashInterface $userCollection\n */\npublic function addCollection(HashInterface $userCollection){ ... }\n\n/**\n * @param UserCollection $userCollection\n */\npublic function addCollection(UserCollection $userCollection){ ... }\n\n```\n\n```php\nuse Tleckie\\Collection\\Hash;\n\n$collection = new Hash(['key' =\u003e 'value']);\n$collection-\u003eappend('otherKey', 'other value');\n```\n\n# Hash methods:\n\n```php\npublic function current(): mixed;\n\npublic function next(): void;\n\npublic function key(): null|string;\n\npublic function valid(): bool;\n\npublic function rewind(): void;\n\npublic function count(): int;\n\npublic function sortByKey(): HashInterface;\n\npublic function sortByValue(): HashInterface;\n\npublic function sort(callable$callable): HashInterface;\n\npublic function reverse(): HashInterface;\n\npublic function filter(callable $filterFunction): HashInterface;\n\npublic function find(callable $findFunction): HashInterface;\n\npublic function empty(): HashInterface;\n\npublic function hasKey(string $key): bool;\n\npublic function hasValue(mixed $item): bool;\n\npublic function get(string $key): mixed;\n\npublic function append(string $key, mixed $item): HashInterface;\n\npublic function prepend(string $key, mixed $item): HashInterface;\n\npublic function remove(string $key): HashInterface;\n\npublic function shuffle(): HashInterface;\n\npublic function toArray(): array;\n```\n\n# Sequence:\nThe sequences are used when the key of the collection is not important besides being ordered. Many of its methods are immutable.\n\nIf you want you can create your own type of collection\n\n```php\n\u003c?php\n\nuse Tleckie\\Collection\\Sequence;\nuse Tleckie\\Collection\\SequenceInterface;\n\n// Create your own collection type\nclass UserCollection extends Sequence {}\n\n$users = [\n    new User('John'),\n    new User( 'Pedro'),\n    new User('Mario'),\n    new User('Walter'),\n    new User('Mario')\n];\n\n$userCollection = new UserCollection($users);\n\n/**\n * @param SequenceInterface $userCollection\n */\npublic function addCollection(SequenceInterface $userCollection){ ... }\n\n/**\n * @param UserCollection $userCollection\n */\npublic function addCollection(UserCollection $userCollection){ ... }\n```\n\n```php\nuse Tleckie\\Collection\\Sequence;\n\n$collection = new Sequence(['value', 'value2']);\n$collection-\u003eappend('other value');\n\n```\n# Sequence methods:\n```php\npublic function current(): mixed;\n\npublic function next(): void;\n\npublic function key(): null|int;\n\npublic function valid(): bool;\n\npublic function rewind(): void;\n\npublic function count(): int;\n\npublic function sortByKey(): SequenceInterface;\n\npublic function sortByValue(): SequenceInterface;\n\npublic function sort(callable$callable): SequenceInterface;\n\npublic function reverse(): SequenceInterface;\n\npublic function filter(callable $filterFunction): SequenceInterface;\n\npublic function find(callable $findFunction): SequenceInterface;\n\npublic function empty(): SequenceInterface;\n\npublic function hasKey(int $index): bool;\n\npublic function hasValue(mixed $item): bool;\n\npublic function get(int $index): mixed;\n\npublic function append(mixed $item): SequenceInterface;\n\npublic function prepend(mixed $item): SequenceInterface;\n\npublic function remove(int $index): SequenceInterface;\n\npublic function shuffle(): SequenceInterface;\n\npublic function toArray(): array;\n\n```\n\n# Collection:\nUse the collection when you have numeric and string keys.\n\n```php\n\u003c?php\n\nuse Tleckie\\Collection\\Collection;\n\n// Create your own collection type\nclass UserCollection extends Collection {}\n\n$users = [\n    new User(1, 'John'),\n    new User(2, 'Pedro'),\n    new User(3, 'Mario'),\n    new User(4, 'Walter'),\n    new User(5, 'Mario')\n];\n\n$collection = new UserCollection($users);\n\n\n\n// iterate\nforeach($collection as $user){\n    $user-\u003ename();\n}\n\n\n// count elements\n$collection-\u003ecount();   //5\ncount($collection);     //5\n\n\n// filter\n$collection = $collection-\u003efilter(function (int $key, User $user) {\n    return $user-\u003eid() \u003e 4;\n}); // UserCollection( [ User(5) ] );\n\n\n// filterNot\n$collection = $collection-\u003efilterNot(function (int $key, User $user) {\n    return $user-\u003eid() \u003e 4;\n}); // UserCollection( [ User(1),User(2),User(3),User(4) ] );\n\n\n// find\n$collection = $collection-\u003efind(function (int $key, User $user) {\n    return $user-\u003ename() === 'Mario';\n}); // UserCollection( [ User(3), User(5) ] );\n\n\n// findIndex\n$collection = $collection-\u003efind(function (int $key, User $user) {\n    return $key === 1;\n}); // UserCollection( [ User(2) ] );\n\n\n// sort\n$collection = $collection-\u003esort(function (User $current, User $next) {\n    if ($current-\u003eid() === $next-\u003eid()) {\n        return 0;\n    }\n    return ($current-\u003eid() \u003e $next-\u003eid()) ? -1 : 1;\n}); // UserCollection( [ User(5), User(4), User(3), User(2), User(1) ] );\n```\n\n## Other methods:\n\n```php\n$collection-\u003eshuffle(): CollectionInterface;\n\n$collection-\u003ereverse(): CollectionInterface;\n\n$collection-\u003eprepend(mixed $item): CollectionInterface;\n\n$collection-\u003eappend(mixed $item): CollectionInterface;\n\n$collection-\u003etoArray(): array;\n\n$collection-\u003epush(mixed $item): CollectionInterface;\n\n$collection-\u003epull(): mixed;\n\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteodoroleckie%2Fcollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteodoroleckie%2Fcollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteodoroleckie%2Fcollection/lists"}