atournayre/collection

Collections library.

0.8.1 2024-05-14 22:23 UTC

This package is auto-updated.

Last update: 2024-11-14 23:31:30 UTC


README

This library provides a way to manipulate collections.

Installation

Use Composer to install the package:

composer require atournayre/collection

Collections

Examples

Typed 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

Typed Collection Immutable

// 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

Decimal Collection

$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

Contribute

Contributions to the package are always welcome!

License

All contents of this package are licensed under the MIT license.