lsr / serializer
Laser framework core - Serializer
Requires
- php: >= 8.4
- dibi/dibi: ^5
- symfony/property-access: ^8|^7
- symfony/property-info: ^8|^7
- symfony/serializer: ^8|^7
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- nette/di: ^3.2
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^2.0
- phpstan/phpstan-dibi: ^2.0
- phpstan/phpstan-nette: ^2.0
- phpunit/phpunit: ^12
- roave/security-advisories: dev-latest
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-08 12:21:49 UTC
README
lsr/serializer adds a typed object mapper, date/time and Dibi row normalizers, and Nette DI integration around Symfony Serializer.
Requirements
- PHP
>= 8.4. - Dibi
^5and Symfony Serializer, PropertyInfo and PropertyAccess^8|^7(installed by Composer). - No PHP extensions are declared directly in this package's manifest; selected database drivers and serialization formats may need their own extensions.
- The optional
Lsr\Serializer\DI\SerializerExtensionsintegration requires Nette DI in the consuming application.nette/di^3.2is currently a development dependency, not a runtime requirement of this package.
Installation
composer require lsr/serializer
Mapping data without a container
require_once __DIR__ . '/vendor/autoload.php'; use Lsr\Serializer\Mapper; use Lsr\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; final class PlayerProfile { public string $name; public DateTimeImmutable $joinedAt; } $mapper = new Mapper(new Serializer([ new DateTimeNormalizer(), new ObjectNormalizer(propertyTypeExtractor: new ReflectionExtractor()), ])); $profile = $mapper->map([ 'name' => 'Ada', 'joinedAt' => '2026-01-01T12:00:00+00:00', ], PlayerProfile::class);
Mapper::map($data, $type, $context) delegates to Symfony's DenormalizerInterface; it does not swallow conversion errors or validate business rules. Add normalizers appropriate to your DTOs, such as Symfony's ArrayDenormalizer or BackedEnumNormalizer. See the full composition in MapperTest.
Normalizers and configuration
Lsr\Serializer\Normalizer\DateTimeNormalizernormalizes dates to RFC 3339 strings by default and accepts date strings or arrays with adatekey and optionaltimezonewhen denormalizing. Its context keys aredatetime_format,datetime_timezoneanddatetime_cast; casting supportsint,floatandarray. ADateTimeInterfacetarget resolves toDateTimeImmutable.Lsr\Serializer\Normalizer\DibiRowNormalizerhandlesDibi\Rowvalues. Register it explicitly when required; it is not in the DI extension's default normalizer list.SerializerHelper::handleCircularReference()returns an object'scode,idornameproperty, in that order, or null. This is the DI integration's default circular-reference handler, not an identity policy for every application.
For an existing Nette DI application, register the extension under a chosen name:
extensions: serializer: Lsr\Serializer\DI\SerializerExtensions
SerializerExtensions registers Symfony's serializer and a mapper service. Its schema supplies reflection/serializer/constructor property extractors, date/time, enum, JSON-serializable and object normalizers, an array denormalizer, and JSON/XML/CSV encoders. Configure extractors, normalizers, denormalizers or encoders to replace the respective lists; the corresponding extraExtractors, extraNormalizers, extraDenormalizers and extraEncoders append entries. Ordering matters when multiple normalizers support the same value. Use the schema and factory wiring in the linked source when customizing context and services.
Development
CI runs on PHP 8.4 and 8.5. Install development dependencies and run the same three checks locally:
composer install --prefer-dist --no-interaction --no-progress composer cs vendor/bin/phpstan analyse --no-progress vendor/bin/phpunit --no-coverage
composer cs checks PHP coding style without changing files; composer cs:fix applies fixes (composer cbf is an alias). The test script enables Xdebug coverage mode; coverage reports need a compatible coverage driver. Configuration is in phpunit.xml, phpstan.neon and .php-cs-fixer.php.
The mapper and normalizer tests operate on in-memory objects, including Dibi rows; DI tests check that serializer components can be discovered by their service tags. No database service is needed. CI disables coverage, so it does not require a coverage driver.
AI coding assistance
See LSR Skills for AI agent skills for working with the LSR framework.
License
Licensed under the MIT License.