hexium-agency/symfony-serializer-for-laravel

Laravel package for the symfony/serializer component

v0.0.6 2024-05-15 06:26 UTC

This package is auto-updated.

Last update: 2024-12-08 17:25:01 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.