|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Serializer\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Serializer\Exception\CircularReferenceException; |
|
6
|
|
|
use Bdf\Serializer\Metadata\PropertyMetadata; |
|
7
|
|
|
use Bdf\Serializer\Normalizer\NormalizerInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* NormalizationContext |
|
11
|
|
|
* |
|
12
|
|
|
* context used by normalization |
|
13
|
|
|
*/ |
|
14
|
|
|
class NormalizationContext extends Context |
|
15
|
|
|
{ |
|
16
|
|
|
//List of denormalization options |
|
17
|
|
|
const EXCLUDES = 'exclude'; |
|
18
|
|
|
const INCLUDES = 'include'; |
|
19
|
|
|
const GROUPS = 'groups'; |
|
20
|
|
|
const NULL = 'null'; |
|
21
|
|
|
const META_TYPE = 'include_type'; |
|
22
|
|
|
const VERSION = 'version'; |
|
23
|
|
|
const DATETIME_FORMAT = 'dateFormat'; |
|
24
|
|
|
const TIMEZONE = 'dateTimezone'; |
|
25
|
|
|
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit'; |
|
26
|
|
|
const REMOVE_DEFAULT_VALUE = 'remove_default_value'; |
|
27
|
|
|
const THROWS_ON_ACCESSOR_ERROR = 'throws_on_accessor_error'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The default options of this context |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $defaultOptions = [ |
|
35
|
|
|
/** |
|
36
|
|
|
* Properties to exclude from normalization |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
self::EXCLUDES => null, |
|
41
|
|
|
/** |
|
42
|
|
|
* Properties to include from normalization |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
self::INCLUDES => null, |
|
47
|
|
|
/** |
|
48
|
|
|
* Groups of properties to include |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
self::GROUPS => null, |
|
53
|
|
|
/** |
|
54
|
|
|
* Null value will be added if true |
|
55
|
|
|
* |
|
56
|
|
|
* @var boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
self::NULL => false, |
|
59
|
|
|
/** |
|
60
|
|
|
* Include the metadata '@type' in the payload |
|
61
|
|
|
* |
|
62
|
|
|
* @var boolean |
|
63
|
|
|
*/ |
|
64
|
|
|
self::META_TYPE => false, |
|
65
|
|
|
/** |
|
66
|
|
|
* The version of the object. |
|
67
|
|
|
* |
|
68
|
|
|
* @var string|null |
|
69
|
|
|
*/ |
|
70
|
|
|
self::VERSION => null, |
|
71
|
|
|
/** |
|
72
|
|
|
* The circular reference limit |
|
73
|
|
|
* |
|
74
|
|
|
* @var integer |
|
75
|
|
|
*/ |
|
76
|
|
|
self::CIRCULAR_REFERENCE_LIMIT => 1, |
|
77
|
|
|
/** |
|
78
|
|
|
* Default value will be removed if true |
|
79
|
|
|
* |
|
80
|
|
|
* @var boolean |
|
81
|
|
|
*/ |
|
82
|
|
|
self::REMOVE_DEFAULT_VALUE => false, |
|
83
|
|
|
/** |
|
84
|
|
|
* Throws exception if accessor has error |
|
85
|
|
|
* |
|
86
|
|
|
* @var boolean |
|
87
|
|
|
*/ |
|
88
|
|
|
self::THROWS_ON_ACCESSOR_ERROR => false, |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* The object reference for circular reference |
|
93
|
|
|
* |
|
94
|
|
|
* @var array |
|
95
|
|
|
*/ |
|
96
|
|
|
private $objectReferences = []; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
75 |
|
public function __construct(NormalizerInterface $normalizer, array $options = []) |
|
102
|
|
|
{ |
|
103
|
75 |
|
parent::__construct($normalizer, $this->defaultOptions); |
|
104
|
|
|
|
|
105
|
75 |
|
$this->prepareOptions($options); |
|
106
|
75 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the context groups |
|
110
|
|
|
* |
|
111
|
|
|
* @return null|array |
|
112
|
|
|
*/ |
|
113
|
56 |
|
public function groups(): ?array |
|
114
|
|
|
{ |
|
115
|
56 |
|
return $this->options[self::GROUPS]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the context exclude properties |
|
120
|
|
|
* |
|
121
|
|
|
* @return null|array |
|
122
|
|
|
*/ |
|
123
|
51 |
|
public function excludeProperties(): ?array |
|
124
|
|
|
{ |
|
125
|
51 |
|
return $this->options[self::EXCLUDES]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the context include properties |
|
130
|
|
|
* |
|
131
|
|
|
* @return null|array |
|
132
|
|
|
*/ |
|
133
|
49 |
|
public function includeProperties(): ?array |
|
134
|
|
|
{ |
|
135
|
49 |
|
return $this->options[self::INCLUDES]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Should the self::NULL value be included |
|
140
|
|
|
* |
|
141
|
|
|
* @return boolean |
|
142
|
|
|
*/ |
|
143
|
8 |
|
public function shouldAddNull(): bool |
|
144
|
|
|
{ |
|
145
|
8 |
|
return $this->options[self::NULL]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Should the default value of a property be removed |
|
150
|
|
|
* |
|
151
|
|
|
* @return boolean |
|
152
|
|
|
*/ |
|
153
|
38 |
|
public function removeDefaultValue(): bool |
|
154
|
|
|
{ |
|
155
|
38 |
|
return $this->options[self::REMOVE_DEFAULT_VALUE]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Serialize to the version |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
53 |
|
public function version(): ?string |
|
164
|
|
|
{ |
|
165
|
53 |
|
return $this->options[self::VERSION]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Should add metadata of type into the serialization |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
45 |
|
public function includeMetaType(): bool |
|
174
|
|
|
{ |
|
175
|
45 |
|
return $this->options[self::META_TYPE]; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Should add metadata of type into the serialization |
|
180
|
|
|
* |
|
181
|
|
|
* @return bool |
|
182
|
|
|
*/ |
|
183
|
|
|
public function throwsOnAccessorError(): bool |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->options[self::THROWS_ON_ACCESSOR_ERROR]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Skip the property by its value |
|
190
|
|
|
* |
|
191
|
|
|
* @param PropertyMetadata $property |
|
192
|
|
|
* @param mixed $value |
|
193
|
|
|
* |
|
194
|
|
|
* @return boolean Returns true if skipped |
|
195
|
|
|
*/ |
|
196
|
39 |
|
public function skipPropertyValue(PropertyMetadata $property, $value): bool |
|
197
|
|
|
{ |
|
198
|
39 |
|
if ($value === null) { |
|
199
|
4 |
|
return !$this->shouldAddNull(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// This does not remove the 'null' default value. |
|
203
|
38 |
|
if ($this->removeDefaultValue()) { |
|
204
|
2 |
|
return $property->defaultValue === $value; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
38 |
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Should the property be skipped |
|
212
|
|
|
* |
|
213
|
|
|
* @param PropertyMetadata $property |
|
214
|
|
|
* |
|
215
|
|
|
* @return boolean Returns true if skipped |
|
216
|
|
|
* |
|
217
|
|
|
* @todo Add custom filters |
|
218
|
|
|
*/ |
|
219
|
52 |
|
public function skipProperty(PropertyMetadata $property): bool |
|
220
|
|
|
{ |
|
221
|
52 |
|
$groups = $this->groups(); |
|
222
|
|
|
|
|
223
|
|
|
// A group has been set and the property is not in that group: we skip the property |
|
224
|
52 |
|
if ($groups !== null && !$property->hasGroups($groups)) { |
|
225
|
3 |
|
return true; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
51 |
|
$version = $this->version(); |
|
229
|
|
|
|
|
230
|
|
|
// A version has been set and the property does not match this version |
|
231
|
51 |
|
if ($version !== null && !$property->matchVersion($version)) { |
|
232
|
4 |
|
return true; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
49 |
|
return !$this->shouldNormalizeProperty($property->class, $property->name); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @param string $class |
|
240
|
|
|
* @param string $property |
|
241
|
|
|
* |
|
242
|
|
|
* @return bool |
|
243
|
|
|
*/ |
|
244
|
49 |
|
public function shouldNormalizeProperty(string $class, string $property): bool |
|
245
|
|
|
{ |
|
246
|
49 |
|
$path = "${class}::${property}"; |
|
247
|
|
|
|
|
248
|
49 |
|
$excludes = $this->excludeProperties(); |
|
249
|
|
|
|
|
250
|
49 |
|
if ($excludes !== null && (isset($excludes[$property]) || isset($excludes[$path]))) { |
|
251
|
4 |
|
return false; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
47 |
|
$includes = $this->includeProperties(); |
|
255
|
|
|
|
|
256
|
47 |
|
if ($includes !== null && !isset($includes[$property]) && !isset($includes[$path])) { |
|
257
|
3 |
|
return false; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
46 |
|
return true; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Detects if the configured circular reference limit is reached. |
|
265
|
|
|
* |
|
266
|
|
|
* @param object $object |
|
267
|
|
|
* |
|
268
|
|
|
* @return string The object hash |
|
269
|
|
|
* |
|
270
|
|
|
* @throws CircularReferenceException If circular reference found |
|
271
|
|
|
*/ |
|
272
|
46 |
|
public function assertNoCircularReference($object): string |
|
273
|
|
|
{ |
|
274
|
46 |
|
$objectHash = spl_object_hash($object); |
|
275
|
|
|
|
|
276
|
46 |
|
if (!isset($this->objectReferences[$objectHash])) { |
|
277
|
46 |
|
$this->objectReferences[$objectHash] = 1; |
|
278
|
|
|
|
|
279
|
46 |
|
return $objectHash; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
5 |
|
if ($this->objectReferences[$objectHash] < $this->options[self::CIRCULAR_REFERENCE_LIMIT]) { |
|
283
|
3 |
|
$this->objectReferences[$objectHash]++; |
|
284
|
|
|
|
|
285
|
3 |
|
return $objectHash; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
3 |
|
unset($this->objectReferences[$objectHash]); |
|
289
|
|
|
|
|
290
|
3 |
|
throw new CircularReferenceException( |
|
291
|
3 |
|
'A circular reference has been detected when serializing the object of class "'.get_class($object).'" (configured limit: '.$this->options[self::CIRCULAR_REFERENCE_LIMIT].')' |
|
292
|
|
|
); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Release the object |
|
297
|
|
|
* |
|
298
|
|
|
* @param string $objectHash |
|
299
|
|
|
*/ |
|
300
|
41 |
|
public function releaseReference(string $objectHash): void |
|
301
|
|
|
{ |
|
302
|
|
|
// Release the memory if depth is equal to 1 |
|
303
|
41 |
|
if ($this->objectReferences[$objectHash] === 1) { |
|
304
|
40 |
|
unset($this->objectReferences[$objectHash]); |
|
305
|
|
|
|
|
306
|
40 |
|
return; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
1 |
|
$this->objectReferences[$objectHash]--; |
|
310
|
1 |
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* {@inheritdoc} |
|
314
|
|
|
*/ |
|
315
|
75 |
|
protected function prepareOptions(array $options): void |
|
316
|
|
|
{ |
|
317
|
75 |
|
foreach ($options as $name => $value) { |
|
318
|
|
|
switch ($name) { |
|
319
|
42 |
|
case 'group': |
|
320
|
40 |
|
case self::GROUPS: |
|
321
|
7 |
|
$this->options[self::GROUPS] = (array)$value; |
|
322
|
7 |
|
break; |
|
323
|
|
|
|
|
324
|
38 |
|
case 'serializeNull': |
|
325
|
35 |
|
case 'serialize_null': |
|
326
|
35 |
|
case self::NULL: |
|
327
|
9 |
|
$this->options[self::NULL] = (bool)$value; |
|
328
|
9 |
|
break; |
|
329
|
|
|
|
|
330
|
34 |
|
case self::EXCLUDES: |
|
331
|
6 |
|
$this->options[self::EXCLUDES] = array_flip((array)$value); |
|
332
|
6 |
|
break; |
|
333
|
|
|
|
|
334
|
29 |
|
case self::INCLUDES: |
|
335
|
6 |
|
$this->options[self::INCLUDES] = array_flip((array)$value); |
|
336
|
6 |
|
break; |
|
337
|
|
|
|
|
338
|
|
|
default: |
|
339
|
24 |
|
$this->options[$name] = $value; |
|
340
|
42 |
|
break; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
} |