-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
MetadataAwareNameConverter.php
144 lines (114 loc) · 4.98 KB
/
MetadataAwareNameConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\NameConverter;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
/**
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
*/
final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
{
private $metadataFactory;
/**
* @var NameConverterInterface|AdvancedNameConverterInterface|null
*/
private $fallbackNameConverter;
private static $normalizeCache = [];
private static $denormalizeCache = [];
private static $attributesMetadataCache = [];
public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null)
{
$this->metadataFactory = $metadataFactory;
$this->fallbackNameConverter = $fallbackNameConverter;
}
/**
* {@inheritdoc}
*/
public function normalize($propertyName, string $class = null, string $format = null, array $context = [])
{
if (null === $class) {
return $this->normalizeFallback($propertyName, $class, $format, $context);
}
if (!isset(self::$normalizeCache[$class][$propertyName])) {
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
}
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
}
/**
* {@inheritdoc}
*/
public function denormalize($propertyName, string $class = null, string $format = null, array $context = [])
{
if (null === $class) {
return $this->denormalizeFallback($propertyName, $class, $format, $context);
}
$cacheKey = $this->getCacheKey($class, $context);
if (!isset(self::$denormalizeCache[$cacheKey][$propertyName])) {
self::$denormalizeCache[$cacheKey][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
}
return self::$denormalizeCache[$cacheKey][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
}
private function getCacheValueForNormalization($propertyName, string $class)
{
if (!$this->metadataFactory->hasMetadataFor($class)) {
return null;
}
$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata();
if (!isset($attributesMetadata[$propertyName])) {
return null;
}
return $attributesMetadata[$propertyName]->getSerializedName() ?? null;
}
private function normalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
{
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
}
private function getCacheValueForDenormalization($propertyName, string $class, array $context)
{
$cacheKey = $this->getCacheKey($class, $context);
if (!isset(self::$attributesMetadataCache[$cacheKey])) {
self::$attributesMetadataCache[$cacheKey] = $this->getCacheValueForAttributesMetadata($class, $context);
}
return self::$attributesMetadataCache[$cacheKey][$propertyName] ?? null;
}
private function denormalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
{
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
}
private function getCacheValueForAttributesMetadata(string $class, array $context): array
{
if (!$this->metadataFactory->hasMetadataFor($class)) {
return [];
}
$classMetadata = $this->metadataFactory->getMetadataFor($class);
$cache = [];
foreach ($classMetadata->getAttributesMetadata() as $name => $metadata) {
if (null === $metadata->getSerializedName()) {
continue;
}
$groups = $metadata->getGroups();
if (!$groups && ($context[AbstractNormalizer::GROUPS] ?? [])) {
continue;
}
if ($groups && !array_intersect($groups, $context[AbstractNormalizer::GROUPS] ?? [])) {
continue;
}
$cache[$metadata->getSerializedName()] = $name;
}
return $cache;
}
private function getCacheKey(string $class, array $context): string
{
if (isset($context['cache_key'])) {
return $class.'-'.$context['cache_key'];
}
return $class.md5(serialize($context[AbstractNormalizer::GROUPS] ?? []));
}
}