1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\GraphNavigator; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Accessor\AccessorStrategyInterface; |
8
|
|
|
use JMS\Serializer\Context; |
9
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
10
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
12
|
|
|
use JMS\Serializer\EventDispatcher\PreSerializeEvent; |
13
|
|
|
use JMS\Serializer\Exception\CircularReferenceDetectedException; |
14
|
|
|
use JMS\Serializer\Exception\ExcludedClassException; |
15
|
|
|
use JMS\Serializer\Exception\ExpressionLanguageRequiredException; |
16
|
|
|
use JMS\Serializer\Exception\InvalidArgumentException; |
17
|
|
|
use JMS\Serializer\Exception\NotAcceptableException; |
18
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
19
|
|
|
use JMS\Serializer\Exception\SkipHandlerException; |
20
|
|
|
use JMS\Serializer\Exception\UninitializedPropertyException; |
21
|
|
|
use JMS\Serializer\Exclusion\ExpressionLanguageExclusionStrategy; |
22
|
|
|
use JMS\Serializer\Expression\ExpressionEvaluatorInterface; |
23
|
|
|
use JMS\Serializer\Functions; |
24
|
|
|
use JMS\Serializer\GraphNavigator; |
25
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
26
|
|
|
use JMS\Serializer\Handler\HandlerRegistryInterface; |
27
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
28
|
|
|
use JMS\Serializer\NullAwareVisitorInterface; |
29
|
|
|
use JMS\Serializer\SerializationContext; |
30
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
31
|
|
|
use JMS\Serializer\VisitorInterface; |
32
|
|
|
use Metadata\MetadataFactoryInterface; |
33
|
|
|
|
34
|
|
|
use function assert; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Handles traversal along the object graph. |
38
|
|
|
* |
39
|
|
|
* This class handles traversal along the graph, and calls different methods |
40
|
|
|
* on visitors, or custom handlers to process its nodes. |
41
|
|
|
* |
42
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
final class SerializationGraphNavigator extends GraphNavigator |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var SerializationVisitorInterface |
48
|
|
|
*/ |
49
|
|
|
protected $visitor; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var SerializationContext |
53
|
|
|
*/ |
54
|
|
|
protected $context; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var ExpressionLanguageExclusionStrategy |
58
|
|
|
*/ |
59
|
|
|
private $expressionExclusionStrategy; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var EventDispatcherInterface |
63
|
|
|
*/ |
64
|
|
|
private $dispatcher; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var MetadataFactoryInterface |
68
|
290 |
|
*/ |
69
|
|
|
private $metadataFactory; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var HandlerRegistryInterface |
73
|
|
|
*/ |
74
|
|
|
private $handlerRegistry; |
75
|
290 |
|
/** |
76
|
290 |
|
* @var AccessorStrategyInterface |
77
|
290 |
|
*/ |
78
|
290 |
|
private $accessor; |
79
|
|
|
|
80
|
290 |
|
/** |
81
|
25 |
|
* @var bool |
82
|
|
|
*/ |
83
|
290 |
|
private $shouldSerializeNull; |
84
|
|
|
|
85
|
290 |
|
public function __construct( |
86
|
|
|
MetadataFactoryInterface $metadataFactory, |
87
|
290 |
|
HandlerRegistryInterface $handlerRegistry, |
88
|
290 |
|
AccessorStrategyInterface $accessor, |
89
|
290 |
|
?EventDispatcherInterface $dispatcher = null, |
90
|
|
|
?ExpressionEvaluatorInterface $expressionEvaluator = null |
91
|
|
|
) { |
92
|
|
|
$this->dispatcher = $dispatcher ?: new EventDispatcher(); |
93
|
|
|
$this->metadataFactory = $metadataFactory; |
94
|
|
|
$this->handlerRegistry = $handlerRegistry; |
95
|
|
|
$this->accessor = $accessor; |
96
|
|
|
|
97
|
|
|
if ($expressionEvaluator) { |
98
|
289 |
|
$this->expressionExclusionStrategy = new ExpressionLanguageExclusionStrategy($expressionEvaluator); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
289 |
|
public function initialize(VisitorInterface $visitor, Context $context): void |
103
|
|
|
{ |
104
|
267 |
|
assert($context instanceof SerializationContext); |
105
|
267 |
|
|
106
|
193 |
|
parent::initialize($visitor, $context); |
107
|
|
|
$this->shouldSerializeNull = $context->shouldSerializeNull(); |
108
|
|
|
} |
109
|
267 |
|
|
110
|
|
|
/** |
111
|
|
|
* Called for each node of the graph that is being traversed. |
112
|
|
|
* |
113
|
154 |
|
* @param mixed $data the data depends on the direction, and type of visitor |
114
|
5 |
|
* @param array|null $type array has the format ["name" => string, "params" => array] |
115
|
|
|
* |
116
|
|
|
* @return mixed the return value depends on the direction, and type of visitor |
117
|
|
|
*/ |
118
|
289 |
|
public function accept($data, ?array $type = null) |
119
|
|
|
{ |
120
|
|
|
// If the type was not given, we infer the most specific type from the |
121
|
|
|
// input data in serialization mode. |
122
|
289 |
|
if (null === $type) { |
123
|
289 |
|
$typeName = \gettype($data); |
124
|
38 |
|
if ('object' === $typeName) { |
125
|
15 |
|
$typeName = \get_class($data); |
126
|
|
|
} |
127
|
23 |
|
|
128
|
|
|
$type = ['name' => $typeName, 'params' => []]; |
129
|
265 |
|
} elseif (null === $data) { |
130
|
163 |
|
// If the data is null, we have to force the type to null regardless of the input in order to |
131
|
|
|
// guarantee correct handling of null values, and not have any internal auto-casting behavior. |
132
|
261 |
|
$type = ['name' => 'NULL', 'params' => []]; |
133
|
261 |
|
} |
134
|
48 |
|
|
135
|
|
|
// Sometimes data can convey null but is not of a null type. |
136
|
258 |
|
// Visitors can have the power to add this custom null evaluation |
137
|
258 |
|
if ($this->visitor instanceof NullAwareVisitorInterface && true === $this->visitor->isNull($data)) { |
138
|
13 |
|
$type = ['name' => 'NULL', 'params' => []]; |
139
|
|
|
} |
140
|
253 |
|
|
141
|
244 |
|
switch ($type['name']) { |
142
|
20 |
|
case 'NULL': |
143
|
|
|
if (!$this->shouldSerializeNull && !$this->isRootNullAllowed()) { |
144
|
244 |
|
throw new NotAcceptableException(); |
145
|
106 |
|
} |
146
|
|
|
|
147
|
206 |
|
return $this->visitor->visitNull($data, $type); |
148
|
1 |
|
|
149
|
1 |
|
case 'string': |
150
|
|
|
return $this->visitor->visitString((string) $data, $type); |
151
|
|
|
|
152
|
|
|
case 'int': |
153
|
1 |
|
case 'integer': |
154
|
|
|
return $this->visitor->visitInteger((int) $data, $type); |
155
|
|
|
|
156
|
|
|
case 'bool': |
157
|
205 |
|
case 'boolean': |
158
|
205 |
|
return $this->visitor->visitBoolean((bool) $data, $type); |
159
|
4 |
|
|
160
|
|
|
case 'double': |
161
|
205 |
|
case 'float': |
162
|
|
|
return $this->visitor->visitDouble((float) $data, $type); |
163
|
|
|
|
164
|
|
|
case 'iterable': |
165
|
|
|
return $this->visitor->visitArray(Functions::iterableToArray($data), $type); |
166
|
205 |
|
|
167
|
203 |
|
case 'array': |
168
|
4 |
|
case 'list': |
169
|
|
|
return $this->visitor->visitArray((array) $data, $type); |
170
|
|
|
|
171
|
|
|
case 'resource': |
172
|
|
|
$msg = 'Resources are not supported in serialized data.'; |
173
|
|
|
if (null !== $path = $this->context->getPath()) { |
174
|
205 |
|
$msg .= ' Path: ' . $path; |
175
|
204 |
|
} |
176
|
204 |
|
|
177
|
|
|
throw new RuntimeException($msg); |
178
|
|
|
|
179
|
|
|
default: |
180
|
|
|
if (null !== $data) { |
181
|
|
|
if ($this->context->isVisiting($data)) { |
182
|
205 |
|
throw new CircularReferenceDetectedException(); |
183
|
49 |
|
} |
184
|
49 |
|
|
185
|
|
|
$this->context->startVisiting($data); |
186
|
49 |
|
} |
187
|
|
|
|
188
|
|
|
// If we're serializing a polymorphic type, then we'll be interested in the |
189
|
|
|
// metadata for the actual type of the object, not the base class. |
190
|
181 |
|
if (class_exists($type['name'], false) || interface_exists($type['name'], false)) { |
191
|
|
|
if (is_subclass_of($data, $type['name'], false) && null === $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $type['name'], $this->format)) { |
|
|
|
|
192
|
179 |
|
$type = ['name' => \get_class($data), 'params' => $type['params'] ?? []]; |
193
|
2 |
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
177 |
|
// Trigger pre-serialization callbacks, and listeners if they exist. |
197
|
10 |
|
// Dispatch pre-serialization event before handling data to have ability change type in listener |
198
|
|
|
if ($this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $this->format)) { |
199
|
10 |
|
$this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $this->format, $event = new PreSerializeEvent($this->context, $data, $type)); |
200
|
|
|
$type = $event->getType(); |
201
|
|
|
} |
202
|
173 |
|
|
203
|
|
|
// First, try whether a custom handler exists for the given type. This is done |
204
|
173 |
|
// before loading metadata because the type name might not be a class, but |
205
|
2 |
|
// could also simply be an artifical type. |
206
|
|
|
if (null !== $handler = $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $type['name'], $this->format)) { |
|
|
|
|
207
|
|
|
try { |
208
|
173 |
|
$rs = \call_user_func($handler, $this->visitor, $data, $type, $this->context); |
|
|
|
|
209
|
173 |
|
$this->context->stopVisiting($data); |
210
|
172 |
|
|
211
|
16 |
|
return $rs; |
212
|
|
|
} catch (SkipHandlerException $e) { |
213
|
|
|
// Skip handler, fallback to default behavior |
214
|
172 |
|
} catch (NotAcceptableException $e) { |
215
|
16 |
|
$this->context->stopVisiting($data); |
216
|
|
|
|
217
|
|
|
throw $e; |
218
|
172 |
|
} |
219
|
|
|
} |
220
|
170 |
|
|
221
|
24 |
|
$metadata = $this->metadataFactory->getMetadataForClass($type['name']); |
222
|
|
|
\assert($metadata instanceof ClassMetadata); |
223
|
|
|
|
224
|
168 |
|
if ($metadata->usingExpression && null === $this->expressionExclusionStrategy) { |
225
|
168 |
|
throw new ExpressionLanguageRequiredException(sprintf('To use conditional exclude/expose in %s you must configure the expression language.', $metadata->name)); |
226
|
167 |
|
} |
227
|
|
|
|
228
|
|
|
if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata, $this->context)) { |
229
|
169 |
|
$this->context->stopVisiting($data); |
230
|
|
|
|
231
|
169 |
|
throw new ExcludedClassException(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if (null !== $this->expressionExclusionStrategy && $this->expressionExclusionStrategy->shouldSkipClass($metadata, $this->context)) { |
235
|
169 |
|
$this->context->stopVisiting($data); |
236
|
|
|
|
237
|
169 |
|
throw new ExcludedClassException(); |
238
|
169 |
|
} |
239
|
|
|
|
240
|
169 |
|
if (!is_object($data)) { |
241
|
2 |
|
throw new InvalidArgumentException('Value at ' . $this->context->getPath() . ' is expected to be an object of class ' . $type['name'] . ' but is of type ' . gettype($data)); |
242
|
|
|
} |
243
|
|
|
|
244
|
169 |
|
$this->context->pushClassMetadata($metadata); |
245
|
2 |
|
|
246
|
|
|
foreach ($metadata->preSerializeMethods as $method) { |
247
|
169 |
|
$method->invoke($data); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$this->visitor->startVisitingObject($metadata, $data, $type); |
251
|
|
|
foreach ($metadata->propertyMetadata as $propertyMetadata) { |
252
|
|
|
if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata, $this->context)) { |
253
|
|
|
continue; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if (null !== $this->expressionExclusionStrategy && $this->expressionExclusionStrategy->shouldSkipProperty($propertyMetadata, $this->context)) { |
257
|
|
|
continue; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** Metadata changes based on context, should not be cached */ |
261
|
|
|
$contextSpecificMetadata = $propertyMetadata; |
262
|
|
|
if (null !== $this->context->getPropertyNamingStrategy()) { |
263
|
|
|
$contextSpecificMetadata = clone $propertyMetadata; |
264
|
|
|
$contextSpecificMetadata->serializedName = $this->context->getPropertyNamingStrategy()->translateName($propertyMetadata); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
try { |
268
|
|
|
$v = $this->accessor->getValue($data, $contextSpecificMetadata, $this->context); |
269
|
|
|
} catch (UninitializedPropertyException $e) { |
270
|
|
|
continue; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if (null === $v && true !== $this->shouldSerializeNull) { |
274
|
|
|
continue; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$this->context->pushPropertyMetadata($contextSpecificMetadata); |
278
|
|
|
$this->visitor->visitProperty($contextSpecificMetadata, $v); |
279
|
|
|
$this->context->popPropertyMetadata(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$this->afterVisitingObject($metadata, $data, $type); |
283
|
|
|
|
284
|
|
|
return $this->visitor->endVisitingObject($metadata, $data, $type); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
private function isRootNullAllowed(): bool |
289
|
|
|
{ |
290
|
|
|
return $this->context->hasAttribute('allows_root_null') && $this->context->getAttribute('allows_root_null') && 0 === $this->context->getVisitingSet()->count(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
private function afterVisitingObject(ClassMetadata $metadata, object $object, array $type): void |
294
|
|
|
{ |
295
|
|
|
$this->context->stopVisiting($object); |
296
|
|
|
$this->context->popClassMetadata(); |
297
|
|
|
|
298
|
|
|
foreach ($metadata->postSerializeMethods as $method) { |
299
|
|
|
$method->invoke($object); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if ($this->dispatcher->hasListeners('serializer.post_serialize', $metadata->name, $this->format)) { |
303
|
|
|
$this->dispatcher->dispatch('serializer.post_serialize', $metadata->name, $this->format, new ObjectEvent($this->context, $object, $type)); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|