kuaukutsu / ds-collection
Is the abstract layer which covers functionality the data structure Collection.
Installs: 22 672
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- php-ds/php-ds: ^1.3
Requires (Dev)
- phpunit/phpunit: ^9.4
- psalm/plugin-phpunit: ^0.13.0
- roave/infection-static-analysis-plugin: ^1.3
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: *
- vimeo/psalm: ^4.1
README
Коллекция объектов.
Примеры
$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 function (Dto $dto): bool { return $dto->name === 'first'; });
Индексация
В классе коллекции необходимо указать на основании какого свойства объекта индексировать коллекцию.
Это делается при помощи метода indexBy
, например:
/** * @param Dto $item * @return int */ protected function indexBy(object $item): int { return $item->id; }
/** * @param Dto $item * @return string */ protected function indexBy(object $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|object $item * @return array<scalar> */ protected function indexBy(object $item): array { return [(int)$item->id, (string)$item->name]; }
$collection = new DtoCollection(); $collection->attach(new Dto(1, 'first')); $collection->attach(new Dto(2, 'second')); $collection->attach(new Dto(3, 'third')); $dto = $collection->get(2, 'second');
Feature
- php >= 8.0
- WeakMap (https://www.php.net/manual/ru/class.weakmap.php, https://sergeymukhin.com/blog/php-8-weakmaps-slabye-karty)
Docker
local
docker build -t kuaukutsu/ds-collection:php . docker run --init -it --rm -v "$(pwd):/app" -w /app kuaukutsu/ds-collection:php sh
Testing
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
local
docker run --init -it --rm -v "$(pwd):/app" -w /app kuaukutsu/ds-collection:php ./vendor/bin/phpunit
Code Sniffer
local
docker run --init -it --rm -v "$(pwd):/app" -w /app kuaukutsu/ds-collection:php ./vendor/bin/phpcs
phpqa
docker run --init -it --rm -v "$(pwd):/app" -v "$(pwd)/phpqa/tmp:/tmp" -w /app jakzal/phpqa phpcs
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
local
docker run --init -it --rm -v "$(pwd):/app" -w /app kuaukutsu/ds-collection:php ./vendor/bin/psalm
phpqa
docker run --init -it --rm -v "$(pwd):/app" -v "$(pwd)/phpqa/tmp:/tmp" -w /app jakzal/phpqa psalm