ideade/typed-collections

1.0.0 2024-11-03 14:23 UTC

This package is auto-updated.

Last update: 2025-07-01 00:12:22 UTC


README

PHP collections with runtime type checking

Installation:

composer require ideade/typed-collections:~1.0

Supported types

  • Scalar: bool, int, float, string ✅
  • Any of your classes ✅
  • resource(including closed), array ❌

Usage

  1. Define your collection class, or use one of scalar(from Ideade\TypedCollections\ScalarCollections):
use Ideade\TypedCollections\TypedCollection;

final class ExampleCollection extends TypedCollection
{
    protected function valueType() : string
    {
        return Example::class;
    }
}
  1. Use it as a normal array, or use the following methods:
$collection = new ExampleCollection();

// Add an element
$collection->add(new Example());

// Get item by key
$collection->get(0);

// Add an element by key
$collection->addByKey(0, new Example());

// Delete element by key
$collection->remove(0);

// Set collection items (check the type of each item)
$collection->setItems([new Example(), new Example(), new Example()]);