Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/serializer/Normalizer/BackedEnumNormalizer.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\PropertyInfo\Type;
  12. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  13. use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
  14. /**
  15.  * Normalizes a {@see \BackedEnum} enumeration to a string or an integer.
  16.  *
  17.  * @author Alexandre Daubois <alex.daubois@gmail.com>
  18.  */
  19. final class BackedEnumNormalizer implements NormalizerInterfaceDenormalizerInterfaceCacheableSupportsMethodInterface
  20. {
  21.     /**
  22.      * {@inheritdoc}
  23.      *
  24.      * @return int|string
  25.      *
  26.      * @throws InvalidArgumentException
  27.      */
  28.     public function normalize($objectstring $format null, array $context = [])
  29.     {
  30.         if (!$object instanceof \BackedEnum) {
  31.             throw new InvalidArgumentException('The data must belong to a backed enumeration.');
  32.         }
  33.         return $object->value;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function supportsNormalization($datastring $format null): bool
  39.     {
  40.         return $data instanceof \BackedEnum;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      *
  45.      * @throws NotNormalizableValueException
  46.      */
  47.     public function denormalize($datastring $typestring $format null, array $context = [])
  48.     {
  49.         if (!is_subclass_of($type\BackedEnum::class)) {
  50.             throw new InvalidArgumentException('The data must belong to a backed enumeration.');
  51.         }
  52.         if (!\is_int($data) && !\is_string($data)) {
  53.             throw NotNormalizableValueException::createForUnexpectedDataType('The data is neither an integer nor a string, you should pass an integer or a string that can be parsed as an enumeration case of type '.$type.'.'$data, [Type::BUILTIN_TYPE_INTType::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? nulltrue);
  54.         }
  55.         try {
  56.             return $type::from($data);
  57.         } catch (\ValueError $e) {
  58.             if (isset($context['has_constructor'])) {
  59.                 throw new InvalidArgumentException('The data must belong to a backed enumeration of type '.$type);
  60.             }
  61.             throw NotNormalizableValueException::createForUnexpectedDataType('The data must belong to a backed enumeration of type '.$type$data, [$type], $context['deserialization_path'] ?? nulltrue0$e);
  62.         }
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function supportsDenormalization($datastring $typestring $format null): bool
  68.     {
  69.         return is_subclass_of($type\BackedEnum::class);
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function hasCacheableSupportsMethod(): bool
  75.     {
  76.         return true;
  77.     }
  78. }