headsnet/collections

集合的基本类

v0.2.1 2024-01-08 12:27 UTC

This package is auto-updated.

Last update: 2024-09-08 18:31:09 UTC


README

Build Status Coverage Latest Stable Version Total Downloads License

Headsnet Collections

安装

composer require headsnet/collections

使用

假设你有一些类 Foo,你想将其放入集合中

final class Foo
{
    public function __construct(
        public string $name
    ) {
    }
}

然后创建一个自定义命名的集合来保存 Foo 实例

/**
 * @extends AbstractImmutableCollection<Foo>
 *
 * @method self filter(callable $func)
 * @method self reverse()
 */
final class FooCollection extends AbstractImmutableCollection
{
    public function getItemClassName(): string
    {
        return Foo::class;
    }
}

然后实例化集合

$foo1 = new Foo();
$foo2 = new Foo();

$allFoos = new FooCollection([$foo1, $foo2]);

你将获得一个不可变、可迭代的对象,可以进行筛选、映射和遍历

foreach ($allFoos as $foo) {
    $this->assertInstanceOf(Foo::class, $foo);
}

贡献

欢迎贡献。请为每个修复/功能提交一个拉取请求。