atournayre / collection
集合库。
0.8.1
2024-05-14 22:23 UTC
Requires
- php: >=8.1
- aimeos/map: ^3.4
- atournayre/assert: ^0
- atournayre/types: ^0
- doctrine/collections: ^2.1
- webmozart/assert: ^1.11
Requires (Dev)
- phpunit/phpunit: ^10.0
Conflicts
- phpstan/phpstan: <0.12.20
- vimeo/psalm: <4.6.1 || 4.6.2
README
此库提供了一种操作集合的方法。
安装
使用 Composer 安装此包
composer require atournayre/collection
集合
示例
类型化集合
// Samples classes class Person { public function __construct( public string $name ) {} } class People extends TypedCollection { protected static string $type = Person::class; }
// Create collection $collection = People::createAsList([ new Person('John'), ]); $collection[] = new Person('Jack'); // Add item
不可变类型化集合
// Samples classes class Person { public function __construct( public string $name ) {} } class People extends TypedCollectionImmutable { protected static string $type = Person::class; }
// Create collection $collection = People::createAsList([ new Person('John'), ]); $collection[] = new Person('Jack'); // Throws a RuntimeException
十进制集合
$collection = DecimalValueCollection::fromArray([ DecimalValue::create(4.235, 3), DecimalValue::fromInt(1), DecimalValue::fromString('2'), DecimalValue::fromFloat(3.01, 2), ], 2); $collection[0]->toFloat(); // 4.24 $collection[1]->toFloat(); // 1.00 $collection[2]->toFloat(); // 2.00 $collection[3]->toFloat(); // 3.01
贡献
欢迎对包的贡献!
许可协议
本包的所有内容均受 MIT 许可协议 许可。