gbprod/uuid-normalizer

使用 Symfony Serializer 序列化 Ramsey Uuid 的标准化器

v7.0.0 2024-01-16 08:00 UTC

This package is auto-updated.

Last update: 2024-08-31 00:29:29 UTC


README

Tests Latest Stable Version Total Downloads Latest Unstable Version

使用 Ramsey UuidSymfony Serializer 序列化。

安装

composer require gbprod/uuid-normalizer

原因

默认情况下,Symfony Serializer 无法处理 Ramsey Uuid 的序列化和反序列化。您可能会遇到此类错误

Not a time-based UUID
500 Internal Server Error - UnsupportedOperationException

设置

在您的 app/config/service.yml 文件中

services:
  uuid_normalizer:
    class: GBProd\UuidNormalizer\UuidNormalizer
    tags:
      - { name: serializer.normalizer }

  uuid_denormalizer:
    class: GBProd\UuidNormalizer\UuidDenormalizer
    tags:
      - { name: serializer.normalizer }

或者使用 xml

<services>
    <service id="uuid_normalizer" class="GBProd\UuidNormalizer\UuidNormalizer">
        <tag name="serializer.normalizer" />
    </service>
    <service id="uuid_denormalizer" class="GBProd\UuidNormalizer\UuidDenormalizer">
        <tag name="serializer.normalizer" />
    </service>
</services>

或者使用 php

use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition('GBProd\UuidNormalizer\UuidNormalizer');
$definition->addTag('serializer.normalizer');
$container->setDefinition('uuid_normalizer', $definition);

$definition = new Definition('GBProd\UuidNormalizer\UuidDenormalizer');
$definition->addTag('serializer.normalizer');
$container->setDefinition('uuid_denormalizer', $definition);

或者构建您自己的序列化器

use GBProd\UuidNormalizer\UuidDenormalizer;
use GBProd\UuidNormalizer\UuidNormalizer;

$serializer = new Serializer([
    new UuidNormalizer(),
    new UuidDenormalizer(),
    // Other normalizers...
]);

要求

  • PHP 7.4+

贡献

请自由贡献,有关更多信息,请参阅 CONTRIBUTING.md 文件。