1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2016 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\Serializer; |
20
|
|
|
|
21
|
|
|
use JMS\Serializer\Construction\ObjectConstructorInterface; |
22
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
24
|
|
|
use JMS\Serializer\EventDispatcher\PreDeserializeEvent; |
25
|
|
|
use JMS\Serializer\EventDispatcher\PreSerializeEvent; |
26
|
|
|
use JMS\Serializer\Exception\ExpressionLanguageRequiredException; |
27
|
|
|
use JMS\Serializer\Exception\InvalidArgumentException; |
28
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
29
|
|
|
use JMS\Serializer\Exclusion\ExpressionLanguageExclusionStrategy; |
30
|
|
|
use JMS\Serializer\Expression\ExpressionEvaluatorInterface; |
31
|
|
|
use JMS\Serializer\Handler\HandlerRegistryInterface; |
32
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
33
|
|
|
use Metadata\MetadataFactoryInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Handles traversal along the object graph. |
37
|
|
|
* |
38
|
|
|
* This class handles traversal along the graph, and calls different methods |
39
|
|
|
* on visitors, or custom handlers to process its nodes. |
40
|
|
|
* |
41
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
final class GraphNavigator |
44
|
|
|
{ |
45
|
|
|
const DIRECTION_SERIALIZATION = 1; |
46
|
|
|
const DIRECTION_DESERIALIZATION = 2; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ExpressionLanguageExclusionStrategy |
50
|
|
|
*/ |
51
|
|
|
private $expressionExclusionStrategy; |
52
|
|
|
|
53
|
|
|
private $dispatcher; |
54
|
|
|
private $metadataFactory; |
55
|
|
|
private $handlerRegistry; |
56
|
|
|
private $objectConstructor; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Parses a direction string to one of the direction constants. |
60
|
|
|
* |
61
|
|
|
* @param string $dirStr |
62
|
|
|
* |
63
|
|
|
* @return integer |
64
|
|
|
*/ |
65
|
6 |
|
public static function parseDirection($dirStr) |
66
|
|
|
{ |
67
|
6 |
|
switch (strtolower($dirStr)) { |
68
|
6 |
|
case 'serialization': |
69
|
6 |
|
return self::DIRECTION_SERIALIZATION; |
70
|
|
|
|
71
|
3 |
|
case 'deserialization': |
72
|
3 |
|
return self::DIRECTION_DESERIALIZATION; |
73
|
|
|
|
74
|
|
|
default: |
75
|
|
|
throw new InvalidArgumentException(sprintf('The direction "%s" does not exist.', $dirStr)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
400 |
|
public function __construct( |
80
|
|
|
MetadataFactoryInterface $metadataFactory, |
81
|
|
|
HandlerRegistryInterface $handlerRegistry, |
82
|
|
|
ObjectConstructorInterface $objectConstructor, |
83
|
|
|
EventDispatcherInterface $dispatcher = null, |
84
|
|
|
ExpressionEvaluatorInterface $expressionEvaluator = null |
85
|
|
|
) |
86
|
|
|
{ |
87
|
400 |
|
$this->dispatcher = $dispatcher; |
88
|
400 |
|
$this->metadataFactory = $metadataFactory; |
89
|
400 |
|
$this->handlerRegistry = $handlerRegistry; |
90
|
400 |
|
$this->objectConstructor = $objectConstructor; |
91
|
400 |
|
if ($expressionEvaluator) { |
92
|
32 |
|
$this->expressionExclusionStrategy = new ExpressionLanguageExclusionStrategy($expressionEvaluator); |
93
|
32 |
|
} |
94
|
400 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Called for each node of the graph that is being traversed. |
98
|
|
|
* |
99
|
|
|
* @param mixed $data the data depends on the direction, and type of visitor |
100
|
|
|
* @param null|array $type array has the format ["name" => string, "params" => array] |
101
|
|
|
* @param Context $context |
102
|
|
|
* @return mixed the return value depends on the direction, and type of visitor |
103
|
|
|
*/ |
104
|
373 |
|
public function accept($data, array $type = null, Context $context) |
105
|
|
|
{ |
106
|
373 |
|
$visitor = $context->getVisitor(); |
107
|
|
|
|
108
|
|
|
// If the type was not given, we infer the most specific type from the |
109
|
|
|
// input data in serialization mode. |
110
|
373 |
|
if (null === $type) { |
111
|
323 |
|
if ($context instanceof DeserializationContext) { |
112
|
|
|
throw new RuntimeException('The type must be given for all properties when deserializing.'); |
113
|
8 |
|
} |
114
|
|
|
|
115
|
323 |
|
$typeName = gettype($data); |
116
|
323 |
|
if ('object' === $typeName) { |
117
|
234 |
|
$typeName = get_class($data); |
118
|
234 |
|
} |
119
|
|
|
|
120
|
323 |
|
$type = array('name' => $typeName, 'params' => array()); |
121
|
323 |
|
} |
122
|
|
|
// If the data is null, we have to force the type to null regardless of the input in order to |
123
|
|
|
// guarantee correct handling of null values, and not have any internal auto-casting behavior. |
124
|
266 |
|
else if ($context instanceof SerializationContext && null === $data) { |
125
|
20 |
|
$type = array('name' => 'NULL', 'params' => array()); |
126
|
20 |
|
} |
127
|
|
|
// Sometimes data can convey null but is not of a null type. |
128
|
|
|
// Visitors can have the power to add this custom null evaluation |
129
|
373 |
|
if ($visitor instanceof NullAwareVisitorInterface && $visitor->isNull($data) === true) { |
130
|
7 |
|
$type = array('name' => 'NULL', 'params' => array()); |
131
|
7 |
|
} |
132
|
|
|
|
133
|
373 |
|
switch ($type['name']) { |
134
|
373 |
|
case 'NULL': |
135
|
48 |
|
return $visitor->visitNull($data, $type, $context); |
136
|
|
|
|
137
|
360 |
|
case 'string': |
138
|
212 |
|
return $visitor->visitString($data, $type, $context); |
139
|
|
|
|
140
|
354 |
|
case 'int': |
141
|
354 |
|
case 'integer': |
142
|
65 |
|
return $visitor->visitInteger($data, $type, $context); |
143
|
|
|
|
144
|
349 |
|
case 'bool': |
145
|
349 |
|
case 'boolean': |
146
|
22 |
|
return $visitor->visitBoolean($data, $type, $context); |
147
|
|
|
|
148
|
338 |
|
case 'double': |
149
|
338 |
|
case 'float': |
150
|
34 |
|
return $visitor->visitDouble($data, $type, $context); |
151
|
|
|
|
152
|
323 |
|
case 'array': |
153
|
140 |
|
return $visitor->visitArray($data, $type, $context); |
154
|
|
|
|
155
|
257 |
|
case 'resource': |
156
|
1 |
|
$msg = 'Resources are not supported in serialized data.'; |
157
|
1 |
|
if ($context instanceof SerializationContext && null !== $path = $context->getPath()) { |
158
|
|
|
$msg .= ' Path: ' . $path; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
throw new RuntimeException($msg); |
162
|
|
|
|
163
|
256 |
|
default: |
164
|
|
|
// TODO: The rest of this method needs some refactoring. |
165
|
256 |
|
if ($context instanceof SerializationContext) { |
166
|
238 |
|
if (null !== $data) { |
167
|
238 |
|
if ($context->isVisiting($data)) { |
168
|
3 |
|
return null; |
169
|
|
|
} |
170
|
238 |
|
$context->startVisiting($data); |
171
|
238 |
|
} |
172
|
|
|
|
173
|
|
|
// If we're serializing a polymorphic type, then we'll be interested in the |
174
|
|
|
// metadata for the actual type of the object, not the base class. |
175
|
238 |
|
if (class_exists($type['name'], false) || interface_exists($type['name'], false)) { |
176
|
238 |
|
if (is_subclass_of($data, $type['name'], false)) { |
|
|
|
|
177
|
6 |
|
$type = array('name' => get_class($data), 'params' => array()); |
178
|
6 |
|
} |
179
|
238 |
|
} |
180
|
256 |
|
} elseif ($context instanceof DeserializationContext) { |
181
|
72 |
|
$context->increaseDepth(); |
182
|
72 |
|
} |
183
|
|
|
|
184
|
|
|
// Trigger pre-serialization callbacks, and listeners if they exist. |
185
|
|
|
// Dispatch pre-serialization event before handling data to have ability change type in listener |
186
|
256 |
|
if ($context instanceof SerializationContext) { |
187
|
238 |
|
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $context->getFormat())) { |
188
|
233 |
|
$this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $context->getFormat(), $event = new PreSerializeEvent($context, $data, $type)); |
189
|
233 |
|
$type = $event->getType(); |
190
|
233 |
|
} |
191
|
256 |
|
} elseif ($context instanceof DeserializationContext) { |
192
|
72 |
|
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_deserialize', $type['name'], $context->getFormat())) { |
193
|
|
|
$this->dispatcher->dispatch('serializer.pre_deserialize', $type['name'], $context->getFormat(), $event = new PreDeserializeEvent($context, $data, $type)); |
194
|
|
|
$type = $event->getType(); |
195
|
|
|
$data = $event->getData(); |
196
|
|
|
} |
197
|
72 |
|
} |
198
|
|
|
|
199
|
|
|
// First, try whether a custom handler exists for the given type. This is done |
200
|
|
|
// before loading metadata because the type name might not be a class, but |
201
|
|
|
// could also simply be an artifical type. |
202
|
256 |
|
if (null !== $handler = $this->handlerRegistry->getHandler($context->getDirection(), $type['name'], $context->getFormat())) { |
203
|
62 |
|
$rs = call_user_func($handler, $visitor, $data, $type, $context); |
204
|
62 |
|
$this->leaveScope($context, $data); |
205
|
|
|
|
206
|
62 |
|
return $rs; |
207
|
|
|
} |
208
|
|
|
|
209
|
228 |
|
$exclusionStrategy = $context->getExclusionStrategy(); |
210
|
|
|
|
211
|
|
|
/** @var $metadata ClassMetadata */ |
212
|
228 |
|
$metadata = $this->metadataFactory->getMetadataForClass($type['name']); |
213
|
|
|
|
214
|
225 |
|
if ($metadata->usingExpression && !$this->expressionExclusionStrategy) { |
215
|
3 |
|
throw new ExpressionLanguageRequiredException("To use conditional exclude/expose in {$metadata->name} you must configure the expression language."); |
216
|
|
|
} |
217
|
|
|
|
218
|
222 |
|
if ($context instanceof DeserializationContext && !empty($metadata->discriminatorMap) && $type['name'] === $metadata->discriminatorBaseClass) { |
219
|
13 |
|
$metadata = $this->resolveMetadata($data, $metadata); |
220
|
11 |
|
} |
221
|
|
|
|
222
|
220 |
|
if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($metadata, $context)) { |
|
|
|
|
223
|
6 |
|
$this->leaveScope($context, $data); |
224
|
|
|
|
225
|
6 |
|
return null; |
226
|
|
|
} |
227
|
|
|
|
228
|
220 |
|
$context->pushClassMetadata($metadata); |
|
|
|
|
229
|
|
|
|
230
|
220 |
|
if ($context instanceof SerializationContext) { |
231
|
205 |
|
foreach ($metadata->preSerializeMethods as $method) { |
232
|
3 |
|
$method->invoke($data); |
233
|
205 |
|
} |
234
|
205 |
|
} |
235
|
|
|
|
236
|
220 |
|
$object = $data; |
237
|
220 |
|
if ($context instanceof DeserializationContext) { |
238
|
61 |
|
$object = $this->objectConstructor->construct($visitor, $metadata, $data, $type, $context); |
|
|
|
|
239
|
61 |
|
} |
240
|
|
|
|
241
|
220 |
|
if (isset($metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()])) { |
242
|
3 |
|
$rs = $object->{$metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()]}( |
243
|
3 |
|
$visitor, |
244
|
3 |
|
$context instanceof SerializationContext ? null : $data, |
245
|
|
|
$context |
246
|
3 |
|
); |
247
|
3 |
|
$this->afterVisitingObject($metadata, $object, $type, $context); |
|
|
|
|
248
|
|
|
|
249
|
3 |
|
return $context instanceof SerializationContext ? $rs : $object; |
250
|
|
|
} |
251
|
|
|
|
252
|
217 |
|
$visitor->startVisitingObject($metadata, $object, $type, $context); |
|
|
|
|
253
|
217 |
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
254
|
215 |
|
if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) { |
255
|
17 |
|
continue; |
256
|
|
|
} |
257
|
|
|
|
258
|
215 |
|
if (null !== $this->expressionExclusionStrategy && $this->expressionExclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) { |
259
|
23 |
|
continue; |
260
|
|
|
} |
261
|
|
|
|
262
|
215 |
|
if ($context instanceof DeserializationContext && $propertyMetadata->readOnly) { |
263
|
17 |
|
continue; |
264
|
|
|
} |
265
|
|
|
|
266
|
215 |
|
$context->pushPropertyMetadata($propertyMetadata); |
267
|
215 |
|
$visitor->visitProperty($propertyMetadata, $data, $context); |
268
|
211 |
|
$context->popPropertyMetadata(); |
269
|
213 |
|
} |
270
|
|
|
|
271
|
211 |
|
if ($context instanceof SerializationContext) { |
272
|
197 |
|
$this->afterVisitingObject($metadata, $data, $type, $context); |
|
|
|
|
273
|
|
|
|
274
|
197 |
|
return $visitor->endVisitingObject($metadata, $data, $type, $context); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
61 |
|
$rs = $visitor->endVisitingObject($metadata, $data, $type, $context); |
|
|
|
|
278
|
61 |
|
$this->afterVisitingObject($metadata, $rs, $type, $context); |
|
|
|
|
279
|
|
|
|
280
|
61 |
|
return $rs; |
281
|
256 |
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
13 |
|
private function resolveMetadata($data, ClassMetadata $metadata) |
285
|
|
|
{ |
286
|
13 |
|
switch (true) { |
287
|
13 |
|
case is_array($data) && isset($data[$metadata->discriminatorFieldName]): |
288
|
5 |
|
$typeValue = (string)$data[$metadata->discriminatorFieldName]; |
289
|
5 |
|
break; |
290
|
|
|
|
291
|
|
|
// Check XML attribute for discriminatorFieldName |
292
|
8 |
|
case is_object($data) && $metadata->xmlDiscriminatorAttribute && isset($data[$metadata->discriminatorFieldName]): |
293
|
1 |
|
$typeValue = (string)$data[$metadata->discriminatorFieldName]; |
294
|
1 |
|
break; |
295
|
|
|
|
296
|
|
|
// Check XML element with namespace for discriminatorFieldName |
297
|
7 |
|
case is_object($data) && !$metadata->xmlDiscriminatorAttribute && null !== $metadata->xmlDiscriminatorNamespace && isset($data->children($metadata->xmlDiscriminatorNamespace)->{$metadata->discriminatorFieldName}): |
298
|
1 |
|
$typeValue = (string)$data->children($metadata->xmlDiscriminatorNamespace)->{$metadata->discriminatorFieldName}; |
299
|
1 |
|
break; |
300
|
|
|
|
301
|
|
|
// Check XML element for discriminatorFieldName |
302
|
6 |
|
case is_object($data) && isset($data->{$metadata->discriminatorFieldName}): |
303
|
4 |
|
$typeValue = (string)$data->{$metadata->discriminatorFieldName}; |
304
|
4 |
|
break; |
305
|
|
|
|
306
|
2 |
|
default: |
307
|
2 |
|
throw new \LogicException(sprintf( |
308
|
2 |
|
'The discriminator field name "%s" for base-class "%s" was not found in input data.', |
309
|
2 |
|
$metadata->discriminatorFieldName, |
310
|
2 |
|
$metadata->name |
311
|
2 |
|
)); |
312
|
2 |
|
} |
313
|
|
|
|
314
|
11 |
|
if (!isset($metadata->discriminatorMap[$typeValue])) { |
315
|
|
|
throw new \LogicException(sprintf( |
316
|
|
|
'The type value "%s" does not exist in the discriminator map of class "%s". Available types: %s', |
317
|
|
|
$typeValue, |
318
|
|
|
$metadata->name, |
319
|
|
|
implode(', ', array_keys($metadata->discriminatorMap)) |
320
|
|
|
)); |
321
|
|
|
} |
322
|
|
|
|
323
|
11 |
|
return $this->metadataFactory->getMetadataForClass($metadata->discriminatorMap[$typeValue]); |
324
|
|
|
} |
325
|
|
|
|
326
|
242 |
|
private function leaveScope(Context $context, $data) |
327
|
|
|
{ |
328
|
242 |
|
if ($context instanceof SerializationContext) { |
329
|
227 |
|
$context->stopVisiting($data); |
330
|
242 |
|
} elseif ($context instanceof DeserializationContext) { |
331
|
69 |
|
$context->decreaseDepth(); |
332
|
69 |
|
} |
333
|
242 |
|
} |
334
|
|
|
|
335
|
214 |
|
private function afterVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
336
|
|
|
{ |
337
|
214 |
|
$this->leaveScope($context, $object); |
338
|
214 |
|
$context->popClassMetadata(); |
339
|
|
|
|
340
|
214 |
|
if ($context instanceof SerializationContext) { |
341
|
200 |
|
foreach ($metadata->postSerializeMethods as $method) { |
342
|
3 |
|
$method->invoke($object); |
343
|
200 |
|
} |
344
|
|
|
|
345
|
200 |
|
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.post_serialize', $metadata->name, $context->getFormat())) { |
346
|
2 |
|
$this->dispatcher->dispatch('serializer.post_serialize', $metadata->name, $context->getFormat(), new ObjectEvent($context, $object, $type)); |
347
|
2 |
|
} |
348
|
|
|
|
349
|
200 |
|
return; |
350
|
|
|
} |
351
|
|
|
|
352
|
63 |
|
foreach ($metadata->postDeserializeMethods as $method) { |
353
|
4 |
|
$method->invoke($object); |
354
|
63 |
|
} |
355
|
|
|
|
356
|
63 |
|
if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.post_deserialize', $metadata->name, $context->getFormat())) { |
357
|
1 |
|
$this->dispatcher->dispatch('serializer.post_deserialize', $metadata->name, $context->getFormat(), new ObjectEvent($context, $object, $type)); |
358
|
1 |
|
} |
359
|
63 |
|
} |
360
|
|
|
} |
361
|
|
|
|