nextdev/collection

This package is abandoned and no longer maintained. The author suggests using the phf/collection package instead.

Collections utility class

This package has no released version yet, and little information is available.


README

DRY helper for collections of validated elements.

With type hinted properties they can ensure type safety on collections without setters.

class Foo
{
    public StringCollection $bar;

    public function __construct()
    {
        $this->bar = new StringCollection();
    }
}

$foo = new Foo();
$foo->bar[] = 123; // throws InvalidArgumentException

Extend for own entities:

class BarEntity
{
    public string $baz;
}

class BarCollection extends \nextdev\Collection\Collection
{
    protected static $invalidElementMessageAllowed = BarEntity::class;

    public function validate(
        $value
    ): bool {
        return $value instanceof BarEntity;
    }
}

class Foo
{
    public BarCollection $bar;

    public function __construct()
    {
        $this->bar = new BarCollection();
    }
}