headsnet / collections
Base classes for collections
Installs: 8 553
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- doctrine/collections: >1
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- symplify/easy-coding-standard: ^12.1
README
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.