{"id":19264448,"url":"https://github.com/dcsg/php-immutable-collections","last_synced_at":"2025-10-20T02:57:59.692Z","repository":{"id":34050777,"uuid":"165499278","full_name":"dcsg/php-immutable-collections","owner":"dcsg","description":"Minimalist and Typed Immutable Collections for PHP","archived":false,"fork":false,"pushed_at":"2022-03-21T12:32:39.000Z","size":41,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-24T12:55:58.125Z","etag":null,"topics":["immutable-collections","php","php-collection","php-library","pipe"],"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/dcsg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-13T11:53:24.000Z","updated_at":"2024-02-13T17:26:56.000Z","dependencies_parsed_at":"2022-08-08T00:00:15.417Z","dependency_job_id":null,"html_url":"https://github.com/dcsg/php-immutable-collections","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/dcsg/php-immutable-collections","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcsg%2Fphp-immutable-collections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcsg%2Fphp-immutable-collections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcsg%2Fphp-immutable-collections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcsg%2Fphp-immutable-collections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcsg","download_url":"https://codeload.github.com/dcsg/php-immutable-collections/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcsg%2Fphp-immutable-collections/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278931653,"owners_count":26070788,"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-10-08T02:00:06.501Z","response_time":56,"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":["immutable-collections","php","php-collection","php-library","pipe"],"created_at":"2024-11-09T19:41:14.452Z","updated_at":"2025-10-08T10:50:33.770Z","avatar_url":"https://github.com/dcsg.png","language":"PHP","readme":"# Minimalist and Typed Immutable Collections for PHP\n\n[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D7.2-blue.svg)](https://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/dcsg/php-immutable-collections/v/stable)](https://packagist.org/packages/dcsg/php-immutable-collections)\n[![License](https://poser.pugx.org/dcsg/php-immutable-collections/license)](https://packagist.org/packages/dcsg/php-immutable-collections)\n[![Build Status](https://travis-ci.org/dcsg/php-immutable-collections.svg?branch=master)](https://travis-ci.org/dcsg/php-immutable-collections)\n[![StyleCI](https://github.styleci.io/repos/165499278/shield?branch=master)](https://github.styleci.io/repos/165499278)\n[![SymfonyInsight](https://insight.symfony.com/projects/8cc27627-24e7-407c-9839-766c9946eb2c/mini.svg)](https://insight.symfony.com/projects/8cc27627-24e7-407c-9839-766c9946eb2c)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=dcsg_php-immutable-collections\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=dcsg_php-immutable-collections)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=dcsg_php-immutable-collections\u0026metric=coverage)](https://sonarcloud.io/dashboard?id=dcsg_php-immutable-collections)\n[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=dcsg_php-immutable-collections\u0026metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=dcsg_php-immutable-collections)\n[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=dcsg_php-immutable-collections\u0026metric=bugs)](https://sonarcloud.io/dashboard?id=dcsg_php-immutable-collections)\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=dcsg_php-immutable-collections\u0026metric=security_rating)](https://sonarcloud.io/dashboard?id=dcsg_php-immutable-collections)\n[![Total Downloads](https://poser.pugx.org/dcsg/php-immutable-collections/downloads)](https://packagist.org/packages/dcsg/php-immutable-collections)\n\nA library that provides a set of minimalist, typed and piped Immutable Collections for PHP.\n\n###### Requirements\n\nPHP \u003e 7.2\n\n###### Installation\n\n```bash\ncomposer require dcsg/php-immutable-collections\n```\n\n#### What Problems does this solve?\n\nThe lack of Generics in PHP does not allow to have typed collections in PHP.\n\nThis Library provides two abstract Immutable Collections:\n\n* Immutable List Collection - ImmutableCollection\n* Immutable Set Collection - SetImmutableCollection\n\n#### Why Typed Immutable Collections\n\n###### Typed Collections\n\nSince PHP does not have [Generics](https://en.wikipedia.org/wiki/Generics_in_Java) like Java has, it's not possible to have native typed collections.\nThe Collections available in this Library are the foundation for you to create your own Typed Collections, you just need to extend them.\n\n###### Typed Immutable Collections for [DDD (Domain Driven Design)](https://en.wikipedia.org/wiki/Domain-driven_design)\n\nDDD is all about having your code speaking the business language, the called Ubiquitous Language. Without Collections in PHP this is very hard to achieve using `Arrays` because you cannot add behavior to them. So what usually happens you add that behavior to your entity but it shouldn't be there. Another problem is the mutability of `Arrays`, `VOs` (Value Objects) **MUST** be always **immutable** and that's impossible with `Arrays` and you should always guarantee that the `elements` of that `Array` are all of the same type. \n\n#### Collections vs [Arrays](https://secure.php.net/manual/pt_BR/language.types.array.php)\n\n* `Collections` and `Arrays` both represent a Group of Elements.\n* `Collections` are not natively supported by PHP while `Arrays` are.\n* `Arrays` is THE data structure of PHP and is used for almost everything.\n* `Arrays` don't allow you to add new behavior while `Collections` allow it.\n* `Arrays` don't allow you to define a **type** for its elements while `Collections` allow it.  \n\n#### Other PHP Collections\n\nThere are other Collections for PHP out there, naming a few we have [Doctrine Collections](https://github.com/doctrine/collections/tree/master/lib/Doctrine/Common/Collections) and [Illuminate Collections](https://github.com/illuminate/support/blob/master/Collection.php).\nThose collections they solve different problems, are tailored to their specific use case, and their APIs are extensive and more important those Collections are Mutable.\nThese combinations make it difficult to use those Collections for more simple use cases.\nThat's why the Collections we provide here, have a very small API and don't even expose the Iterator API.\nThis way you have the possibility to use them and extend it's behavior tailored for your needs. \n\n#### Features\n\n* Static construction for pipe usage.\n* Util methods like `isEmpty`, `count`, `toArray`, `contains`, `get` ,`map`, `filter`, `slice`, `merge`, `reverse`, `reduce`, `first`, `last`, `head`, `tail`.\n\n#### Basic usage\n\n###### Creating a Typed List Collection\n\n```php\n\u003c?php declare(strict_types=1);\n\nuse DCSG\\ImmutableCollections\\ImmutableCollection;\n\nfinal class MyStringCollection extends ImmutableCollection {\n    protected function validateItems(array $elements): void\n    {\n        foreach ($elements as $element) {\n            if (!\\is_string($element)) {\n                throw new InvalidArgumentException('Element is not a String.');\n            }\n        }\n    }\n}\n\n$collection = MyStringCollection::create(['foo', 'bar']);\necho $collection-\u003ecount(); // 2\n$slicedCollection = $collection-\u003eslice(0, 1); // MyStringCollection { $elements=['foo']}\n```\n\n###### Creating a Typed Set Collection\n\n```php\n\u003c?php declare(strict_types=1);\n\nuse DCSG\\ImmutableCollections\\SetImmutableCollection;\n\nfinal class MyStringSetCollection extends SetImmutableCollection {\n    protected function validateItems(array $elements): void\n    {\n        foreach ($elements as $element) {\n            if (!\\is_string($element)) {\n                throw new InvalidArgumentException('Element is not a String.');\n            }\n        }\n    }\n}\n\n$collection = MyStringSetCollection::create(['foo', 'bar']);\necho $collection-\u003ecount(); // 2\n$slicedCollection = $collection-\u003etail(); // MyStringSetCollection { $elements=['bar']}\n\n$collection = MyStringSetCollection::create(['foo', 'bar', 'foo']); // Throws InvalidArgumentException\n```\n\n## Examples\n\nWe provide two simple examples for better understanding. One related to [invoices](https://github.com/dcsg/php-immutable-collections/blob/master/examples/Invoices/app.php) and another one regarding [Legs of a Cargo Ship](https://github.com/dcsg/php-immutable-collections/blob/master/examples/CargoLegs/app.php).\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Testing\n\n```bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) and [CODE OF CONDUCT](CODE_OF_CONDUCT.md) for details.\n\n## Security\n\nIf you discover any security related issues, please email hi@dcsg.me instead of using the issue tracker.\n\n## Credits\n\n- [Daniel Gomes][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[link-author]: https://github.com/dcsg\n[link-contributors]: ../../contributors\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcsg%2Fphp-immutable-collections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcsg%2Fphp-immutable-collections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcsg%2Fphp-immutable-collections/lists"}