Search by

msstc4php / dto-collection

maxim.shamaev@gmail.com

Typed lists for PHP 8.1+: immutable, mutable and Doctrine MongoDB ODM variants with natively typed element signatures.

Package info

github.com/msstc4php/dto-collection

pkg:composer/msstc4php/dto-collection

Statistics

Installs: 10

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.1.0 2026-10-26 13:35 UTC

This package is auto-updated.

Last update: 2026-11-29 08:47:23 UTC


README

Build Status PHP Version PHPStan Last commit License: MIT

Typed lists for PHP 8.1+. The element type is a native type hint of the list class, so PHP itself rejects a wrong element:

$users->push($user);        // push(User ...$elements): void
$first = $users->first();   // first(): ?User
$users->push('alice');      // TypeError

PHP has no generic arrays. This library supplies the behaviour as traits, and each list class adds a few one-line methods that carry the element type. You write that class by hand or let dto-collection-generator write it. Without any class of your own, ListOf and MutableListOf check types at runtime.

  • Three variants: immutable (with*/without* return a copy), mutable (in-place mutators return void), and Doctrine MongoDB ODM (usable as collectionClass of EmbedMany/ReferenceMany).
  • Keys are always 0..n-1. Every operation reindexes.
  • Element types: int, string, float, bool, classes and interfaces, ?T, unions A|B, intersections A&B.
  • About 70 methods: the useful parts of lodash-php and voku/Arrayy, without their aliases and bugs, plus pipe functions for PHP 8.5's |>.
  • Elements are compared with ===, or with equals() for classes implementing Equatable.

Installation

composer require msstc4php/dto-collection

Version 1.x supports PHP 8.1–8.4. Version 2.x, for PHP 8.5+, has the same API. For the ODM variant, also install doctrine/mongodb-odm (^2.10).

Quick start

use MSSTC4PHP\DtoCollection\ListOf;

$scores = ListOf::integers([3, 1, 2]);
$scores->sum();                                   // 6
$scores->sortedBy(fn (int $score) => $score)->toArray();  // [1, 2, 3]

$users = ListOf::of(User::class, $repository->findAll());
$admins = $users->filter(fn (User $user) => $user->isAdmin());
$names = $admins->mapToArray(fn (User $user) => $user->name());
$byTeam = $users->groupBy(fn (User $user) => $user->team());   // array<string, ListOf<User>>

$users->with('alice');   // ElementTypeMismatch: Element #0 must be User, string given

Documentation

Development

make install   # package and tool dependencies
make verify    # PHPStan, CS-Fixer, Rector, deptrac, PHP 8.1 lint, tests on local PHP and 8.1
make test-odm  # ODM integration tests against MongoDB in Docker (port 27018)
make infection # mutation testing

Security

See SECURITY.md.

License

MIT, see LICENSE.