atournayre/collection

0.8.1 2024-05-14 22:23 UTC

This package is auto-updated.

Last update: 2024-09-14 23:13:43 UTC


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 许可协议 许可。