small/collection

Provide PHP collection classes, with a selector to make complex queries on collections.

Maintainers

Package info

git.small-project.dev/lib/small-collection

pkg:composer/small/collection

Transparency log

Statistics

Installs: 1 373

Dependents: 13

Suggesters: 0

4.0.0 2026-08-14 02:58 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

  1. Generic collection
  2. Numeric collection
  3. String collection
  4. Selector
  5. List collection
  6. Set collection
  7. Map collection
  8. Integer and float collections
  9. Enum collection
  10. DateTime collection
  11. Boolean collection
  12. Object collection
  13. Sorted collection
  14. Immutable collection
  15. Records, type safety and extension

small-collection is part of small-project.
Copyright (c) 2023, 2024 Sébastien Kus.
Licensed under GNU GPL v3.