hexium-agency / symfony-serializer-for-laravel
Laravel package for the symfony/serializer component
Fund package maintenance!
Alexandre Gérault
Requires
- php: ^8.3
- illuminate/contracts: ^10 || ^11
- phpdocumentor/reflection-docblock: ^5.4
- spatie/laravel-package-tools: ^1.16
- symfony/cache: ^7.0
- symfony/property-access: ^7.0
- symfony/property-info: ^7.0
- symfony/serializer: ^7.0
- symfony/validator: ^7.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.15
- nunomaduro/collision: ^7.10 ||^8.1
- orchestra/testbench: ^8.22 || ^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.36
README
The symfony/serializer component is a great tool to serialize and deserialize objects. This package provides a bridge between Laravel and the symfony/serializer component. Then it should be very easy to use with DI in your application code, as well as adding some normalizers and encoders.
Installation
You can install the package via composer:
composer require hexium-agency/symfony-serializer-for-laravel
You can publish the config file with:
php artisan vendor:publish --tag="symfony-serializer-for-laravel-config"
This is the contents of the published config file:
<?php /** * @return array{ * normalizers: array<array{id: string, priority?: int}>, * encoders: array<array{id: string, priority?: int}>, * list_extractors: array<array{id: string, priority?: int}>, * type_extractors: array<array{id: string, priority?: int}>, * access_extractors: array<array{id: string, priority?: int}>, * initializable_extractors: array<array{id: string, priority?: int}>, * defaultContext: array<string, mixed> * } */ return [ 'normalizers' => [ [ 'id' => 'serializer.normalizer.datetimezone', 'priority' => -915, ], [ 'id' => 'serializer.normalizer.dateinterval', 'priority' => -915, ], [ 'id' => 'serializer.normalizer.datetime', 'priority' => -910, ], [ 'id' => 'serializer.normalizer.json_serializable', 'priority' => -950, ], [ 'id' => 'serializer.denormalizer.unwrapping', 'priority' => 1000, ], [ 'id' => 'serializer.normalizer.uid', 'priority' => -890, ], [ 'id' => 'serializer.normalizer.object', 'priority' => -1000, ], [ 'id' => 'serializer.denormalizer.array', 'priority' => -990, ], [ 'id' => 'serializer.normalizer.backed_enum', 'priority' => -915, ], ], 'encoders' => [ [ 'id' => 'serializer.encoder.xml', ], [ 'id' => 'serializer.encoder.json', ], [ 'id' => 'serializer.encoder.yaml', ], [ 'id' => 'serializer.encoder.csv', ], ], 'list_extractors' => [ [ 'id' => 'property_info.reflection_extractor', 'priority' => -1000, ], [ 'id' => 'property_info.serializer_extractor', 'priority' => -999, ], ], 'type_extractors' => [ [ 'id' => 'property_info.php_doc_extractor', 'priority' => -990, ], [ 'id' => 'property_info.reflection_extractor', 'priority' => -1002, ], ], 'access_extractors' => [ [ 'id' => 'property_info.reflection_extractor', 'priority' => -1000, ], ], 'initializable_extractors' => [ [ 'id' => 'property_info.reflection_extractor', 'priority' => -1000, ], ], ];
Usage
Normal dependency injection
class SendDiscordNotification { private SerializerInterface $serializer; private HttpClientInterface $httpClient; private string $webhookUrl; public function __construct(SerializerInterface $serializer, HttpClientInterface $httpClient, string $webhookUrl) { $this->serializer = $serializer; $this->httpClient = $httpClient; $this->webhookUrl = $webhookUrl; } public function __invoke(DiscordNotification $notification) { $data = $this->serializer->serialize($notification, 'json'); $this->httpClient->request('POST', $this->webhookUrl, [ 'body' => $data, ]); } }
class DiscordNotificationParser { private SerializerInterface $serializer; public function __construct(SerializerInterface $serializer) { $this->serializer = $serializer; } public function parse(string $data): DiscordNotification { return $this->serializer->deserialize($data, DiscordNotification::class, 'json'); } }
Using the facade
use HexiumAgency\SymfonySerializerForLaravel\Facades\Serializer; class DiscordNotificationParser { public function parse(string $data): DiscordNotification { return Serializer::deserialize($data, DiscordNotification::class, 'json'); } }
Add normalizers and encoders
You can add normalizers and encoders to the serializer by adding them to the config file. The priority is used to sort the normalizers and encoders. The ids of the services must match some of your container.
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.
Credits
License
The MIT License (MIT). Please see License File for more information.