PHP code example of spiral-packages / symfony-serializer
1. Go to this page and download the library: Download spiral-packages/symfony-serializer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spiral-packages / symfony-serializer example snippets
use Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
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 AttributeLoader() // by default
// Other available loaders:
// 'metadataLoader' => new YamlFileLoader('/path/to/your/definition.yaml')
// 'metadataLoader' => new XmlFileLoader('/path/to/your/definition.xml')
];
namespace App\Application\Bootloader;
use Spiral\Serializer\Symfony\EncodersRegistryInterface;
use Spiral\Serializer\Symfony\NormalizersRegistryInterface;
use Spiral\Boot\Bootloader\Bootloader;
final class AppBootloader extends Bootloader
{
public function boot(
NormalizersRegistryInterface $normalizersRegistry,
EncodersRegistryInterface $encodersRegistry,
): void {
// Add CustomNormalizer before ObjectNormalizer
$normalizersRegistry->register(normalizer: new CustomNormalizer(), priority: 699);
$encodersRegistry->register(new CustomEncoder());
}
}
use Spiral\Serializer\SerializerInterface;
use App\Repository\PostRepository;
final class PostController
{
public function __construct(
private readonly SerializerInterface $serializer,
private readonly PostRepository $repository,
) {}
public function show(string $postId): string
{
$post = $this->repository->find($postId);
return $this->serializer->serialize($post);
}
}
use App\Entity\Post;use Spiral\Serializer\SerializerInterface;
final class PostService
{
public function __construct(
private readonly SerializerInterface $serializer,
private readonly HttpClient $http,
) {}
public function show(string $postId): Post
{
$json = $this->http->get('https://example.com/posts/' . $postId);
return $this->serializer->unserialize($json, Post::class);
}
}
use Spiral\Serializer\SerializerManager;
use Spiral\Serializer\SerializerInterface;
use App\Repository\PostRepository;
final class PostController
{
private readonly SerializerInterface $serializer;
public function __construct(
SerializerManager $manager,
private readonly PostRepository $repository,
) {
$this->serializer = $manager->getSerializer('symfony-json');
}
public function show(string $postId): string
{
$post = $this->repository->find($postId);
return $this->serializer->serialize($post);
}
}
use App\Entity\Post;use Spiral\Serializer\SerializerInterface;
final class PostService
{
private readonly SerializerInterface $serializer;
public function __construct(
SerializerManager $manager,
private readonly HttpClient $http,
) {
$this->serializer = $manager->getSerializer('symfony-json');
}
public function show(string $postId): Post
{
$json = $this->http->get('https://example.com/posts/' . $postId);
return $this->serializer->unserialize($json, Post::class);
}
}
use Psr\Container\ContainerInterface;
use Spiral\Serializer\SerializerManager;
use App\Repository\PostRepository;
use App\Entity\Post;
/** @var PostRepository $repository */
$post = $repository->find($postId);
/** @var ContainerInterface $container */
$manager = $container->get(SerializerManager::class);
$serializedString = $manager->serialize($post , 'symfony-json');
$post = $manager->unserialize($serializedString , Post::class, 'symfony-json');