| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Normalizer; |
| 13 |
|
| 14 |
use Symfony\Component\PropertyInfo\Type as LegacyType; |
| 15 |
use Symfony\Component\Serializer\Exception\BadMethodCallException; |
| 16 |
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 17 |
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 18 |
use Symfony\Component\TypeInfo\Type; |
| 19 |
use Symfony\Component\TypeInfo\Type\BuiltinType; |
| 20 |
use Symfony\Component\TypeInfo\Type\CollectionType; |
| 21 |
use Symfony\Component\TypeInfo\Type\UnionType; |
| 22 |
use Symfony\Component\TypeInfo\TypeIdentifier; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class ArrayDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface |
| 32 |
{ |
| 33 |
use DenormalizerAwareTrait; |
| 34 |
|
| 35 |
public function getSupportedTypes(?string $format): array |
| 36 |
{ |
| 37 |
return ['object' => null, '*' => false]; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): array |
| 44 |
{ |
| 45 |
if (!isset($this->denormalizer)) { |
| 46 |
throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!'); |
| 47 |
} |
| 48 |
if (!\is_array($data)) { |
| 49 |
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Data expected to be "%s", "%s" given.', $type, get_debug_type($data)), $data, ['array'], $context['deserialization_path'] ?? null); |
| 50 |
} |
| 51 |
if (!str_ends_with($type, '[]')) { |
| 52 |
throw new InvalidArgumentException('Unsupported class: '.$type); |
| 53 |
} |
| 54 |
|
| 55 |
$type = substr($type, 0, -2); |
| 56 |
|
| 57 |
$typeIdentifiers = []; |
| 58 |
if (null !== $keyType = ($context['key_type'] ?? null)) { |
| 59 |
if ($keyType instanceof Type) { |
| 60 |
|
| 61 |
if (method_exists(Type::class, 'getBaseType')) { |
| 62 |
$typeIdentifiers = array_map(static fn (Type $t): string => $t->getBaseType()->getTypeIdentifier()->value, $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType]); |
| 63 |
} else { |
| 64 |
|
| 65 |
$keyTypes = $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType]; |
| 66 |
|
| 67 |
$typeIdentifiers = array_map(static fn (BuiltinType $t): string => $t->getTypeIdentifier()->value, $keyTypes); |
| 68 |
} |
| 69 |
} else { |
| 70 |
$typeIdentifiers = array_map(static fn (LegacyType $t): string => $t->getBuiltinType(), \is_array($keyType) ? $keyType : [$keyType]); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$valueType = $context['value_type'] ?? null; |
| 75 |
if ($valueType instanceof CollectionType) { |
| 76 |
$context['key_type'] = $valueType->getCollectionKeyType(); |
| 77 |
$context['value_type'] = $valueType->getCollectionValueType(); |
| 78 |
} elseif ($valueType instanceof LegacyType && $valueType->isCollection()) { |
| 79 |
if ($collectionKeyTypes = $valueType->getCollectionKeyTypes()) { |
| 80 |
$context['key_type'] = \count($collectionKeyTypes) > 1 ? $collectionKeyTypes : $collectionKeyTypes[0]; |
| 81 |
} |
| 82 |
|
| 83 |
if ($collectionValueTypes = $valueType->getCollectionValueTypes()) { |
| 84 |
$context['value_type'] = $collectionValueTypes[0]; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
foreach ($data as $key => $value) { |
| 89 |
$subContext = $context; |
| 90 |
$subContext['deserialization_path'] = ($context['deserialization_path'] ?? false) ? \sprintf('%s[%s]', $context['deserialization_path'], $key) : "[$key]"; |
| 91 |
|
| 92 |
$this->validateKeyType($typeIdentifiers, $key, $subContext['deserialization_path']); |
| 93 |
|
| 94 |
$data[$key] = $this->denormalizer->denormalize($value, $type, $format, $subContext); |
| 95 |
} |
| 96 |
|
| 97 |
return $data; |
| 98 |
} |
| 99 |
|
| 100 |
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 101 |
{ |
| 102 |
if (!isset($this->denormalizer)) { |
| 103 |
throw new BadMethodCallException(\sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__)); |
| 104 |
} |
| 105 |
|
| 106 |
return str_ends_with($type, '[]') |
| 107 |
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private function validateKeyType(array $typeIdentifiers, mixed $key, string $path): void |
| 114 |
{ |
| 115 |
if (!$typeIdentifiers) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
foreach ($typeIdentifiers as $typeIdentifier) { |
| 120 |
if (('is_'.$typeIdentifier)($key)) { |
| 121 |
return; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
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); |
| 126 |
} |
| 127 |
} |