kuaukutsu / ds-collection
Is the abstract layer which covers functionality the data structure Collection.
Installs: 42 507
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1.31
- ext-json: *
- php-ds/php-ds: ^1.3
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^10.1
- rector/rector: ^2.0
- roave/security-advisories: dev-latest
- slevomat/coding-standard: ^8.7
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^6.13
- dev-master
- 2.1.3
- 2.1.2
- 2.1.1
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.3.3
- 1.3.2
- 1.3.1
- 1.2.2
- 1.2.1
- 1.1.1
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-feature/update_20250809
- dev-feature/update_20250808
- dev-feature/internal
- dev-feature/map
- dev-feature/operations
- dev-feature/update_20250807
- dev-tech-stack-file
This package is auto-updated.
Last update: 2025-08-09 07:21:05 UTC
README
Коллекция объектов.
Tech Stack
kuaukutsu/ds-collection is built on the following main stack:
- PHP – Languages
- PHPUnit – Testing Frameworks
- GitHub Actions – Continuous Integration
Примеры
$collection = new DtoCollection(); $collection->attach(new Dto(1, 'first')); $collection->attach(new Dto(2, 'second')); $collectionOther = new DtoCollection(); $collectionOther->attach(new Dto(3, 'third')); $collectionOther->attach(new Dto(4, 'fourth')); $collection->merge($collectionOther);
Фильтрация
$collection = new DtoCollection(); $collection->attach(new Dto(1, 'first')); $collection->attach(new Dto(2, 'second')); $collection->attach(new Dto(3, 'first')); $collection->attach(new Dto(4, 'second')); $collectionByFiltered = $collection->filter( static fn(Dto $dto): bool => $dto->name === 'first' );
Сортировка
$collection = new DtoCollection(); $collection->attach(new Dto(1, 'first')); $collection->attach(new Dto(2, 'second')); $collection->attach(new Dto(3, 'first')); $collection->attach(new Dto(4, 'second')); $sortCollection = $collection->sort( static fn(Dto $a, Dto $b): int => strcmp($a->name, $b->name) );
Индексация
В классе коллекции необходимо указать на основании какого свойства объекта индексировать коллекцию.
Это делается при помощи метода indexBy
, например:
/** * @param Dto $item * @return int */ protected function indexBy($item): int { return $item->id; }
/** * @param Dto $item * @return string */ protected function indexBy($item): string { return $item->name; }
Это позволяет получить быстрый доступ к объекту по ключу индекса, например для indexBy по ключу name:
$collection = new DtoCollection(); $collection->attach(new Dto(1, 'first')); $collection->attach(new Dto(2, 'second')); $dto = $collection->get('second');
Составные ключи
Ключ индексирования может быть составным, например:
/** * @param Dto $item * @return array<scalar> */ protected function indexBy($item): array { return [$item->id, $item->name]; }
$collection = new DtoCollection(); $collection->attach(new Dto(1, 'first')); $collection->attach(new Dto(2, 'second')); $collection->attach(new Dto(22, 'second')); $dto = $collection->get(2, 'second');
Testing
Unit testing
The package is tested with PHPUnit. To run tests:
make phpunit
PHP_VERSION=8.1 make phpunit
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
make psalm
PHP_VERSION=8.1 make psalm
make phpstan
PHP_VERSION=8.4 make phpstan
Code Sniffer
make phpcs
Rector
make rector