phf/collection

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

This package is auto-updated.

Last update: 2024-09-06 02:00:40 UTC


README

用于验证元素集合的DRY辅助工具。

具有类型提示属性,它们可以在没有设置器的情况下确保集合的类型安全。

class Foo
{
    public StringCollection $bar;

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

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

扩展以用于自己的实体

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