{"id":19326007,"url":"https://github.com/linkorb/collection","last_synced_at":"2025-02-24T06:18:00.180Z","repository":{"id":62517360,"uuid":"99411180","full_name":"linkorb/collection","owner":"linkorb","description":"Super powered collections for PHP","archived":false,"fork":false,"pushed_at":"2017-08-05T08:57:34.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-01-06T06:43:53.477Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/linkorb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-05T08:53:52.000Z","updated_at":"2017-08-05T08:54:14.000Z","dependencies_parsed_at":"2022-11-02T13:45:34.653Z","dependency_job_id":null,"html_url":"https://github.com/linkorb/collection","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkorb%2Fcollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkorb%2Fcollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkorb%2Fcollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linkorb%2Fcollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linkorb","download_url":"https://codeload.github.com/linkorb/collection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240427316,"owners_count":19799471,"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-10T02:12:11.755Z","updated_at":"2025-02-24T06:18:00.145Z","avatar_url":"https://github.com/linkorb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Collection\n==========\n\nThis library implements a few helpful collection classes and interfaces.\n\n## TypedArray\n\nThis class behaves exactly like an array, but only allows items of a specified type:\n\n```php\n$monkeys = new \\Collection\\TypedArray(Monkey::class);\n\n$monkeys-\u003eadd(new Monkey('Bubbles')); // OK\n$monkeys[] = new Monkey('King Kong'); // OK\n$monkeys-\u003eadd(new Snake('Kaa')); // throws CollectionException\n\n// You can iterate over the collection\nforeach ($monkeys as $monkey) {\n    echo $monkey-\u003egetName();\n}\n```\n\n## Adding items to a collection\n\nYou can add items in the regular array way:\n\n```php\n$monkeys[] = new Monkey('King Kong');\n```\n\nOr use the `add()` method\n\n```php\n$monkeys-\u003eadd(new Monkey('King Kong'));\n```\n\n## Identifiable interface\n\nThe `Identifiable` class forces a class to implement the `identifier()` method. This method\nshould return a key that makes this instance of a class unique.\nMaybe it's an ID, a key, a name, or an email address, as long as it uniquely identifies the item.\n\n```php\nclass Monkey implements \\Collection\\Identifiable\n{\n    protected $name;\n    public function __construct($name)\n    {\n        $this-\u003ename = $name;\n    }\n\n    public function identifier()\n    {\n        return $this-\u003ename;\n    }\n}\n```\n\nWhen adding instances of this class to a collection, the identifier will automatically be used as the key:\n\n```php\n$monkeys = new \\Collection\\TypedArray(Monkey::class);\n\n$m1 = new Monkey('George');\n$m2 = new Monkey('Koko');\n\n$monkeys[] = $m1; // array contains 1 item\n$monkeys[] = $m2; // array contains 2 item\n$monkeys[] = $m2; // array still contains 2 items!\n$monkeys-\u003eadd($m2); // array still contains 2 items!\n\n$monkeys-\u003ehasKey('George')); // returns true\n$monkeys-\u003ehasKey('King Kong')); // returns false\nisset($monkeys['George']); // returns true\n```\n\n## Using collections in classes\n\n\n```php\nclass Zoo\n{\n    protected $monkeys;\n\n    public function __construct()\n    {\n        $this-\u003emonkeys = new Collection\\TypedArray(Monkey::class);\n    }\n\n    public function getMonkeys()\n    {\n        return $this-\u003emonkeys;\n    }\n}\n\n$zoo = new Zoo();\n$zoo-\u003egetMonkeys()-\u003eadd(new Monkey('Koko');\n\nforeach ($zoo-\u003egetMonkeys() as $monkey() {\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinkorb%2Fcollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinkorb%2Fcollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinkorb%2Fcollection/lists"}