Collection
This package provides powerful and extensible Collection, PaginatedCollection and LazyCollection implementations for the Beauty Framework. It offers a PHP-compliant, chainable, and efficient API for working with structured data.
⚠️ This is an optional module. You need to install and configure it manually.
Installation
make composer require beauty-framework/collection
# or without Docker
composer require beauty-framework/collection
Overview
Collection— eager evaluation, array-like behavior.LazyCollection— lazy evaluation using generators.PaginatedCollection- defaultCollectionwith paginator- Separate interfaces for operations, accessors, and lazy behavior.
- Built-in support for alternative storages (
Array,Ds\Map).
Storages
By default collection use ArrayStorage, but if you install ds extension, collection has use DsMapStorage. In framework Dockerfile this extension is already included.
pecl install ds
Usage
Creating a Collection
use Beauty\Collection\Collection;
$collection = new Collection([
['name' => 'Kirill'],
['name' => 'Hui'],
['name' => 'Zver'],
]);
Filtering and Mapping
$result = $collection
->where('name', 'Kirill')
->map(fn($item) => strtoupper($item['name']))
->toArray();
Sorting
$sorted = $collection->sortBy('name');
Getting First / Last Items
$first = $collection->first();
$last = $collection->last();
Lazy Collection
use Beauty\Collection\LazyCollection;
$lazy = new LazyCollection(fn() => yield from range(1, 1000000));
$result = $lazy
->filter(fn($x) => $x % 2 === 0)
->take(10)
->flatMap(fn($x) => [$x, $x * 10])
->toArray();
Available Methods
Common Operations
map(callable)filter(callable)each(callable)where(string $field, mixed $value)sortBy(string $field)chunk(int $size)reduce(callable, $initial)flatMap(callable)paginate(int $perPage)(PaginatedCollection only)
Accessors
first()last()toArray()count()
Interfaces
CollectionInterfacePaginatedCollectionInterfaceSupportsOperationsSupportsAccessorsLazyCollectionInterface
API
Collection
map(callable $callback): staticTransform each value using the callback.each(callable $callback): staticRun a callback over each item (no transform).filter(?callable $callback = null): staticKeep only items that pass the callback (or truthy if null).where(string $field, mixed $value): staticFilter items where a field/property equals the given value.sortBy(string|callable $callback, string $direction = 'asc'): staticSort by a field or callback.values(): staticRe-index the collection numerically.first(): mixedGet the first item in the collection.last(): mixedGet the last item in the collection.get(mixed $key, mixed $default = null): mixedRetrieve a value by key with optional default.put(mixed $key, mixed $value): voidStore a value by key.has(mixed $key): boolDetermine if a key exists.remove(mixed $key): voidRemove a value by key.toArray(): arrayConvert the collection to a plain array.IteratorAggregate::getIterator()Required to supportforeachloops.ArrayAccessEnables$collection[$key]syntax.Countable::count()Enablescount($collection).JsonSerializable::jsonSerialize()Enablesjson_encode($collection).
Paginated Collection
like a Collection, but with method paginate(int $perPage, int $page = 1)
LazyCollection
map(callable $callback): staticLazily transform each value using the callback.filter(?callable $callback = null): staticLazily filter values using the callback (or truthy if null).take(int $limit): staticTake the first N elements from the collection.chunk(int $size): staticBreak the collection into chunks of the given size.reduce(callable $callback, mixed $initial = null): mixedAggregate values down to a single result.flatMap(callable $callback): staticFlatten and transform each item using the callback.first(): mixedGet the first item in the lazy collection.toArray(): arrayConvert the lazy collection to a plain array.IteratorAggregate::getIterator()Required to supportforeachloops.
Resources
- This package repo: https://github.com/beauty-framework/collection