phf/collection

Collections utility class

v1.0.0 2021-05-05 18:22 UTC

This package is auto-updated.

Last update: 2024-04-06 01:07:34 UTC


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 \PhF\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();
    }
}