headsnet/collections

Base classes for collections

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

This package is auto-updated.

Last update: 2024-11-08 18:51:33 UTC


README

Build Status Coverage Latest Stable Version Total Downloads License

Headsnet Collections

Installation

composer require headsnet/collections

Usage

Assuming you have some class Foo that you want to put into a collection:

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

Then create a custom named collection to hold the Foo instances:

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

Then instantiate the collection:

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

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

You then have an immutable, iterable object that can be filtered, mapped and walked:

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

Contributing

Contributions are welcome. Please submit pull requests with one fix/feature per pull request.