https://github.com/edumarques/typed-collections
Typed collections are an implementation of typed lists. Collections allow us to have traversable lists of a predetermined type and its subtypes. Its behavior prevents the looseness of arrays in PHP.
https://github.com/edumarques/typed-collections
collections php type-safe
Last synced: 4 months ago
JSON representation
Typed collections are an implementation of typed lists. Collections allow us to have traversable lists of a predetermined type and its subtypes. Its behavior prevents the looseness of arrays in PHP.
- Host: GitHub
- URL: https://github.com/edumarques/typed-collections
- Owner: edumarques
- License: mit
- Created: 2023-05-20T14:36:52.000Z (over 2 years ago)
- Default Branch: v2.x
- Last Pushed: 2024-01-14T22:40:17.000Z (about 2 years ago)
- Last Synced: 2025-09-27T11:19:57.580Z (5 months ago)
- Topics: collections, php, type-safe
- Language: PHP
- Homepage: https://packagist.org/packages/edumarques/typed-collections
- Size: 77.1 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
Typed Collections
================


[](https://app.circleci.com/pipelines/github/edumarques)
[](https://codecov.io/gh/edumarques/typed-collections)
## Description
Typed collections are an implementation of typed lists. Collections allow us to have traversable lists of a predetermined type and its subtypes. Its behavior prevents the looseness of arrays in PHP.
This library contains four collection classes: mutable and immutable lists, and mutable and immutable dictionaries. Mutable lists/dictionaries have its internal array altered whenever we perform operations on it. Immutable lists/dictionaries generate a new instance of itself when operations are done on it.
## Installation
```
composer require edumarques/typed-collections:2.*
```
## Basic usage
```php
use EduardoMarques\TypedCollections\TypedCollection;
use EduardoMarques\TypedCollections\TypedCollectionImmutable;
use EduardoMarques\TypedCollections\Enum\ScalarType;
use EduardoMarques\TypedCollections\Enum\NonScalarType;
$typedCollection1 = TypedCollection::create(ScalarType::INTEGER);
$typedCollection2 = TypedCollection::create(ScalarType::STRING);
$typedCollectionImmutable1 = TypedCollectionImmutable::create(NonScalarType::CALLABLE);
$typedCollectionImmutable2 = TypedCollectionImmutable::create(\stdClass::class);
```
```php
use EduardoMarques\TypedCollections\TypedDictionary;
use EduardoMarques\TypedCollections\TypedDictionaryImmutable;
use EduardoMarques\TypedCollections\Enum\ScalarType;
use EduardoMarques\TypedCollections\Enum\NonScalarType;
$typedDictionary1 = TypedDictionary::create(ScalarType::INTEGER, ScalarType::STRING);
$typedDictionary2 = TypedDictionary::create(ScalarType::STRING, ScalarType::DOUBLE);
$typedDictionaryImmutable1 = TypedDictionaryImmutable::create(ScalarType::INTEGER, NonScalarType::CALLABLE);
$typedDictionaryImmutable2 = TypedDictionaryImmutable::create(ScalarType::STRING, \stdClass::class);
```
## Collection
A PHP implementation of an array list. Its type is specified at instantiation/construction. The class will perform runtime type checks to validate the appropriate values are being added. Many of the standard array functionalities are encapsulated in this class.
## Dictionary
Dictionaries extend Collections' functionalities. The main difference is that they work with associative arrays, where you map keys to values.
## Supported types:
- `ScalarType::INTEGER`
- `ScalarType::STRING`
- `ScalarType::BOOLEAN`
- `ScalarType::DOUBLE`
- `NonScalarType::ARRAY`
- `NonScalarType::CALLABLE`
- `class-string`
## Support for abstract classes and interfaces
Collections and Dictionaries will check inheritance, so if you require a base class, derived classes can be added safely.
## Contributing
Contributors are always welcome! For more information on how you can contribute, please read our [contribution guideline](CONTRIBUTING.md).