piotrpress/typed-collection

A strict typed collection for PHP with runtime validation for scalar and object-based item types.

Maintainers

Package info

github.com/PiotrPress/typed-collection

pkg:composer/piotrpress/typed-collection

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-24 20:44 UTC

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 be Class, Interface or Enum name or one of the following:
    • Type:String
    • Type:Int
    • Type:Float
    • Type:Bool
    • Type:Array
    • Type:Object
    • Type:Callable
    • Type: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

License

MIT