petrenkoanton / php-collection
Custom collection implementation
v1.1.2
2024-01-26 19:55 UTC
Requires
- php: ^8.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.7
- phpunit/phpunit: ^9.0
- psalm/plugin-phpunit: ^0.18.4
- ramsey/coding-standard: ^2.1
- squizlabs/php_codesniffer: ^3.8
- vimeo/psalm: ^5.16
README
Installation | Functionality | Usage | For developers | License | Related projects
Installation
Requirements
- php 8.0 or higher
Composer
composer require petrenkoanton/php-collection
Functionality
Public methods
Collection
Method | Exception |
---|---|
__construct(Collectable ...$items) | - |
add(Collectable $item): void | InvalidItemTypeException | InvalidConstructorDeclarationException |
filter(callable $callback): Collection | - |
getItems(): array | - |
getItem(int $key): Collectable | InvalidKeyException |
first(): Collectable | InvalidKeyException |
count(): int | - |
Exceptions
Main library exception is CollectionException.
Code | Message pattern | Exception | Parent |
---|---|---|---|
100 | Collection: %s | Expected item type: %s | Given: %s | InvalidItemTypeException | CollectionException |
101 | Collection: %s | Err: Invalid constructor declaration | InvalidConstructorDeclarationException | CollectionException |
200 | Collection: %s | Invalid key: %d | InvalidKeyException | CollectionException |
Usage
<?php declare(strict_types=1); use Collection\Arrayable; use Collection\Collectable; use Collection\Collection; // All collection items must implements `Collection\Collectable` interface interface EntityInterface extends Collectable { } class Entity implements Arrayable, EntityInterface { public function __construct(private int $id) { } public function getId(): int { return $this->id; } public function toArray(): array { return ['id' => $this->id]; } } class EntityInterfaceCollection extends Collection { public function __construct(EntityInterface ...$items) { parent::__construct(...$items); // Mandatory call of the parent constructor } } $firstEntity = new Entity(1); $secondEntity = new Entity(2); $collection = new EntityInterfaceCollection($firstEntity); $collection->add($secondEntity); $firstEntityId = $collection->first()->getId(); // 1 $count = $collection->count(); // 2 $collectionAsArray = $collection->toArray() // [['id' => 1], ['id' => 2]];
For developers
Requirements
Utils:
- make
- docker-compose
Setup
Initialize
Create ./docker/.env
make init
Build container with the different php version
php 8.0
make up80
php 8.1
make up81
php 8.2
make up82
php 8.3
make up83
Also you need to run this command before build container with another php version. It will remove network and previously created container.
make down
Other commands
Go inside of the container
make inside
Check php version
make php-v
Check package version
make v
Run tests and linters
Run PHPUnit tests with code coverage
make test-c
Run Psalm
make psalm
Run PHP_CodeSniffer
make phpcs
Or by all-in-one command from the inside of the container
composer check-all
License
The php-collection library is open-sourced software licensed under the MIT license.