ramsey / uuid

:snowflake: A PHP library for generating universally unique identifiers (UUIDs).

Home Page:https://uuid.ramsey.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for symfony/serializer

Legion112 opened this issue · comments

A new package for symfony/serializer adapter

Background/problem

I often use the Symfony/serializer and create DTO classes with UuidInterface.
I have created a manual normalizer denormalized each time.
It would be nice to have a package for that and list it in the suggestion section of the composer.json

Proposal/solution

<?php

declare(strict_types=1);

namespace App\Serializer;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;

class UuidNormalizer implements ContextAwareNormalizerInterface, ContextAwareDenormalizerInterface, CacheableSupportsMethodInterface
{
    /**
     * @inheritdoc
     * @psalm-suppress MissingParamType
     */
    public function supportsNormalization($data, ?string $format = null, array $context = []): bool
    {
        return $data instanceof UuidInterface;
    }

    /**
     * @psalm-suppress MoreSpecificImplementedParamType
     * @param UuidInterface $object
     */
    public function normalize($object, ?string $format = null, array $context = [])
    {
        return $object->toString();
    }

    /**
     * @psalm-suppress MissingParamType
     * @inheritdoc
     */
    public function supportsDenormalization($data, string $type, ?string $format = null, array $context = []): bool
    {
        return $type === UuidInterface::class;
    }

    public function denormalize($data, string $type, ?string $format = null, array $context = [])
    {
        if (Uuid::isValid($data)) {
            return Uuid::fromString($data);
        }
        return null;
    }

    public function hasCacheableSupportsMethod(): bool
    {
        return true;
    }
}

Also, we need to register this normalizer during the installation of the package (this is optional)

Alternatives

Additional context

If you’d like to create and maintain a package for this, I’ll be happy to add it to the suggestion section of ramsey/uuid composer.json. I don’t have the bandwidth to maintain such a package myself.

Where I work for we extensively use ramsey/uuid and symfony/serializer altogether for 4 years now, and we have an implementation we copy/paste from project to project. Recently we chose to add it into an completely unrelated package that we use in all those projects, in an rationalisation effort (we maintain numerous operational business applications whose lifespan goes from 1 to many years). Here is the implementation: https://github.com/makinacorpus/php-normalization/blob/master/src/Bridge/Symfony/Serializer/RamseyUuidNormalizer.php

Searching through packagist, at the time, I was able to find at least 2 different composer packages which provide more or less the same implementation, but because we don't want to introduce any new dependency, especially because we maintain those projects for many years and all dependencies don't evolve at the same pace, causing many dependency management trouble, we chose to put our own at a random place where dependencies are not a problem.

I strongly advice you do the same, the implementation is about 10 lines of real code (outside of usual boilerplate such as use statements and method signature), copy/paste it somewhere you always use and just live with it. It's fine.