1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
8
|
|
|
use Metadata\MetadataFactoryInterface; |
9
|
|
|
|
10
|
|
|
class SerializationContext extends Context |
11
|
|
|
{ |
12
|
|
|
/** @var \SplObjectStorage */ |
13
|
|
|
private $visitingSet; |
14
|
|
|
|
15
|
|
|
/** @var \SplStack */ |
16
|
|
|
private $visitingStack; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $initialType; |
22
|
|
|
|
23
|
108 |
|
/** |
24
|
|
|
* @var bool |
25
|
108 |
|
*/ |
26
|
|
|
private $serializeNull = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
287 |
|
private $serializeUnitializedNull = false; |
32
|
|
|
|
33
|
287 |
|
public static function create(): self |
34
|
|
|
{ |
35
|
287 |
|
return new self(); |
36
|
287 |
|
} |
37
|
287 |
|
|
38
|
|
|
public function initialize(string $format, VisitorInterface $visitor, GraphNavigatorInterface $navigator, MetadataFactoryInterface $factory): void |
39
|
208 |
|
{ |
40
|
|
|
parent::initialize($format, $visitor, $navigator, $factory); |
41
|
208 |
|
|
42
|
6 |
|
$this->visitingSet = new \SplObjectStorage(); |
43
|
|
|
$this->visitingStack = new \SplStack(); |
44
|
202 |
|
} |
45
|
202 |
|
|
46
|
202 |
|
/** |
47
|
|
|
* Set if NULLs should be serialized (TRUE) ot not (FALSE) |
48
|
200 |
|
*/ |
49
|
|
|
public function setSerializeNull(bool $bool): self |
50
|
200 |
|
{ |
51
|
6 |
|
$this->assertMutable(); |
52
|
|
|
|
53
|
194 |
|
$this->serializeNull = $bool; |
54
|
194 |
|
|
55
|
|
|
return $this; |
56
|
194 |
|
} |
57
|
|
|
|
58
|
|
|
public function getShouldSerializeUnitializedAsNull(): bool |
59
|
194 |
|
{ |
60
|
|
|
return $this->serializeUnitializedNull; |
61
|
208 |
|
} |
62
|
|
|
|
63
|
208 |
|
public function setShouldSerializeUnitializedAsNull(bool $bool): self |
64
|
6 |
|
{ |
65
|
|
|
$this->assertMutable(); |
66
|
|
|
|
67
|
202 |
|
$this->serializeUnitializedNull = $bool; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
/** |
73
|
1 |
|
* Returns TRUE when NULLs should be serialized |
74
|
1 |
|
* Returns FALSE when NULLs should not be serialized |
75
|
|
|
*/ |
76
|
|
|
public function shouldSerializeNull(): bool |
77
|
1 |
|
{ |
78
|
|
|
return $this->serializeNull; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
/** |
82
|
|
|
* @param mixed $object |
83
|
|
|
*/ |
84
|
3 |
|
public function startVisiting($object): void |
85
|
|
|
{ |
86
|
3 |
|
if (!\is_object($object)) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
7 |
|
|
90
|
|
|
$this->visitingSet->attach($object); |
91
|
7 |
|
$this->visitingStack->push($object); |
92
|
|
|
} |
93
|
|
|
|
94
|
23 |
|
/** |
95
|
|
|
* @param mixed $object |
96
|
23 |
|
*/ |
97
|
|
|
public function stopVisiting($object): void |
98
|
|
|
{ |
99
|
|
|
if (!\is_object($object)) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->visitingSet->detach($object); |
104
|
|
|
$poppedObject = $this->visitingStack->pop(); |
105
|
|
|
|
106
|
|
|
if ($object !== $poppedObject) { |
107
|
|
|
throw new RuntimeException('Context visitingStack not working well'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param mixed $object |
113
|
6 |
|
*/ |
114
|
|
|
public function isVisiting($object): bool |
115
|
6 |
|
{ |
116
|
6 |
|
if (!\is_object($object)) { |
117
|
6 |
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
return $this->visitingSet->contains($object); |
121
|
|
|
} |
122
|
1 |
|
|
123
|
1 |
|
public function getPath(): ?string |
124
|
1 |
|
{ |
125
|
|
|
$path = []; |
126
|
|
|
foreach ($this->visitingStack as $obj) { |
127
|
|
|
$path[] = \get_class($obj); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!$path) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return implode(' -> ', $path); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getDirection(): int |
138
|
|
|
{ |
139
|
|
|
return GraphNavigatorInterface::DIRECTION_SERIALIZATION; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getDepth(): int |
143
|
|
|
{ |
144
|
|
|
return $this->visitingStack->count(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getObject(): ?object |
148
|
|
|
{ |
149
|
|
|
return !$this->visitingStack->isEmpty() ? $this->visitingStack->top() : null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getVisitingStack(): \SplStack |
153
|
|
|
{ |
154
|
|
|
return $this->visitingStack; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getVisitingSet(): \SplObjectStorage |
158
|
|
|
{ |
159
|
|
|
return $this->visitingSet; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function setInitialType(string $type): self |
166
|
|
|
{ |
167
|
|
|
$this->assertMutable(); |
168
|
|
|
|
169
|
|
|
$this->initialType = $type; |
170
|
|
|
$this->setAttribute('initial_type', $type); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getInitialType(): ?string |
176
|
|
|
{ |
177
|
|
|
return $this->initialType |
178
|
|
|
? $this->initialType |
179
|
|
|
: ($this->hasAttribute('initial_type') ? $this->getAttribute('initial_type') : null); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|