承接 hexium-agency/symfony-serializer-for-laravel 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hexium-agency/symfony-serializer-for-laravel

最新稳定版本:v0.0.6

Composer 安装命令:

composer require hexium-agency/symfony-serializer-for-laravel

包简介

Laravel package for the symfony/serializer component

README 文档

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.

统计信息

  • 总下载量: 324
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-20