vendor/symfony/serializer/Normalizer/ArrayDenormalizer.php line 35

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\BadMethodCallException;
  13. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  14. use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
  15. /**
  16.  * Denormalizes arrays of objects.
  17.  *
  18.  * @author Alexander M. Turek <me@derrabus.de>
  19.  *
  20.  * @final
  21.  */
  22. class ArrayDenormalizer implements ContextAwareDenormalizerInterfaceDenormalizerAwareInterfaceCacheableSupportsMethodInterface
  23. {
  24.     use DenormalizerAwareTrait;
  25.     /**
  26.      * {@inheritdoc}
  27.      *
  28.      * @throws NotNormalizableValueException
  29.      */
  30.     public function denormalize(mixed $datastring $typestring $format null, array $context = []): array
  31.     {
  32.         if (null === $this->denormalizer) {
  33.             throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!');
  34.         }
  35.         if (!\is_array($data)) {
  36.             throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be "%s", "%s" given.'$typeget_debug_type($data)), $data, [Type::BUILTIN_TYPE_ARRAY], $context['deserialization_path'] ?? null);
  37.         }
  38.         if (!str_ends_with($type'[]')) {
  39.             throw new InvalidArgumentException('Unsupported class: '.$type);
  40.         }
  41.         $type substr($type0, -2);
  42.         $builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
  43.         foreach ($data as $key => $value) {
  44.             $subContext $context;
  45.             $subContext['deserialization_path'] = ($context['deserialization_path'] ?? false) ? sprintf('%s[%s]'$context['deserialization_path'], $key) : "[$key]";
  46.             if (null !== $builtinType && !('is_'.$builtinType)($key)) {
  47.                 throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the key "%s" must be "%s" ("%s" given).'$key$builtinTypeget_debug_type($key)), $key, [$builtinType], $subContext['deserialization_path'] ?? nulltrue);
  48.             }
  49.             $data[$key] = $this->denormalizer->denormalize($value$type$format$subContext);
  50.         }
  51.         return $data;
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function supportsDenormalization(mixed $datastring $typestring $format null, array $context = []): bool
  57.     {
  58.         if (null === $this->denormalizer) {
  59.             throw new BadMethodCallException(sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.'__METHOD__));
  60.         }
  61.         return str_ends_with($type'[]')
  62.             && $this->denormalizer->supportsDenormalization($datasubstr($type0, -2), $format$context);
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function hasCacheableSupportsMethod(): bool
  68.     {
  69.         return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod();
  70.     }
  71. }