ideade/typed-collections

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/ideade/typed-collections

1.0.0 2024-11-03 14:23 UTC

This package is auto-updated.

Last update: 2025-12-31 00:22:34 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()]);