petrenkoanton/php-collection
最新稳定版本:v1.1.2
Composer 安装命令:
composer require petrenkoanton/php-collection
包简介
Custom collection implementation
关键字:
README 文档
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.
Related projects
统计信息
- 总下载量: 597
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-10