1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CCT\Component\Rest\Serializer\Context; |
13
|
|
|
|
14
|
|
|
use CCT\Component\Rest\Serializer\ContextInterface; |
15
|
|
|
use JMS\Serializer\Exclusion\ExclusionStrategyInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Stores the serialization or deserialization context (groups, version, ...). |
19
|
|
|
* |
20
|
|
|
* @author Ener-Getick <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class Context implements ContextInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $attributes = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
private $version; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array|null |
36
|
|
|
*/ |
37
|
|
|
private $groups; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int|null |
41
|
|
|
*/ |
42
|
|
|
private $maxDepth; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $isMaxDepthEnabled; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool|null |
51
|
|
|
*/ |
52
|
|
|
private $serializeNull; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ExclusionStrategyInterface[] |
56
|
|
|
*/ |
57
|
|
|
private $exclusionStrategies = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sets an attribute. |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* @param mixed $value |
64
|
|
|
* |
65
|
|
|
* @return ContextInterface |
66
|
|
|
*/ |
67
|
2 |
|
public function setAttribute($key, $value): ContextInterface |
68
|
|
|
{ |
69
|
2 |
|
$this->attributes[$key] = $value; |
70
|
|
|
|
71
|
2 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Checks if contains a normalization attribute. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function hasAttribute($key): bool |
82
|
|
|
{ |
83
|
|
|
return isset($this->attributes[$key]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets an attribute. |
88
|
|
|
* |
89
|
|
|
* @param string $key |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function getAttribute($key) |
94
|
|
|
{ |
95
|
|
|
if (isset($this->attributes[$key])) { |
96
|
|
|
return $this->attributes[$key]; |
97
|
|
|
} |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets the attributes. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
11 |
|
public function getAttributes(): array |
107
|
|
|
{ |
108
|
11 |
|
return $this->attributes; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets the normalization version. |
113
|
|
|
* |
114
|
|
|
* @param string|null $version |
115
|
|
|
* |
116
|
|
|
* @return ContextInterface |
117
|
|
|
*/ |
118
|
2 |
|
public function setVersion($version): ContextInterface |
119
|
|
|
{ |
120
|
2 |
|
$this->version = $version; |
121
|
|
|
|
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets the normalization version. |
127
|
|
|
* |
128
|
|
|
* @return string|null |
129
|
|
|
*/ |
130
|
11 |
|
public function getVersion(): ?string |
131
|
|
|
{ |
132
|
11 |
|
return $this->version; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Adds a normalization group. |
137
|
|
|
* |
138
|
|
|
* @param string $group |
139
|
|
|
* |
140
|
|
|
* @return ContextInterface |
141
|
|
|
*/ |
142
|
|
|
public function addGroup($group): ContextInterface |
143
|
|
|
{ |
144
|
|
|
if (null === $this->groups) { |
145
|
|
|
$this->groups = []; |
146
|
|
|
} |
147
|
|
|
if (!\in_array($group, $this->groups, true)) { |
148
|
|
|
$this->groups[] = $group; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Adds normalization groups. |
156
|
|
|
* |
157
|
|
|
* @param string[] $groups |
158
|
|
|
* |
159
|
|
|
* @return ContextInterface |
160
|
|
|
*/ |
161
|
|
|
public function addGroups(array $groups): ContextInterface |
162
|
|
|
{ |
163
|
|
|
foreach ($groups as $group) { |
164
|
|
|
$this->addGroup($group); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Gets the normalization groups. |
172
|
|
|
* |
173
|
|
|
* @return string[]|null |
174
|
|
|
*/ |
175
|
11 |
|
public function getGroups(): ?array |
176
|
|
|
{ |
177
|
11 |
|
return $this->groups; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set the normalization groups. |
182
|
|
|
* |
183
|
|
|
* @param string[]|null $groups |
184
|
|
|
* |
185
|
|
|
* @return ContextInterface |
186
|
|
|
*/ |
187
|
6 |
|
public function setGroups(array $groups = null): ContextInterface |
188
|
|
|
{ |
189
|
6 |
|
$this->groups = $groups; |
190
|
|
|
|
191
|
6 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Sets the normalization max depth. |
196
|
|
|
* |
197
|
|
|
* @param int|null $maxDepth |
198
|
|
|
* |
199
|
|
|
* @return ContextInterface |
200
|
|
|
*/ |
201
|
|
|
public function setMaxDepth($maxDepth): ContextInterface |
202
|
|
|
{ |
203
|
|
|
$this->maxDepth = $maxDepth; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Gets the normalization max depth. |
210
|
|
|
* |
211
|
|
|
* @return int|null |
212
|
|
|
* |
213
|
|
|
*/ |
214
|
7 |
|
public function getMaxDepth(): ?int |
215
|
|
|
{ |
216
|
7 |
|
return $this->maxDepth; |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
public function enableMaxDepth() |
220
|
|
|
{ |
221
|
2 |
|
$this->isMaxDepthEnabled = true; |
222
|
|
|
|
223
|
2 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function disableMaxDepth() |
227
|
|
|
{ |
228
|
|
|
$this->isMaxDepthEnabled = false; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return bool|null |
235
|
|
|
*/ |
236
|
11 |
|
public function isMaxDepthEnabled(): ?bool |
237
|
|
|
{ |
238
|
11 |
|
return $this->isMaxDepthEnabled; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Sets serialize null. |
243
|
|
|
* |
244
|
|
|
* @param bool|null $serializeNull |
245
|
|
|
* |
246
|
|
|
* @return ContextInterface |
247
|
|
|
*/ |
248
|
2 |
|
public function setSerializeNull($serializeNull): ContextInterface |
249
|
|
|
{ |
250
|
2 |
|
$this->serializeNull = $serializeNull; |
251
|
|
|
|
252
|
2 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Gets serialize null. |
257
|
|
|
* |
258
|
|
|
* @return bool|null |
259
|
|
|
*/ |
260
|
9 |
|
public function getSerializeNull(): ?bool |
261
|
|
|
{ |
262
|
9 |
|
return $this->serializeNull; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Gets the array of exclusion strategies. |
267
|
|
|
* |
268
|
|
|
* Notice: This method only applies to the JMS serializer adapter. |
269
|
|
|
* |
270
|
|
|
* @return ExclusionStrategyInterface[] |
271
|
|
|
*/ |
272
|
7 |
|
public function getExclusionStrategies(): array |
273
|
|
|
{ |
274
|
7 |
|
return $this->exclusionStrategies; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Adds an exclusion strategy. |
279
|
|
|
* |
280
|
|
|
* Notice: This method only applies to the JMS serializer adapter. |
281
|
|
|
* |
282
|
|
|
* @param ExclusionStrategyInterface $exclusionStrategy |
283
|
|
|
*/ |
284
|
1 |
|
public function addExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy): void |
285
|
|
|
{ |
286
|
1 |
|
$this->exclusionStrategies[] = $exclusionStrategy; |
287
|
1 |
|
} |
288
|
|
|
|
289
|
4 |
|
public static function create() |
290
|
|
|
{ |
291
|
4 |
|
return new static(); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|