stefna / ds-collection
Common collection classes for stefna
Installs: 3 149
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- ext-ds: *
Requires (Dev)
- bnf/phpstan-psr-container: ^1.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.9
- stefna/codestyle: ^1.15
- tomasvotruba/type-coverage: ^0.0.7
This package is auto-updated.
Last update: 2024-12-03 12:32:00 UTC
README
This package is a lightweight config loader with type safety as the primary corner stone.
Requirements
PHP 8.2 or higher.
Installation
composer require stefna/ds-collection
Usage
Generic implementations
We provide 2 generic implementations of the collections for convenience and internal usage
List:
<?php use Stefna\Collection\GenericListCollection; use Stefna\Collection\GenericMapCollection; /** @var GenericListCollection<ClassType> */ $collection = new GenericListCollection(ClassType::class); $collection[] = new ClassType(); // or $collection->add(new ClassType()); /** @var GenericMapCollection<ClassType> */ $newCollection = $collection->indexBy(fn (ClassType $o) => (string)$o->id);
Map
<?php use Stefna\Collection\GenericListCollection; use Stefna\Collection\GenericMapCollection; /** @var GenericMapCollection<ClassType> */ $collection = new GenericMapCollection(ClassType::class); $collection['id'] = new ClassType(); // or $collection->add('id', new ClassType()); foreach ($collection as $key => $object) { $key === string; $object === ClassType::class; } /** @var GenericListCollection<ClassType> */ $listCollection = $collection->toList(); foreach ($collection as $key => $object) { $key === int; $object === ClassType::class; }
Creating Typed Collections
It is preferable to subclass AbstractListCollection
and AbstractMapCollection
<?php use Stefna\Collection\AbstractListCollection; final class RowCollection extends AbstractListCollection { protected static string $defaultCollectionType = ClassType::class; } $collection = new RowCollection(); $collection->getType() === ClassType::class;
Contribute
We are always happy to receive bug/security reports and bug/security fixes
License
The MIT License (MIT). Please see License File for more information.