src/Core/Serializer/Normalizer/IdentityNormalizer.php line 38

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Core\Serializer\Normalizer;
  4. use App\Component\Identity\Encoder\IdEncoderInterface;
  5. use App\Component\Identity\IdInterface;
  6. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  7. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  8. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  9. final class IdentityNormalizer implements NormalizerInterfaceDenormalizerInterfaceCacheableSupportsMethodInterface
  10. {
  11.     private IdEncoderInterface $encoder;
  12.     public function __construct(IdEncoderInterface $encoder)
  13.     {
  14.         $this->encoder $encoder;
  15.     }
  16.     public function normalize($objectstring $format null, array $context = []): array|string|int|float|bool|\ArrayObject|null
  17.     {
  18.         return $this->encoder->encode($object);
  19.     }
  20.     public function supportsNormalization($datastring $format null): bool
  21.     {
  22.         return true === $data instanceof IdInterface;
  23.     }
  24.     public function denormalize($datastring $typestring $format null, array $context = []): IdInterface
  25.     {
  26.         /* @var $type IdInterface */
  27.         return $type::createFromString($data);
  28.     }
  29.     public function supportsDenormalization($datastring $typestring $format null, array $context = []): bool
  30.     {
  31.         return true === is_subclass_of($typeIdInterface::class);
  32.     }
  33.     public function hasCacheableSupportsMethod(): bool
  34.     {
  35.         return __CLASS__ === self::class;
  36.     }
  37. }