Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ArrayDenormalizer.php

class ArrayDenormalizer

Denormalizes arrays of objects.

@author Alexander M. Turek <me@derrabus.de>

@final

Hierarchy

  • class \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface uses \Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait

Expanded class hierarchy of ArrayDenormalizer

File

vendor/symfony/serializer/Normalizer/ArrayDenormalizer.php, line 30

Namespace

Symfony\Component\Serializer\Normalizer
View source
class ArrayDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface {
    use DenormalizerAwareTrait;
    public function getSupportedTypes(?string $format) : array {
        return [
            'object' => null,
            '*' => false,
        ];
    }
    
    /**
     * @throws NotNormalizableValueException
     */
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []) : array {
        if (!isset($this->denormalizer)) {
            throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!');
        }
        if (!\is_array($data)) {
            throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Data expected to be "%s", "%s" given.', $type, get_debug_type($data)), $data, [
                'array',
            ], $context['deserialization_path'] ?? null);
        }
        if (!str_ends_with($type, '[]')) {
            throw new InvalidArgumentException('Unsupported class: ' . $type);
        }
        $type = substr($type, 0, -2);
        $typeIdentifiers = [];
        if (null !== ($keyType = $context['key_type'] ?? null)) {
            if ($keyType instanceof Type) {
                // BC layer for type-info < 7.2
                if (method_exists(Type::class, 'getBaseType')) {
                    $typeIdentifiers = array_map(fn(Type $t): string => $t->getBaseType()
                        ->getTypeIdentifier()->value, $keyType instanceof UnionType ? $keyType->getTypes() : [
                        $keyType,
                    ]);
                }
                else {
                    
                    /** @var list<BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>> */
                    $keyTypes = $keyType instanceof UnionType ? $keyType->getTypes() : [
                        $keyType,
                    ];
                    $typeIdentifiers = array_map(fn(BuiltinType $t): string => $t->getTypeIdentifier()->value, $keyTypes);
                }
            }
            else {
                $typeIdentifiers = array_map(fn(LegacyType $t): string => $t->getBuiltinType(), \is_array($keyType) ? $keyType : [
                    $keyType,
                ]);
            }
        }
        foreach ($data as $key => $value) {
            $subContext = $context;
            $subContext['deserialization_path'] = $context['deserialization_path'] ?? false ? \sprintf('%s[%s]', $context['deserialization_path'], $key) : "[{$key}]";
            $this->validateKeyType($typeIdentifiers, $key, $subContext['deserialization_path']);
            $data[$key] = $this->denormalizer
                ->denormalize($value, $type, $format, $subContext);
        }
        return $data;
    }
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []) : bool {
        if (!isset($this->denormalizer)) {
            throw new BadMethodCallException(\sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__));
        }
        return str_ends_with($type, '[]') && $this->denormalizer
            ->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
    }
    
    /**
     * @param list<string> $typeIdentifiers
     */
    private function validateKeyType(array $typeIdentifiers, mixed $key, string $path) : void {
        if (!$typeIdentifiers) {
            return;
        }
        foreach ($typeIdentifiers as $typeIdentifier) {
            if (('is_' . $typeIdentifier)($key)) {
                return;
            }
        }
        throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, implode('", "', $typeIdentifiers), get_debug_type($key)), $key, $typeIdentifiers, $path, true);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ArrayDenormalizer::denormalize public function Overrides DenormalizerInterface::denormalize
ArrayDenormalizer::getSupportedTypes public function Returns the types potentially supported by this denormalizer. Overrides DenormalizerInterface::getSupportedTypes
ArrayDenormalizer::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer. Overrides DenormalizerInterface::supportsDenormalization
ArrayDenormalizer::validateKeyType private function
DenormalizerAwareTrait::$denormalizer protected property
DenormalizerAwareTrait::setDenormalizer public function
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS public constant

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal