|
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\Mapping\Loader; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\Serializer\Exclusion\ExclusionPolicy; |
|
15
|
|
|
use Ivory\Serializer\Mapping\ClassMetadataInterface; |
|
16
|
|
|
use Ivory\Serializer\Mapping\PropertyMetadata; |
|
17
|
|
|
use Ivory\Serializer\Mapping\PropertyMetadataInterface; |
|
18
|
|
|
use Ivory\Serializer\Type\Parser\TypeParser; |
|
19
|
|
|
use Ivory\Serializer\Type\Parser\TypeParserInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author GeLo <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractClassMetadataLoader implements ClassMetadataLoaderInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var TypeParserInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $typeParser; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var mixed[][] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $data = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param TypeParserInterface|null $typeParser |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(TypeParserInterface $typeParser = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->typeParser = $typeParser ?: new TypeParser(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
|
48
|
|
|
{ |
|
49
|
|
|
$class = $classMetadata->getName(); |
|
50
|
|
|
|
|
51
|
|
|
if (!array_key_exists($class, $this->data)) { |
|
52
|
|
|
$this->data[$class] = $this->loadData($class); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!is_array($data = $this->data[$class])) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->doLoadClassMetadata($classMetadata, $data); |
|
60
|
|
|
|
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $class |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed[]|null |
|
68
|
|
|
*/ |
|
69
|
|
|
abstract protected function loadData($class); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param ClassMetadataInterface $classMetadata |
|
73
|
|
|
* @param mixed[] $data |
|
74
|
|
|
*/ |
|
75
|
|
|
private function doLoadClassMetadata(ClassMetadataInterface $classMetadata, array $data) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!isset($data['properties']) || empty($data['properties'])) { |
|
78
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
79
|
|
|
'No mapping properties found for "%s".', |
|
80
|
|
|
$classMetadata->getName() |
|
81
|
|
|
)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$properties = $classMetadata->getProperties(); |
|
85
|
|
|
$policy = $this->getExclusionPolicy($data); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($data['properties'] as $property => $value) { |
|
88
|
|
|
$propertyMetadata = $classMetadata->getProperty($property) ?: new PropertyMetadata($property); |
|
89
|
|
|
$this->loadPropertyMetadata($propertyMetadata, $value); |
|
90
|
|
|
|
|
91
|
|
|
if ($this->isPropertyMetadataExposed($value, $policy)) { |
|
92
|
|
|
$properties[$property] = $propertyMetadata; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (($order = $this->getOrder($data, $properties)) !== null) { |
|
97
|
|
|
$properties = $this->sortProperties($properties, $order); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$classMetadata->setProperties($properties); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param PropertyMetadataInterface $propertyMetadata |
|
105
|
|
|
* @param mixed $data |
|
106
|
|
|
*/ |
|
107
|
|
|
private function loadPropertyMetadata(PropertyMetadataInterface $propertyMetadata, $data) |
|
108
|
|
|
{ |
|
109
|
|
|
if (!is_array($data)) { |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (array_key_exists('exclude', $data)) { |
|
114
|
|
|
$this->validatePropertyMetadataExclude($data['exclude']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (array_key_exists('expose', $data)) { |
|
118
|
|
|
$this->validatePropertyMetadataExpose($data['expose']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (array_key_exists('alias', $data)) { |
|
122
|
|
|
$this->loadPropertyMetadataAlias($propertyMetadata, $data['alias']); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (array_key_exists('type', $data)) { |
|
126
|
|
|
$this->loadPropertyMetadataType($propertyMetadata, $data['type']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (array_key_exists('since', $data)) { |
|
130
|
|
|
$this->loadPropertyMetadataSinceVersion($propertyMetadata, $data['since']); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (array_key_exists('until', $data)) { |
|
134
|
|
|
$this->loadPropertyMetadataUntilVersion($propertyMetadata, $data['until']); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (array_key_exists('max_depth', $data)) { |
|
138
|
|
|
$this->loadPropertyMetadataMaxDepth($propertyMetadata, $data['max_depth']); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if (array_key_exists('groups', $data)) { |
|
142
|
|
|
$this->loadPropertyMetadataGroups($propertyMetadata, $data['groups']); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param PropertyMetadataInterface $propertyMetadata |
|
148
|
|
|
* @param string $alias |
|
149
|
|
|
*/ |
|
150
|
|
View Code Duplication |
private function loadPropertyMetadataAlias(PropertyMetadataInterface $propertyMetadata, $alias) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
if (!is_string($alias)) { |
|
153
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
154
|
|
|
'The mapping property alias must be a non empty string, got "%s".', |
|
155
|
|
|
is_object($alias) ? get_class($alias) : gettype($alias) |
|
156
|
|
|
)); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$alias = trim($alias); |
|
160
|
|
|
|
|
161
|
|
|
if (empty($alias)) { |
|
162
|
|
|
throw new \InvalidArgumentException('The mapping property alias must be a non empty string.'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$propertyMetadata->setAlias($alias); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param PropertyMetadataInterface $propertyMetadata |
|
170
|
|
|
* @param string $type |
|
171
|
|
|
*/ |
|
172
|
|
|
private function loadPropertyMetadataType(PropertyMetadataInterface $propertyMetadata, $type) |
|
173
|
|
|
{ |
|
174
|
|
|
if (!is_string($type)) { |
|
175
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
176
|
|
|
'The mapping property type must be a non empty string, got "%s".', |
|
177
|
|
|
is_object($type) ? get_class($type) : gettype($type) |
|
178
|
|
|
)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$propertyMetadata->setType($this->typeParser->parse($type)); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param PropertyMetadataInterface $propertyMetadata |
|
186
|
|
|
* @param string $version |
|
187
|
|
|
*/ |
|
188
|
|
View Code Duplication |
private function loadPropertyMetadataSinceVersion(PropertyMetadataInterface $propertyMetadata, $version) |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
if (!is_string($version)) { |
|
191
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
192
|
|
|
'The mapping property since version must be a non empty string, got "%s".', |
|
193
|
|
|
is_object($version) ? get_class($version) : gettype($version) |
|
194
|
|
|
)); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$version = trim($version); |
|
198
|
|
|
|
|
199
|
|
|
if (empty($version)) { |
|
200
|
|
|
throw new \InvalidArgumentException('The mapping property since version must be a non empty string.'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$propertyMetadata->setSinceVersion($version); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param PropertyMetadataInterface $propertyMetadata |
|
208
|
|
|
* @param string $version |
|
209
|
|
|
*/ |
|
210
|
|
View Code Duplication |
private function loadPropertyMetadataUntilVersion(PropertyMetadataInterface $propertyMetadata, $version) |
|
|
|
|
|
|
211
|
|
|
{ |
|
212
|
|
|
if (!is_string($version)) { |
|
213
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
214
|
|
|
'The mapping property until version must be a non empty string, got "%s".', |
|
215
|
|
|
is_object($version) ? get_class($version) : gettype($version) |
|
216
|
|
|
)); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$version = trim($version); |
|
220
|
|
|
|
|
221
|
|
|
if (empty($version)) { |
|
222
|
|
|
throw new \InvalidArgumentException('The mapping property until version must be a non empty string.'); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$propertyMetadata->setUntilVersion($version); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param PropertyMetadataInterface $propertyMetadata |
|
230
|
|
|
* @param string|int $maxDepth |
|
231
|
|
|
*/ |
|
232
|
|
|
private function loadPropertyMetadataMaxDepth(PropertyMetadataInterface $propertyMetadata, $maxDepth) |
|
233
|
|
|
{ |
|
234
|
|
|
if (!is_int($maxDepth) && !is_string($maxDepth) && !ctype_digit($maxDepth)) { |
|
235
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
236
|
|
|
'The mapping property max depth must be a positive integer, got "%s".', |
|
237
|
|
|
is_object($maxDepth) ? get_class($maxDepth) : gettype($maxDepth) |
|
238
|
|
|
)); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$maxDepth = (int) $maxDepth; |
|
242
|
|
|
|
|
243
|
|
|
if ($maxDepth <= 0) { |
|
244
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
245
|
|
|
'The mapping property max depth must be a positive integer, got "%d".', |
|
246
|
|
|
$maxDepth |
|
247
|
|
|
)); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
$propertyMetadata->setMaxDepth($maxDepth); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param PropertyMetadataInterface $propertyMetadata |
|
255
|
|
|
* @param string[] $groups |
|
256
|
|
|
*/ |
|
257
|
|
|
private function loadPropertyMetadataGroups(PropertyMetadataInterface $propertyMetadata, $groups) |
|
258
|
|
|
{ |
|
259
|
|
|
if (!is_array($groups)) { |
|
260
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
261
|
|
|
'The mapping property groups must be an array of non empty strings, got "%s".', |
|
262
|
|
|
is_object($groups) ? get_class($groups) : gettype($groups) |
|
263
|
|
|
)); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
foreach ($groups as $group) { |
|
267
|
|
|
if (!is_string($group)) { |
|
268
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
269
|
|
|
'The mapping property groups must be an array of non empty strings, got "%s".', |
|
270
|
|
|
is_object($group) ? get_class($group) : gettype($group) |
|
271
|
|
|
)); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$group = trim($group); |
|
275
|
|
|
|
|
276
|
|
|
if (empty($group)) { |
|
277
|
|
|
throw new \InvalidArgumentException( |
|
278
|
|
|
'The mapping property groups must be an array of non empty strings.' |
|
279
|
|
|
); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
$propertyMetadata->addGroup($group); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param mixed[] $data |
|
288
|
|
|
* |
|
289
|
|
|
* @return string|null |
|
290
|
|
|
*/ |
|
291
|
|
|
private function getExclusionPolicy(array $data) |
|
292
|
|
|
{ |
|
293
|
|
|
if (!isset($data['exclusion_policy'])) { |
|
294
|
|
|
return ExclusionPolicy::NONE; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
$policy = $data['exclusion_policy']; |
|
298
|
|
|
|
|
299
|
|
|
if (!is_string($policy)) { |
|
300
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
301
|
|
|
'The mapping exclusion policy must be "%s" or "%s", got "%s".', |
|
302
|
|
|
ExclusionPolicy::ALL, |
|
303
|
|
|
ExclusionPolicy::NONE, |
|
304
|
|
|
is_object($policy) ? get_class($policy) : gettype($policy) |
|
305
|
|
|
)); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
$policy = strtolower(trim($policy)); |
|
309
|
|
|
|
|
310
|
|
|
if ($policy !== ExclusionPolicy::ALL && $policy !== ExclusionPolicy::NONE) { |
|
311
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
312
|
|
|
'The mapping exclusion policy must be "%s" or "%s", got "%s".', |
|
313
|
|
|
ExclusionPolicy::ALL, |
|
314
|
|
|
ExclusionPolicy::NONE, |
|
315
|
|
|
$policy |
|
316
|
|
|
)); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
return $policy; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* @param mixed[] $data |
|
324
|
|
|
* @param PropertyMetadataInterface[] $properties |
|
325
|
|
|
* |
|
326
|
|
|
* @return string|string[]|null |
|
327
|
|
|
*/ |
|
328
|
|
|
private function getOrder(array $data, array $properties) |
|
329
|
|
|
{ |
|
330
|
|
|
if (!isset($data['order'])) { |
|
331
|
|
|
return; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
$order = $data['order']; |
|
335
|
|
|
|
|
336
|
|
|
if (is_string($order)) { |
|
337
|
|
|
$order = trim($order); |
|
338
|
|
|
|
|
339
|
|
|
if (empty($order)) { |
|
340
|
|
|
throw new \InvalidArgumentException( |
|
341
|
|
|
'The mapping order must be an non empty strings or an array of non empty strings.' |
|
342
|
|
|
); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
if (strcasecmp($order, 'ASC') === 0 || strcasecmp($order, 'DESC') === 0) { |
|
346
|
|
|
return strtoupper($order); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
$order = explode(',', $order); |
|
350
|
|
|
} elseif (!is_array($order)) { |
|
351
|
|
|
throw new \InvalidArgumentException( |
|
352
|
|
|
'The mapping order must be an non empty strings or an array of non empty strings.' |
|
353
|
|
|
); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
if (empty($order)) { |
|
357
|
|
|
throw new \InvalidArgumentException( |
|
358
|
|
|
'The mapping order must be an non empty strings or an array of non empty strings.' |
|
359
|
|
|
); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
foreach ($order as &$property) { |
|
363
|
|
|
if (!is_string($property)) { |
|
364
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
365
|
|
|
'The mapping order must be an non empty strings or an array of non empty strings, got "%s".', |
|
366
|
|
|
is_object($property) ? get_class($property) : gettype($property) |
|
367
|
|
|
)); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
$property = trim($property); |
|
371
|
|
|
|
|
372
|
|
|
if (empty($property)) { |
|
373
|
|
|
throw new \InvalidArgumentException( |
|
374
|
|
|
'The mapping order must be an non empty strings or an array of non empty strings.' |
|
375
|
|
|
); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
if (!isset($properties[$property])) { |
|
379
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
380
|
|
|
'The property "%s" defined in the mapping order does not exist.', |
|
381
|
|
|
$property |
|
382
|
|
|
)); |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
return $order; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* @param PropertyMetadataInterface[] $properties |
|
391
|
|
|
* @param string|string[] $order |
|
392
|
|
|
* |
|
393
|
|
|
* @return PropertyMetadataInterface[] |
|
394
|
|
|
*/ |
|
395
|
|
|
private function sortProperties(array $properties, $order) |
|
396
|
|
|
{ |
|
397
|
|
|
if (is_string($order)) { |
|
398
|
|
|
if ($order === 'ASC') { |
|
399
|
|
|
ksort($properties); |
|
400
|
|
|
} else { |
|
401
|
|
|
krsort($properties); |
|
402
|
|
|
} |
|
403
|
|
|
} elseif (is_array($order)) { |
|
404
|
|
|
$properties = array_merge(array_flip($order), $properties); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
return $properties; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* @param bool $exclude |
|
412
|
|
|
*/ |
|
413
|
|
View Code Duplication |
private function validatePropertyMetadataExclude($exclude) |
|
|
|
|
|
|
414
|
|
|
{ |
|
415
|
|
|
if (!is_bool($exclude)) { |
|
416
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
417
|
|
|
'The mapping property exclude must be a boolean, got "%s".', |
|
418
|
|
|
is_object($exclude) ? get_class($exclude) : gettype($exclude) |
|
419
|
|
|
)); |
|
420
|
|
|
} |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
/** |
|
424
|
|
|
* @param bool $expose |
|
425
|
|
|
*/ |
|
426
|
|
View Code Duplication |
private function validatePropertyMetadataExpose($expose) |
|
|
|
|
|
|
427
|
|
|
{ |
|
428
|
|
|
if (!is_bool($expose)) { |
|
429
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
430
|
|
|
'The mapping property expose must be a boolean, got "%s".', |
|
431
|
|
|
is_object($expose) ? get_class($expose) : gettype($expose) |
|
432
|
|
|
)); |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* @param mixed[] $property |
|
438
|
|
|
* @param string $policy |
|
439
|
|
|
* |
|
440
|
|
|
* @return bool |
|
441
|
|
|
*/ |
|
442
|
|
|
private function isPropertyMetadataExposed($property, $policy) |
|
443
|
|
|
{ |
|
444
|
|
|
$expose = isset($property['expose']) && $property['expose']; |
|
445
|
|
|
$exclude = isset($property['exclude']) && $property['exclude']; |
|
446
|
|
|
|
|
447
|
|
|
return ($policy === ExclusionPolicy::ALL && $expose) || ($policy === ExclusionPolicy::NONE && !$exclude); |
|
448
|
|
|
} |
|
449
|
|
|
} |
|
450
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.