small / collection
Provide PHP collection classes, with a selector to make complex queries on collections.
Requires
- php: >=8.3
Requires (Dev)
- pestphp/pest: ^2.36
- phpstan/phpstan: ^2.0
- rector/rector: ^2.0
- symfony/var-dumper: ^6.4
This package is auto-updated.
Last update: 2026-08-14 04:16:16 UTC
README
small/collection provides typed, invariant-driven PHP collections and an in-memory selector API. Instead of passing loosely structured arrays around your application, choose a collection that expresses the rules of your data: ordered lists, unique sets, keyed maps, enums, dates, booleans, numeric values, objects, sorted values, or immutable state.
Requires PHP 8.3+.
composer require small/collection
Why use it?
A collection keeps the rules close to the data and gives PHPStan useful generic types:
use Small\Collection\Collection\DateTimeCollection;
use Small\Collection\Collection\ListCollection;
use Small\Collection\Collection\MapCollection;
use Small\Collection\Collection\SetCollection;
$list = new ListCollection(['first', 'third']);
$list->insertAt(1, 'second'); // keys stay 0..n-1
$tags = new SetCollection(['php', 'php']); // duplicates are ignored
$tags = $tags->union(['phpstan']);
$settings = new MapCollection([
'timezone' => 'Europe/Paris',
'retries' => 3,
]);
$timezone = $settings->require('timezone'); // throws when the key is missing
$dates = new DateTimeCollection([
new DateTimeImmutable('2026-08-01'),
new DateTimeImmutable('2026-08-15'),
]);
$next = $dates->after(new DateTimeImmutable('2026-08-10'))->earliest();
The package also provides RecordCollection and CollectionSelector for querying structured in-memory data with typed conditions and brackets.
Documentation
- Generic collection
- Numeric collection
- String collection
- Selector
- List collection
- Set collection
- Map collection
- Integer and float collections
- Enum collection
- DateTime collection
- Boolean collection
- Object collection
- Sorted collection
- Immutable collection
- Records, type safety and extension
small-collection is part of small-project.
Copyright (c) 2023, 2024 Sébastien Kus.
Licensed under GNU GPL v3.