eugeneerg / collections-symfony
Symfony Serializer and VarDumper integration for eugeneerg/collections
Requires
- php: ^8.2
- eugeneerg/collections: ^1.4
- symfony/serializer: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- phpstan/phpstan: ^2.3.7
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11.5
- symfony/property-access: ^6.4 || ^7.0 || ^8.0
- symfony/property-info: ^6.4 || ^7.0 || ^8.0
- symfony/var-dumper: ^6.4 || ^7.0 || ^8.0
Suggests
- symfony/var-dumper: dump() and the debug toolbar show collection elements instead of internals
Provides
None
Conflicts
None
Replaces
None
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|nullacceptsnullas is. - The collection is built through its factory: element types and invariants are checked.
- Wrong data becomes
NotNormalizableValueExceptionwith the path (order.lines[2]). WithCOLLECT_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.