piotrpress / typed-collection
A strict typed collection for PHP with runtime validation for scalar and object-based item types.
v1.0.0
2026-08-24 20:44 UTC
Requires
- php: >=8.2
Requires (Dev)
- phpunit/phpunit: ^11.0
- piotrpress/composer-formatter: ^1.0
This package is auto-updated.
Last update: 2026-08-24 20:46:40 UTC
README
A strict typed collection for PHP with runtime validation for scalar and object-based item types.
Installation
composer require piotrpress/typed-collection
Example
require_once __DIR__ . '/vendor/autoload.php'; use PiotrPress\TypedCollection; $collection = new TypedCollection( type: Example::class, values: [ new Example() ] ); $collection[0]; // returns Example instance from key 0 of the collection $collection[] = new Example(); // adds new Example instance to the collection foreach( $collection as $value ) // iterates over the collection
Usage
use PiotrPress\TypedCollection; use PiotrPress\TypedCollection\Type; new TypedCollection( Type|string $type, array $values = [] );
Parameters
type- the type of the collection. Can beClass,InterfaceorEnumname or one of the following:Type:StringType:IntType:FloatType:BoolType:ArrayType:ObjectType:CallableType:Iterable
values- an array of values to initialize the collection with. All values must be of the specified type.
Methods
TypedCollection extends ArrayObject, for more information about the methods available, please refer to the PHP documentation.
Development
docker compose run --rm php composer install
docker compose run --rm php composer format
docker compose run --rm php composer test
Requirements
- PHP
>= 8.1