Search by

eugeneerg / collections-symfony

EugeneErg

Symfony Serializer and VarDumper integration for eugeneerg/collections

Package info

github.com/EugeneErg/collections-symfony

pkg:composer/eugeneerg/collections-symfony

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-12-02 23:48 UTC

This package is auto-updated.

Last update: 2026-12-03 07:24:34 UTC


README

Symfony integration for eugeneerg/collections: immutable typed collections in Serializer and in dump().

Русская версия · Documentation

Why

Symfony Serializer turns a collection into JSON without help, but cannot turn JSON back into one: ObjectNormalizer reads a JSON list as object properties and silently returns an empty collection, and with a generic PHPDoc it passes a plain array to the constructor. There is no attribute or tag for this. CollectionDenormalizer fixes that.

dump() shows a collection's internal hashes next to its elements. CollectionCaster shows only the elements.

Installation

composer require eugeneerg/collections-symfony

PHP 8.2+, Symfony 6.4, 7 or 8.

Serializer

In a Symfony application with autoconfiguration, register the service; it gets the serializer.normalizer tag and runs before ObjectNormalizer:

# config/services.yaml
services:
    EugeneErg\Collections\Symfony\Serializer\CollectionDenormalizer: ~

Standalone, put it before ObjectNormalizer:

$serializer = new Serializer(
    [new CollectionDenormalizer(), new BackedEnumNormalizer(), new DateTimeNormalizer(), new ObjectNormalizer()],
    [new JsonEncoder()],
);

$users = $serializer->deserialize('[{"name":"Ann"}]', Users::class, 'json'); // Users

Collections work anywhere the serializer goes: as the root type, as a typed property of a DTO, in #[MapRequestPayload].

  • Each element is denormalized by the serializer into the type the collection declares, so enums, dates and your objects use their usual normalizers. Nested collections (ImmutableMap<string, Users>) are built too.
  • For a union of classes (Cat|Dog) each class is tried in order; the first that can be built wins. Users|null accepts null as is.
  • The collection is built through its factory: element types and invariants are checked.
  • Wrong data becomes NotNormalizableValueException with the path (order.lines[2]). With COLLECT_DENORMALIZATION_ERRORS, as in #[MapRequestPayload], it is reported like any other invalid field. Exceptions from your invariants are not wrapped: they are your domain's to translate.

Serialization needs nothing extra: collections are JsonSerializable and iterable.

dump()

Nothing to configure: when symfony/var-dumper is installed, the caster is registered by Composer's autoloader, so dump(), the profiler and the debug toolbar show

App\Users {#12
  count: 2
  items: array:2 [ … ]
}

If you create cloners before the autoloader runs, call CollectionCaster::register() or pass CollectionCaster::CASTERS to VarCloner::addCasters().

Versioning

Semantic Versioning. The public API is the two classes above and CollectionCaster::CASTERS.

Development

composer check   # code style, PHPStan, tests

CI also runs the tests on PHP 8.2–8.5 with the lowest and the highest dependencies, and mutation testing with Infection at 100% MSI.

License

MIT, see LICENSE.