pogulailo/collection
最新稳定版本:v1.0.0
Composer 安装命令:
composer require pogulailo/collection
包简介
Strictly typed collection
README 文档
README
A simple and concise implementation of strictly typed arrays in PHP. No extra code, just type-checked arrays.
Usage
First you need to create your own collection class which extends the GenericCollection and sets its type:
use Pogulailo\Collection\GenericCollection; class CustomerCollection extends GenericCollection { public function __construct(...$values) { parent::__construct(Customer::class, ...$values); } }
That's all, then you can enjoy all the advantages of strictly typed arrays:
function getCustomers(): CustomerCollection { $customers = new CustomerCollection(); $customers->append(new Customer()); $customers->append(new Customer()); $customers->append(new Customer()); return $customers; } function doSomething(CustomerCollection $customers): void { foreach ($customers as $customer) { // Do what you need to do } } $customers = getCustomers(); doSomething($customers);
You can choose not to create your own collection class, but then you will need to do additional type checking:
use Pogulailo\Collection\GenericCollection; function getCustomers(): GenericCollection { $customers = new GenericCollection(Customer::class); $customers->append(new Customer()); $customers->append(new Customer()); $customers->append(new Customer()); return $customers; } function doSomething(GenericCollection $customers): void { // In this case, you need to check the collection type first if ($customers->getType() !== Customer::class) { throw new Exception('I need customers, more customers...') } foreach ($customers as $customer) { // Do what you need to do } } $customers = getCustomers(); doSomething($customers);
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2022-09-13