vendor/api-platform/core/src/Core/Bridge/Symfony/Identifier/Normalizer/UuidNormalizer.php line 28

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer;
  12. use ApiPlatform\Exception\InvalidIdentifierException;
  13. use ApiPlatform\Symfony\UriVariableTransformer\UuidUriVariableTransformer;
  14. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  15. use Symfony\Component\Uid\Uuid;
  16. /**
  17.  * Denormalizes an UUID string to an instance of Symfony\Component\Uid\Uuid.
  18.  */
  19. final class UuidNormalizer implements DenormalizerInterface
  20. {
  21.     public function __construct()
  22.     {
  23.         trigger_deprecation('api-platform/core''2.7'sprintf('The class "%s" will be replaced by "%s".'self::class, UuidUriVariableTransformer::class));
  24.     }
  25.     public function denormalize($data$class$format null, array $context = [])
  26.     {
  27.         try {
  28.             return Uuid::fromString($data);
  29.         } catch (\InvalidArgumentException|\ValueError $e) { // catching ValueError will not be necessary anymore when https://github.com/symfony/symfony/pull/39636 will be released
  30.             throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e);
  31.         }
  32.     }
  33.     public function supportsDenormalization($data$type$format null, array $context = []): bool
  34.     {
  35.         return \is_string($data) && is_a($typeUuid::class, true);
  36.     }
  37. }