{"id":17705155,"url":"https://github.com/jeromegamez/typed-collection","last_synced_at":"2025-08-20T23:32:46.245Z","repository":{"id":24611180,"uuid":"102046333","full_name":"jeromegamez/typed-collection","owner":"jeromegamez","description":"Type-safe collections based on Laravel Collections","archived":false,"fork":false,"pushed_at":"2024-04-24T23:53:15.000Z","size":105,"stargazers_count":37,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-05-02T00:41:04.951Z","etag":null,"topics":["collection","collections","laravel","php","type-safety","typed"],"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/jeromegamez.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"jeromegamez"}},"created_at":"2017-08-31T21:18:22.000Z","updated_at":"2024-06-21T12:56:11.149Z","dependencies_parsed_at":"2024-06-21T12:56:09.569Z","dependency_job_id":"eeea7fcb-c5f0-4ecb-8656-e4abb8a056d5","html_url":"https://github.com/jeromegamez/typed-collection","commit_stats":{"total_commits":44,"total_committers":3,"mean_commits":"14.666666666666666","dds":0.06818181818181823,"last_synced_commit":"eb691cf274cf1127f068c99b05f832452b6be7be"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeromegamez%2Ftyped-collection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeromegamez%2Ftyped-collection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeromegamez%2Ftyped-collection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeromegamez%2Ftyped-collection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeromegamez","download_url":"https://codeload.github.com/jeromegamez/typed-collection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229890910,"owners_count":18140193,"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":["collection","collections","laravel","php","type-safety","typed"],"created_at":"2024-10-24T22:06:46.735Z","updated_at":"2025-08-20T23:32:46.239Z","avatar_url":"https://github.com/jeromegamez.png","language":"PHP","funding_links":["https://github.com/sponsors/jeromegamez"],"categories":[],"sub_categories":[],"readme":"# Type-safe PHP collections based on [Laravel Collections] \n\n[![Latest Stable Version](https://poser.pugx.org/gamez/typed-collection/v/stable)](https://packagist.org/packages/gamez/typed-collection)\n[![Total Downloads](https://poser.pugx.org/gamez/typed-collection/downloads)](https://packagist.org/packages/gamez/typed-collection)\n[![Tests](https://github.com/jeromegamez/typed-collection/actions/workflows/tests.yml/badge.svg)](https://github.com/jeromegamez/typed-collection/actions/workflows/tests.yml)\n[![Sponsor](https://img.shields.io/static/v1?logo=GitHub\u0026label=Sponsor\u0026message=%E2%9D%A4\u0026color=ff69b4)](https://github.com/sponsors/jeromegamez)\n\n\u003e [!NOTE]  \n\u003e Laravel 11 added the `ensure()` collection method that verifies that all elements of\n\u003e a collection are of a given type or list of types. However, this verification does not\n\u003e prevent items of different types to be added at a later time.\n\n\u003e [!NOTE]  \n\u003e If you use Laravel collections combined with Larastan/PHPStan, you won't need this library\n\u003e and can just™ annotate your collection classes directly.\n\n## Installation\n\nThe package can be installed with [Composer]:\n\n```bash\n$ composer require gamez/typed-collection\n```\n\n## Usage\n\n```php\nclass Person\n{\n    public $name;\n\n    public function __construct($name)\n    {\n        $this-\u003ename = $name;\n    }\n}\n\n$taylor = new Person('Taylor');\n$jeffrey = new Person('Jeffrey');\n```\n\n### Typed Collections\n\n```php\nuse Gamez\\Illuminate\\Support\\TypedCollection;\n\n/**\n * @extends TypedCollection\u003carray-key, Person\u003e \n */\nclass People extends TypedCollection\n{\n    protected static array $allowedTypes = [Person::class];\n}\n\n$people = People::make([$taylor, $jeffrey])\n    -\u003eeach(function (Person $person) {\n        printf(\"This is %s.\\n\", $person-\u003ename);\n    });\n/* Output:\nThis is Taylor.\nThis is Jeffrey.\n*/\n\ntry {\n    People::make('Not a person');\n} catch (InvalidArgumentException $e) {\n    echo $e-\u003egetMessage().PHP_EOL;\n}\n/* Output:\nOutput: A People collection only accepts items of the following type(s): Person.\n*/\n```\n\n### Lazy Typed Collections\n\n```php\nuse Gamez\\Illuminate\\Support\\LazyTypedCollection;\n\n/**\n * @extends LazyTypedCollection\u003carray-key, Person\u003e \n */\nclass LazyPeople extends LazyTypedCollection\n{\n    protected static array $allowedTypes = [Person::class];\n}\n\n$lazyPeople = LazyPeople::make([$taylor, $jeffrey])\n    -\u003eeach(function (Person $person) {\n        printf(\"This is %s.\\n\", $person-\u003ename);\n    });\n/* Output:\nThis is Lazy Taylor.\nThis is Lazy Jeffrey.\n*/\n\ntry {\n    LazyPeople::make('Nope!');\n} catch (InvalidArgumentException $e) {\n    echo $e-\u003egetMessage().PHP_EOL;\n}\n/* Output:\nOutput: A People collection only accepts objects of the following type(s): Person.\n*/\n```\n\n### Mixed collections\n\n```php\n/**\n * @extends LazyTypedCollection\u003carray-key, int|string|Person\u003e \n */\nclass MixedTypeCollection extends TypedCollection\n{\n    protected static array $allowedTypes = ['int', 'string', Person::class];\n}\n```\n\n### Supported types\n\nSupported types are class strings, like `Person::class`, or types recognized by the\n[`get_debug_type()` function](https://www.php.net/get-debug-type), `int`, `float`,\n`string`, `bool`, and `array`.\n\n### Helper functions\n\nThe `typedCollect()` helper function enables you to dynamically create typed collections\non the fly:\n\n```php\n$dateTimes = typedCollect([new DateTime(), new DateTime()], DateTimeInterface::class);\n```\n\nFor further information on how to use Laravel Collections,\nhave a look at the [official documentation].\n\n[Laravel Collections]: https://laravel.com/docs/collections\n[official documentation]: https://laravel.com/docs/collections\n[Composer]: https://getcomposer.org \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeromegamez%2Ftyped-collection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeromegamez%2Ftyped-collection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeromegamez%2Ftyped-collection/lists"}