petrenkoanton/php-collection
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
petrenkoanton/php-collection 适用场景与选型建议
petrenkoanton/php-collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 606 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「structure」 「collection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 petrenkoanton/php-collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 petrenkoanton/php-collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 petrenkoanton/php-collection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Extension provide very simply use enum for models (and others) in yii2
Traits to build collections of specific objects
TYPO3 extension providing content elements powered by gridelements and bootstrap. Available elements: container, columns, tabs, accordion, tile unit and card.
ItalyStrap Helpers
统计信息
- 总下载量: 606
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-10