PHP code example of hexium-agency / symfony-serializer-for-laravel

1. Go to this page and download the library: Download hexium-agency/symfony-serializer-for-laravel 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/ */

    

hexium-agency / symfony-serializer-for-laravel example snippets




/**
 * @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,
        ],
    ],
];

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');
    }
}

use HexiumAgency\SymfonySerializerForLaravel\Facades\Serializer;

class DiscordNotificationParser {
    public function parse(string $data): DiscordNotification {
        return Serializer::deserialize($data, DiscordNotification::class, 'json');
    }
}
bash
php artisan vendor:publish --tag="symfony-serializer-for-laravel-config"