1
|
|
|
<?php |
2
|
|
|
namespace W2w\Lib\Apie\Plugins\Core\Normalizers; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
5
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
6
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
7
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
8
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
9
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Normalizer for the symfony serializer to enable/disable a normalizer by context. This can be done globally with |
13
|
|
|
* ContextualNormalizer::enableNormalizer and ContextualNormalizer::disableNormalizer or by providing it in the |
14
|
|
|
* context of the serializer. |
15
|
|
|
*/ |
16
|
|
|
class ContextualNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, NormalizerAwareInterface, DenormalizerAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var boolean[] |
20
|
|
|
*/ |
21
|
|
|
private static $globalDisabledNormalizers = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var boolean[] |
25
|
|
|
*/ |
26
|
|
|
private static $globalDisabledDenormalizers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var iterable<NormalizerInterface|DenormalizerInterface> |
30
|
|
|
*/ |
31
|
|
|
private $normalizers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param iterable<NormalizerInterface|DenormalizerInterface> $normalizers |
35
|
|
|
*/ |
36
|
|
|
public function __construct(iterable $normalizers) |
37
|
|
|
{ |
38
|
|
|
$this->normalizers = $normalizers; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $object |
43
|
|
|
* @param string|null $format |
44
|
|
|
* @param array $context |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function normalize($object, $format = null, array $context = []) |
48
|
|
|
{ |
49
|
|
|
foreach ($this->normalizers as $normalizer) { |
50
|
|
|
if ($normalizer instanceof NormalizerInterface |
51
|
|
|
&& $this->isNormalizerEnabled($normalizer) |
52
|
|
|
&& $normalizer->supportsNormalization($object, $format)) { |
53
|
|
|
return $normalizer->normalize($object, $format, $context); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
// @codeCoverageIgnoreStart |
57
|
|
|
} |
58
|
|
|
// @codeCoverageIgnoreEnd |
59
|
|
|
/** |
60
|
|
|
* @param mixed $data |
61
|
|
|
* @param string|null $format |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function supportsNormalization($data, $format = null) |
65
|
|
|
{ |
66
|
|
|
foreach ($this->normalizers as $normalizer) { |
67
|
|
|
if ($normalizer instanceof NormalizerInterface |
68
|
|
|
&& $this->isNormalizerEnabled($normalizer) |
69
|
|
|
&& $normalizer->supportsNormalization($data, $format)) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $data |
79
|
|
|
* @param string $class |
80
|
|
|
* @param string|null $format |
81
|
|
|
* @param array $context |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
85
|
|
|
{ |
86
|
|
|
foreach ($this->normalizers as $denormalizer) { |
87
|
|
|
if ($denormalizer instanceof DenormalizerInterface |
88
|
|
|
&& $this->isDenormalizerEnabled($denormalizer) |
89
|
|
|
&& $denormalizer->supportsDenormalization($data, $class, $format)) { |
90
|
|
|
return $denormalizer->denormalize($data, $class, $format, $context); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
// @codeCoverageIgnoreStart |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// @codeCoverageIgnoreEnd |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param mixed $data |
100
|
|
|
* @param string $type |
101
|
|
|
* @param string|null $format |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
105
|
|
|
{ |
106
|
|
|
foreach ($this->normalizers as $denormalizer) { |
107
|
|
|
if ($denormalizer instanceof DenormalizerInterface |
108
|
|
|
&& $this->isDenormalizerEnabled($denormalizer) |
109
|
|
|
&& $denormalizer->supportsDenormalization($data, $type, $format)) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Mark a normalizer as disabled. |
119
|
|
|
* |
120
|
|
|
* @param string $className |
121
|
|
|
*/ |
122
|
|
|
public static function disableNormalizer(string $className) |
123
|
|
|
{ |
124
|
|
|
self::$globalDisabledNormalizers[$className] = true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Mark a normalizer as enabled. |
129
|
|
|
* |
130
|
|
|
* @param string $className |
131
|
|
|
*/ |
132
|
|
|
public static function enableNormalizer(string $className) |
133
|
|
|
{ |
134
|
|
|
unset(self::$globalDisabledNormalizers[$className]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Mark a denormalizer as disabled. |
139
|
|
|
* |
140
|
|
|
* @param string $className |
141
|
|
|
*/ |
142
|
|
|
public static function disableDenormalizer(string $className) |
143
|
|
|
{ |
144
|
|
|
self::$globalDisabledDenormalizers[$className] = true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Mark a denormalizer as enabled. |
149
|
|
|
* |
150
|
|
|
* @param string $className |
151
|
|
|
*/ |
152
|
|
|
public static function enableDenormalizer(string $className) |
153
|
|
|
{ |
154
|
|
|
unset(self::$globalDisabledDenormalizers[$className]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns true if the normalizer is enabled. |
159
|
|
|
* |
160
|
|
|
* @param NormalizerInterface $normalizer |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
private function isNormalizerEnabled(NormalizerInterface $normalizer): bool |
164
|
|
|
{ |
165
|
|
|
$className = get_class($normalizer); |
166
|
|
|
|
167
|
|
|
return empty(self::$globalDisabledNormalizers[$className]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns true if the denormalizer is enabled. |
172
|
|
|
* |
173
|
|
|
* @param DenormalizerInterface $normalizer |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
private function isDenormalizerEnabled(DenormalizerInterface $normalizer): bool |
177
|
|
|
{ |
178
|
|
|
$className = get_class($normalizer); |
179
|
|
|
|
180
|
|
|
return empty(self::$globalDisabledDenormalizers[$className]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Sets the owning Serializer object to the normalizers we have. |
185
|
|
|
*/ |
186
|
|
|
public function setSerializer(SerializerInterface $serializer) |
187
|
|
|
{ |
188
|
|
|
foreach ($this->normalizers as $normalizer) { |
189
|
|
|
if ($normalizer instanceof SerializerAwareInterface) { |
190
|
|
|
$normalizer->setSerializer($serializer); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets the owning Denormalizer object. |
197
|
|
|
*/ |
198
|
|
|
public function setDenormalizer(DenormalizerInterface $denormalizer) |
199
|
|
|
{ |
200
|
|
|
foreach ($this->normalizers as $normalizer) { |
201
|
|
|
if ($normalizer instanceof DenormalizerAwareInterface) { |
202
|
|
|
$normalizer->setDenormalizer($denormalizer); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Sets the owning Normalizer object. |
209
|
|
|
*/ |
210
|
|
|
public function setNormalizer(NormalizerInterface $owningNormalizer) |
211
|
|
|
{ |
212
|
|
|
foreach ($this->normalizers as $normalizer) { |
213
|
|
|
if ($normalizer instanceof NormalizerAwareInterface) { |
214
|
|
|
$normalizer->setNormalizer($owningNormalizer); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|