1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Context; |
13
|
|
|
|
14
|
|
|
use Ivory\Serializer\Exclusion\ExclusionStrategy; |
15
|
|
|
use Ivory\Serializer\Exclusion\ExclusionStrategyInterface; |
16
|
|
|
use Ivory\Serializer\Mapping\MetadataInterface; |
17
|
|
|
use Ivory\Serializer\Naming\IdenticalNamingStrategy; |
18
|
|
|
use Ivory\Serializer\Naming\NamingStrategyInterface; |
19
|
|
|
use Ivory\Serializer\Navigator\NavigatorInterface; |
20
|
|
|
use Ivory\Serializer\Visitor\VisitorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author GeLo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Context implements ContextInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var NavigatorInterface |
29
|
|
|
*/ |
30
|
|
|
private $navigator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var VisitorInterface |
34
|
|
|
*/ |
35
|
|
|
private $visitor; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $direction; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var mixed[] |
44
|
|
|
*/ |
45
|
|
|
private $dataStack; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var MetadataInterface[] |
49
|
|
|
*/ |
50
|
|
|
private $metadataStack; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ExclusionStrategyInterface |
54
|
|
|
*/ |
55
|
|
|
private $exclusionStrategy; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var NamingStrategyInterface |
59
|
|
|
*/ |
60
|
|
|
private $namingStrategy; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ExclusionStrategyInterface|null $exclusionStrategy |
64
|
|
|
* @param NamingStrategyInterface|null $namingStrategy |
65
|
|
|
*/ |
66
|
468 |
|
public function __construct( |
67
|
|
|
ExclusionStrategyInterface $exclusionStrategy = null, |
68
|
|
|
NamingStrategyInterface $namingStrategy = null |
69
|
|
|
) { |
70
|
468 |
|
$this->exclusionStrategy = $exclusionStrategy ?: new ExclusionStrategy(); |
71
|
468 |
|
$this->namingStrategy = $namingStrategy ?: new IdenticalNamingStrategy(); |
72
|
468 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
549 |
|
public function initialize(NavigatorInterface $navigator, VisitorInterface $visitor, $direction) |
78
|
|
|
{ |
79
|
366 |
|
$this |
80
|
549 |
|
->setNavigator($navigator) |
81
|
549 |
|
->setVisitor($visitor) |
82
|
549 |
|
->setDirection($direction) |
83
|
549 |
|
->setDataStack([]) |
84
|
549 |
|
->setMetadataStack([]); |
85
|
549 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
549 |
|
public function getNavigator() |
91
|
|
|
{ |
92
|
549 |
|
return $this->navigator; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
549 |
|
public function setNavigator(NavigatorInterface $navigator) |
99
|
|
|
{ |
100
|
549 |
|
$this->navigator = $navigator; |
101
|
|
|
|
102
|
549 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
549 |
|
public function getVisitor() |
109
|
|
|
{ |
110
|
549 |
|
return $this->visitor; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
549 |
|
public function setVisitor(VisitorInterface $visitor) |
117
|
|
|
{ |
118
|
549 |
|
$this->visitor = $visitor; |
119
|
|
|
|
120
|
549 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
72 |
|
public function getDirection() |
127
|
|
|
{ |
128
|
72 |
|
return $this->direction; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
549 |
|
public function setDirection($direction) |
135
|
|
|
{ |
136
|
549 |
|
$this->direction = $direction; |
137
|
|
|
|
138
|
549 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
411 |
|
public function getExclusionStrategy() |
145
|
|
|
{ |
146
|
411 |
|
return $this->exclusionStrategy; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy) |
153
|
|
|
{ |
154
|
|
|
$this->exclusionStrategy = $exclusionStrategy; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
387 |
|
public function getNamingStrategy() |
163
|
|
|
{ |
164
|
387 |
|
return $this->namingStrategy; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
public function setNamingStrategy(NamingStrategyInterface $namingStrategy) |
171
|
|
|
{ |
172
|
|
|
$this->namingStrategy = $namingStrategy; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
18 |
|
public function getDataStack() |
181
|
|
|
{ |
182
|
18 |
|
return $this->dataStack; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
549 |
|
public function setDataStack(array $dataStack) |
189
|
|
|
{ |
190
|
549 |
|
$this->dataStack = $dataStack; |
191
|
|
|
|
192
|
549 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
18 |
|
public function getMetadataStack() |
199
|
|
|
{ |
200
|
18 |
|
return $this->metadataStack; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
549 |
|
public function setMetadataStack(array $metadataStack) |
207
|
|
|
{ |
208
|
549 |
|
$this->metadataStack = $metadataStack; |
209
|
|
|
|
210
|
549 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
411 |
|
public function enterScope($data, MetadataInterface $metadata) |
217
|
|
|
{ |
218
|
411 |
|
$this->dataStack[] = $data; |
219
|
411 |
|
$this->metadataStack[] = $metadata; |
220
|
411 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
411 |
|
public function leaveScope() |
226
|
|
|
{ |
227
|
411 |
|
array_pop($this->dataStack); |
228
|
411 |
|
array_pop($this->metadataStack); |
229
|
411 |
|
} |
230
|
|
|
} |
231
|
|
|
|