Awesome
Symfony serializer bridge for Spiral Framework
Requirements
Make sure that your server is configured with following PHP version and extensions:
- PHP 8.1+
- Spiral framework ^3.0
- Symfony Serializer Component ^5.4 || ^6.0
- Symfony PropertyAccess Component ^5.4 || ^6.0
Installation
You can install the package via composer:
composer require spiral-packages/symfony-serializer
After package install you need to register bootloader from the package.
protected const LOAD = [
// ...
\Spiral\Serializer\Symfony\Bootloader\SerializerBootloader::class,
];
Note Bootloader
Spiral\Serializer\Bootloader\SerializerBootloader
can be removed. If you are usingspiral-packages/discoverer
, you don't need to register bootloader by yourself.
Configuration
The package is already configured by default, use these features only if you need to change the default configuration.
The package provides the ability to configure the normalizers
, encoders
and Symfony\Component\Serializer\Mapping\Loader\LoaderInterface
for Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory
used by the Symfony Serializer component.
Create a file app/config/symfony-serializer.php
.
Add the normalizers
, encoders
, metadataLoader
parameters. For example:
<?php
declare(strict_types=1);
use Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Normalizer;
use Spiral\Core\Container\Autowire;
return [
'normalizers' => [
new Normalizer\UnwrappingDenormalizer(),
new Normalizer\ProblemNormalizer(debug: false),
new Normalizer\UidNormalizer(),
new Normalizer\JsonSerializableNormalizer(),
new Normalizer\DateTimeNormalizer(),
new Normalizer\ConstraintViolationListNormalizer(),
new Normalizer\MimeMessageNormalizer(new Normalizer\PropertyNormalizer()),
new Normalizer\DateTimeZoneNormalizer(),
new Normalizer\DateIntervalNormalizer(),
new Normalizer\FormErrorNormalizer(),
new Normalizer\BackedEnumNormalizer(),
new Normalizer\DataUriNormalizer(),
new Autowire(Normalizer\ArrayDenormalizer::class), // by Autowire
Normalizer\ObjectNormalizer::class, // by class string
],
'encoders' => [
new Encoder\JsonEncoder(),
new Encoder\CsvEncoder(),
Encoder\XmlEncoder::class,
new Autowire(Encoder\YamlEncoder::class),
],
'metadataLoader' => new AnnotationLoader(new AnnotationReader()) // by default
// Other available loaders:
// 'metadataLoader' => new YamlFileLoader('/path/to/your/definition.yaml')
// 'metadataLoader' => new XmlFileLoader('/path/to/your/definition.xml')
];
Usage
Using with Spiral\Serializer\SerializerManager
. For example:
use Spiral\Serializer\SerializerManager;
$serializer = $this->container->get(SerializerManager::class);
$result = $manager->serialize($payload, 'json');
$result = $manager->serialize($payload, 'csv');
$result = $manager->serialize($payload, 'xml');
$result = $manager->serialize($payload, 'yaml');
$result = $manager->unserialize($payload, Post::class, 'json');
$result = $manager->unserialize($payload, Post::class, 'csv');
$result = $manager->unserialize($payload, Post::class, 'xml');
$result = $manager->unserialize($payload, Post::class, 'yaml');
// Getting a serializer `Spiral\Serializer\Symfony\Serializer`
$serializer = $manager->getSerializer('json');
// $serializer->serialize($payload, $context);
// $serializer->unserialize($payload, $type, $context);
// $serializer->normalize($data, $format, $context);
// $serializer->denormalize($data, $type, $format, $context);
// $serializer->supportsNormalization($data, $format, $context);
// $serializer->supportsDenormalization($data, $type, $format, $context);
// $serializer->encode($data, $format, $context);
// $serializer->decode($data, $format, $context);
// $serializer->supportsEncoding($format, $context);
// $serializer->supportsDecoding($format, $context);
Using with Symfony\Component\Serializer\SerializerInterface
. For example:
use Symfony\Component\Serializer\SerializerInterface;
$serializer = $this->container->get(SerializerInterface::class);
$result = $serializer->serialize($payload, 'json', $context);
$result = $serializer->serialize($payload, 'csv', $context);
$result = $serializer->serialize($payload, 'xml', $context);
$result = $serializer->serialize($payload, 'yaml', $context);
$result = $serializer->deserialize($payload, Post::class, 'json', $context);
$result = $serializer->deserialize($payload, Post::class, 'csv', $context);
$result = $serializer->deserialize($payload, Post::class, 'xml', $context);
$result = $serializer->deserialize($payload, Post::class, 'yaml', $context);
Note The
yaml
encoder requires thesymfony/yaml
package and is disabled when the package is not installed. Install thesymfony/yaml
package and the encoder will be automatically enabled.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.