| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\NameConverter; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Exception\LogicException; |
| 15 |
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
| 16 |
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
final class MetadataAwareNameConverter implements AdvancedNameConverterInterface |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private static array $normalizeCache = []; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private static array $denormalizeCache = []; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private static array $attributesMetadataCache = []; |
| 37 |
|
| 38 |
public function __construct( |
| 39 |
private readonly ClassMetadataFactoryInterface $metadataFactory, |
| 40 |
private readonly ?NameConverterInterface $fallbackNameConverter = null, |
| 41 |
) { |
| 42 |
} |
| 43 |
|
| 44 |
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string |
| 45 |
{ |
| 46 |
if (null === $class) { |
| 47 |
return $this->normalizeFallback($propertyName, $class, $format, $context); |
| 48 |
} |
| 49 |
|
| 50 |
if (!\array_key_exists($class, self::$normalizeCache) || !\array_key_exists($propertyName, self::$normalizeCache[$class])) { |
| 51 |
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class); |
| 52 |
} |
| 53 |
|
| 54 |
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context); |
| 55 |
} |
| 56 |
|
| 57 |
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string |
| 58 |
{ |
| 59 |
if (null === $class) { |
| 60 |
return $this->denormalizeFallback($propertyName, $class, $format, $context); |
| 61 |
} |
| 62 |
|
| 63 |
$cacheKey = $this->getCacheKey($class, $context); |
| 64 |
if (!\array_key_exists($cacheKey, self::$denormalizeCache) || !\array_key_exists($propertyName, self::$denormalizeCache[$cacheKey])) { |
| 65 |
self::$denormalizeCache[$cacheKey][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context); |
| 66 |
} |
| 67 |
|
| 68 |
return self::$denormalizeCache[$cacheKey][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context); |
| 69 |
} |
| 70 |
|
| 71 |
private function getCacheValueForNormalization(string $propertyName, string $class): ?string |
| 72 |
{ |
| 73 |
if (!$this->metadataFactory->hasMetadataFor($class)) { |
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata(); |
| 78 |
if (!\array_key_exists($propertyName, $attributesMetadata)) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
if (null !== $attributesMetadata[$propertyName]->getSerializedName() && null !== $attributesMetadata[$propertyName]->getSerializedPath()) { |
| 83 |
throw new LogicException(\sprintf('Found SerializedName and SerializedPath attributes on property "%s" of class "%s".', $propertyName, $class)); |
| 84 |
} |
| 85 |
|
| 86 |
return $attributesMetadata[$propertyName]->getSerializedName() ?? null; |
| 87 |
} |
| 88 |
|
| 89 |
private function normalizeFallback(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string |
| 90 |
{ |
| 91 |
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName; |
| 92 |
} |
| 93 |
|
| 94 |
private function getCacheValueForDenormalization(string $propertyName, string $class, array $context): ?string |
| 95 |
{ |
| 96 |
$cacheKey = $this->getCacheKey($class, $context); |
| 97 |
if (!\array_key_exists($cacheKey, self::$attributesMetadataCache)) { |
| 98 |
self::$attributesMetadataCache[$cacheKey] = $this->getCacheValueForAttributesMetadata($class, $context); |
| 99 |
} |
| 100 |
|
| 101 |
return self::$attributesMetadataCache[$cacheKey][$propertyName] ?? null; |
| 102 |
} |
| 103 |
|
| 104 |
private function denormalizeFallback(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string |
| 105 |
{ |
| 106 |
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private function getCacheValueForAttributesMetadata(string $class, array $context): array |
| 113 |
{ |
| 114 |
if (!$this->metadataFactory->hasMetadataFor($class)) { |
| 115 |
return []; |
| 116 |
} |
| 117 |
|
| 118 |
$classMetadata = $this->metadataFactory->getMetadataFor($class); |
| 119 |
|
| 120 |
$cache = []; |
| 121 |
foreach ($classMetadata->getAttributesMetadata() as $name => $metadata) { |
| 122 |
if (null === $metadata->getSerializedName()) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
if (null !== $metadata->getSerializedName() && null !== $metadata->getSerializedPath()) { |
| 127 |
throw new LogicException(\sprintf('Found SerializedName and SerializedPath attributes on property "%s" of class "%s".', $name, $class)); |
| 128 |
} |
| 129 |
|
| 130 |
$metadataGroups = $metadata->getGroups(); |
| 131 |
$contextGroups = (array) ($context[AbstractNormalizer::GROUPS] ?? []); |
| 132 |
|
| 133 |
if ($contextGroups && !$metadataGroups) { |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
if ($metadataGroups && !array_intersect($metadataGroups, $contextGroups) && !\in_array('*', $contextGroups, true)) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
$cache[$metadata->getSerializedName()] = $name; |
| 142 |
} |
| 143 |
|
| 144 |
return $cache; |
| 145 |
} |
| 146 |
|
| 147 |
private function getCacheKey(string $class, array $context): string |
| 148 |
{ |
| 149 |
if (isset($context['cache_key'])) { |
| 150 |
return $class.'-'.$context['cache_key']; |
| 151 |
} |
| 152 |
|
| 153 |
return $class.hash('xxh128', serialize($context[AbstractNormalizer::GROUPS] ?? [])); |
| 154 |
} |
| 155 |
} |