1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Metadata\Driver; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Annotation\ExclusionPolicy; |
8
|
|
|
use JMS\Serializer\Exception\InvalidMetadataException; |
9
|
|
|
use JMS\Serializer\Exception\XmlErrorException; |
10
|
|
|
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface; |
11
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
12
|
|
|
use JMS\Serializer\Metadata\ExpressionPropertyMetadata; |
13
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
14
|
|
|
use JMS\Serializer\Metadata\VirtualPropertyMetadata; |
15
|
|
|
use JMS\Serializer\Naming\PropertyNamingStrategyInterface; |
16
|
|
|
use JMS\Serializer\Type\Parser; |
17
|
|
|
use JMS\Serializer\Type\ParserInterface; |
18
|
|
|
use Metadata\ClassMetadata as BaseClassMetadata; |
19
|
|
|
use Metadata\Driver\AbstractFileDriver; |
20
|
|
|
use Metadata\Driver\FileLocatorInterface; |
21
|
|
|
use Metadata\MethodMetadata; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @method ClassMetadata|null loadMetadataForClass(\ReflectionClass $class) |
25
|
|
|
*/ |
26
|
|
|
class XmlDriver extends AbstractFileDriver |
27
|
|
|
{ |
28
|
|
|
use ExpressionMetadataTrait; |
29
|
33 |
|
|
30
|
|
|
/** |
31
|
33 |
|
* @var ParserInterface |
32
|
33 |
|
*/ |
33
|
33 |
|
private $typeParser; |
34
|
33 |
|
/** |
35
|
|
|
* @var PropertyNamingStrategyInterface |
36
|
31 |
|
*/ |
37
|
|
|
private $namingStrategy; |
38
|
31 |
|
|
39
|
31 |
|
public function __construct(FileLocatorInterface $locator, PropertyNamingStrategyInterface $namingStrategy, ?ParserInterface $typeParser = null, ?CompilableExpressionEvaluatorInterface $expressionEvaluator = null) |
40
|
|
|
{ |
41
|
31 |
|
parent::__construct($locator); |
42
|
31 |
|
|
43
|
|
|
$this->typeParser = $typeParser ?? new Parser(); |
44
|
31 |
|
$this->namingStrategy = $namingStrategy; |
45
|
1 |
|
$this->expressionEvaluator = $expressionEvaluator; |
46
|
|
|
} |
47
|
|
|
|
48
|
30 |
|
protected function loadMetadataFromFile(\ReflectionClass $class, string $path): ?BaseClassMetadata |
49
|
30 |
|
{ |
50
|
|
|
$previous = libxml_use_internal_errors(true); |
51
|
|
|
libxml_clear_errors(); |
52
|
30 |
|
|
53
|
|
|
$elem = simplexml_load_file($path); |
54
|
30 |
|
libxml_use_internal_errors($previous); |
55
|
30 |
|
|
56
|
30 |
|
if (false === $elem) { |
57
|
30 |
|
throw new InvalidMetadataException('Invalid XML content for metadata', 0, new XmlErrorException(libxml_get_last_error())); |
58
|
30 |
|
} |
59
|
|
|
|
60
|
30 |
|
$metadata = new ClassMetadata($name = $class->name); |
61
|
30 |
|
if (!$elems = $elem->xpath("./class[@name = '" . $name . "']")) { |
62
|
|
|
throw new InvalidMetadataException(sprintf('Could not find class %s inside XML element.', $name)); |
63
|
30 |
|
} |
64
|
|
|
|
65
|
|
|
$elem = reset($elems); |
66
|
|
|
|
67
|
30 |
|
$metadata->fileResources[] = $path; |
68
|
7 |
|
$fileResource = $class->getFilename(); |
69
|
|
|
if (false !== $fileResource) { |
70
|
|
|
$metadata->fileResources[] = $fileResource; |
71
|
30 |
|
} |
72
|
1 |
|
|
73
|
|
|
$exclusionPolicy = strtoupper((string) $elem->attributes()->{'exclusion-policy'}) ?: 'NONE'; |
74
|
30 |
|
$exclude = $elem->attributes()->exclude; |
75
|
1 |
|
$excludeAll = null !== $exclude ? 'true' === strtolower((string) $exclude) : false; |
76
|
|
|
|
77
|
|
|
if (null !== $excludeIf = $elem->attributes()->{'exclude-if'}) { |
78
|
30 |
|
$metadata->excludeIf = $this->parseExpression((string) $excludeIf); |
79
|
|
|
} |
80
|
30 |
|
|
81
|
30 |
|
$classAccessType = (string) ($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY); |
82
|
30 |
|
|
83
|
5 |
|
$propertiesMetadata = []; |
84
|
|
|
$propertiesNodes = []; |
85
|
|
|
|
86
|
|
|
if (null !== $accessorOrder = $elem->attributes()->{'accessor-order'}) { |
87
|
5 |
|
$metadata->setAccessorOrder((string) $accessorOrder, preg_split('/\s*,\s*/', (string) $elem->attributes()->{'custom-accessor-order'})); |
88
|
|
|
} |
89
|
|
|
|
90
|
30 |
|
if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) { |
91
|
|
|
$metadata->xmlRootName = (string) $xmlRootName; |
92
|
30 |
|
} |
93
|
|
|
|
94
|
5 |
|
if (null !== $xmlRootNamespace = $elem->attributes()->{'xml-root-namespace'}) { |
95
|
5 |
|
$metadata->xmlRootNamespace = (string) $xmlRootNamespace; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
5 |
|
if (null !== $xmlRootPrefix = $elem->attributes()->{'xml-root-prefix'}) { |
99
|
|
|
$metadata->xmlRootPrefix = (string) $xmlRootPrefix; |
100
|
|
|
} |
101
|
30 |
|
|
102
|
3 |
|
$readOnlyClass = 'true' === strtolower((string) $elem->attributes()->{'read-only'}); |
103
|
|
|
|
104
|
|
|
$discriminatorFieldName = (string) $elem->attributes()->{'discriminator-field-name'}; |
105
|
|
|
$discriminatorMap = []; |
106
|
3 |
|
foreach ($elem->xpath('./discriminator-class') as $entry) { |
107
|
3 |
|
if (!isset($entry->attributes()->value)) { |
108
|
|
|
throw new InvalidMetadataException('Each discriminator-class element must have a "value" attribute.'); |
109
|
2 |
|
} |
110
|
|
|
|
111
|
|
|
$discriminatorMap[(string) $entry->attributes()->value] = (string) $entry; |
112
|
3 |
|
} |
113
|
|
|
|
114
|
|
|
if ('true' === (string) $elem->attributes()->{'discriminator-disabled'}) { |
115
|
30 |
|
$metadata->discriminatorDisabled = true; |
116
|
3 |
|
} elseif (!empty($discriminatorFieldName) || !empty($discriminatorMap)) { |
117
|
2 |
|
$discriminatorGroups = []; |
118
|
|
|
foreach ($elem->xpath('./discriminator-groups/group') as $entry) { |
119
|
3 |
|
$discriminatorGroups[] = (string) $entry; |
120
|
1 |
|
} |
121
|
|
|
|
122
|
3 |
|
$metadata->setDiscriminator($discriminatorFieldName, $discriminatorMap, $discriminatorGroups); |
123
|
3 |
|
} |
124
|
|
|
|
125
|
|
|
foreach ($elem->xpath('./xml-namespace') as $xmlNamespace) { |
126
|
|
|
if (!isset($xmlNamespace->attributes()->uri)) { |
127
|
30 |
|
throw new InvalidMetadataException('The prefix attribute must be set for all xml-namespace elements.'); |
128
|
|
|
} |
129
|
7 |
|
|
130
|
2 |
|
if (isset($xmlNamespace->attributes()->prefix)) { |
131
|
|
|
$prefix = (string) $xmlNamespace->attributes()->prefix; |
132
|
6 |
|
} else { |
133
|
|
|
$prefix = null; |
134
|
|
|
} |
135
|
6 |
|
|
136
|
|
|
$metadata->registerNamespace((string) $xmlNamespace->attributes()->uri, $prefix); |
137
|
|
|
} |
138
|
7 |
|
|
139
|
7 |
|
foreach ($elem->xpath('./xml-discriminator') as $xmlDiscriminator) { |
140
|
|
|
if (isset($xmlDiscriminator->attributes()->attribute)) { |
141
|
|
|
$metadata->xmlDiscriminatorAttribute = 'true' === (string) $xmlDiscriminator->attributes()->attribute; |
142
|
30 |
|
} |
143
|
|
|
|
144
|
30 |
|
if (isset($xmlDiscriminator->attributes()->cdata)) { |
145
|
25 |
|
$metadata->xmlDiscriminatorCData = 'true' === (string) $xmlDiscriminator->attributes()->cdata; |
146
|
2 |
|
} |
147
|
|
|
|
148
|
|
|
if (isset($xmlDiscriminator->attributes()->namespace)) { |
149
|
24 |
|
$metadata->xmlDiscriminatorNamespace = (string) $xmlDiscriminator->attributes()->namespace; |
150
|
24 |
|
} |
151
|
|
|
} |
152
|
24 |
|
|
153
|
|
|
foreach ($elem->xpath('./virtual-property') as $method) { |
154
|
|
|
if (isset($method->attributes()->expression)) { |
155
|
30 |
|
$virtualPropertyMetadata = new ExpressionPropertyMetadata( |
156
|
|
|
$name, |
157
|
26 |
|
(string) $method->attributes()->name, |
158
|
26 |
|
$this->parseExpression((string) $method->attributes()->expression), |
159
|
26 |
|
); |
160
|
|
|
} else { |
161
|
26 |
|
if (!isset($method->attributes()->method)) { |
162
|
26 |
|
throw new InvalidMetadataException('The method attribute must be set for all virtual-property elements.'); |
163
|
|
|
} |
164
|
26 |
|
|
165
|
2 |
|
$virtualPropertyMetadata = new VirtualPropertyMetadata($name, (string) $method->attributes()->method); |
166
|
|
|
} |
167
|
|
|
|
168
|
26 |
|
$propertiesMetadata[] = $virtualPropertyMetadata; |
169
|
2 |
|
$propertiesNodes[] = $method; |
170
|
|
|
} |
171
|
|
|
|
172
|
24 |
|
if (!$excludeAll) { |
173
|
2 |
|
foreach ($class->getProperties() as $property) { |
174
|
|
|
if ($property->class !== $name || (isset($property->info) && $property->info['class'] !== $name)) { |
175
|
|
|
continue; |
176
|
24 |
|
} |
177
|
1 |
|
|
178
|
|
|
$pName = $property->getName(); |
179
|
|
|
$propertiesMetadata[] = new PropertyMetadata($name, $pName); |
180
|
24 |
|
|
181
|
1 |
|
$pElems = $elem->xpath("./property[@name = '" . $pName . "']"); |
182
|
|
|
$propertiesNodes[] = $pElems ? reset($pElems) : null; |
183
|
|
|
} |
184
|
24 |
|
|
185
|
1 |
|
foreach ($propertiesMetadata as $propertyKey => $pMetadata) { |
186
|
1 |
|
$isExclude = false; |
187
|
|
|
$isExpose = $pMetadata instanceof VirtualPropertyMetadata |
188
|
|
|
|| $pMetadata instanceof ExpressionPropertyMetadata |
189
|
24 |
|
|| isset($propertiesNodes[$propertyKey]); |
190
|
|
|
|
191
|
|
|
$pElem = $propertiesNodes[$propertyKey]; |
192
|
|
|
if (!empty($pElem)) { |
193
|
24 |
|
if (null !== $exclude = $pElem->attributes()->exclude) { |
194
|
|
|
$isExclude = 'true' === strtolower((string) $exclude); |
195
|
|
|
} |
196
|
|
|
|
197
|
24 |
|
if ($isExclude) { |
198
|
6 |
|
continue; |
199
|
|
|
} |
200
|
|
|
|
201
|
24 |
|
if (null !== $expose = $pElem->attributes()->expose) { |
202
|
15 |
|
$isExpose = 'true' === strtolower((string) $expose); |
203
|
12 |
|
} |
204
|
4 |
|
|
205
|
|
|
if (null !== $excludeIf = $pElem->attributes()->{'exclude-if'}) { |
206
|
|
|
$pMetadata->excludeIf = $this->parseExpression((string) $excludeIf); |
207
|
24 |
|
} |
208
|
2 |
|
|
209
|
23 |
|
if (null !== $skip = $pElem->attributes()->{'skip-when-empty'}) { |
210
|
1 |
|
$pMetadata->skipWhenEmpty = 'true' === strtolower((string) $skip); |
211
|
|
|
} |
212
|
|
|
|
213
|
24 |
|
if (null !== $excludeIf = $pElem->attributes()->{'expose-if'}) { |
214
|
|
|
$pMetadata->excludeIf = $this->parseExpression('!(' . (string) $excludeIf . ')'); |
215
|
2 |
|
$isExpose = true; |
216
|
|
|
} |
217
|
2 |
|
|
218
|
2 |
|
if (null !== $version = $pElem->attributes()->{'since-version'}) { |
219
|
1 |
|
$pMetadata->sinceVersion = (string) $version; |
220
|
|
|
} |
221
|
|
|
|
222
|
2 |
|
if (null !== $version = $pElem->attributes()->{'until-version'}) { |
223
|
1 |
|
$pMetadata->untilVersion = (string) $version; |
224
|
|
|
} |
225
|
|
|
|
226
|
2 |
|
if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) { |
227
|
1 |
|
$pMetadata->serializedName = (string) $serializedName; |
228
|
|
|
} |
229
|
1 |
|
|
230
|
|
|
if (null !== $type = $pElem->attributes()->type) { |
231
|
|
|
$pMetadata->setType($this->typeParser->parse((string) $type)); |
232
|
2 |
|
} elseif (isset($pElem->type)) { |
233
|
|
|
$pMetadata->setType($this->typeParser->parse((string) $pElem->type)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (null !== $groups = $pElem->attributes()->groups) { |
237
|
24 |
|
$pMetadata->groups = preg_split('/\s*,\s*/', trim((string) $groups)); |
238
|
|
|
} elseif (isset($pElem->groups)) { |
239
|
|
|
$pMetadata->groups = (array) $pElem->groups->value; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if (isset($pElem->{'xml-list'})) { |
243
|
|
|
$pMetadata->xmlCollection = true; |
244
|
|
|
|
245
|
|
|
$colConfig = $pElem->{'xml-list'}; |
246
|
|
|
if (isset($colConfig->attributes()->inline)) { |
247
|
|
|
$pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (isset($colConfig->attributes()->{'entry-name'})) { |
251
|
|
|
$pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'}; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (isset($colConfig->attributes()->{'skip-when-empty'})) { |
255
|
|
|
$pMetadata->xmlCollectionSkipWhenEmpty = 'true' === (string) $colConfig->attributes()->{'skip-when-empty'}; |
256
|
|
|
} else { |
257
|
|
|
$pMetadata->xmlCollectionSkipWhenEmpty = true; |
258
|
24 |
|
} |
259
|
4 |
|
|
260
|
4 |
|
if (isset($colConfig->attributes()->namespace)) { |
261
|
2 |
|
$pMetadata->xmlEntryNamespace = (string) $colConfig->attributes()->namespace; |
262
|
|
|
} |
263
|
|
|
} |
264
|
4 |
|
|
265
|
3 |
|
if (isset($pElem->{'xml-map'})) { |
266
|
|
|
$pMetadata->xmlCollection = true; |
267
|
|
|
|
268
|
|
|
$colConfig = $pElem->{'xml-map'}; |
269
|
24 |
|
if (isset($colConfig->attributes()->inline)) { |
270
|
4 |
|
$pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline; |
271
|
|
|
} |
272
|
|
|
|
273
|
24 |
|
if (isset($colConfig->attributes()->{'entry-name'})) { |
274
|
|
|
$pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'}; |
275
|
|
|
} |
276
|
|
|
|
277
|
24 |
|
if (isset($colConfig->attributes()->namespace)) { |
278
|
2 |
|
$pMetadata->xmlEntryNamespace = (string) $colConfig->attributes()->namespace; |
279
|
|
|
} |
280
|
|
|
|
281
|
24 |
|
if (isset($colConfig->attributes()->{'key-attribute-name'})) { |
282
|
1 |
|
$pMetadata->xmlKeyAttribute = (string) $colConfig->attributes()->{'key-attribute-name'}; |
283
|
|
|
} |
284
|
|
|
} |
285
|
24 |
|
|
286
|
1 |
|
if (isset($pElem->{'xml-element'})) { |
287
|
|
|
$colConfig = $pElem->{'xml-element'}; |
288
|
|
|
if (isset($colConfig->attributes()->cdata)) { |
289
|
|
|
$pMetadata->xmlElementCData = 'true' === (string) $colConfig->attributes()->cdata; |
290
|
24 |
|
} |
291
|
1 |
|
|
292
|
|
|
if (isset($colConfig->attributes()->namespace)) { |
293
|
23 |
|
$pMetadata->xmlNamespace = (string) $colConfig->attributes()->namespace; |
294
|
|
|
} |
295
|
|
|
} |
296
|
24 |
|
|
297
|
24 |
|
if (isset($pElem->attributes()->{'xml-attribute'})) { |
298
|
24 |
|
$pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'}; |
299
|
24 |
|
} |
300
|
24 |
|
|
301
|
24 |
|
if (isset($pElem->attributes()->{'xml-attribute-map'})) { |
302
|
|
|
$pMetadata->xmlAttributeMap = 'true' === (string) $pElem->attributes()->{'xml-attribute-map'}; |
303
|
|
|
} |
304
|
24 |
|
|
305
|
2 |
|
if (isset($pElem->attributes()->{'xml-value'})) { |
306
|
|
|
$pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'}; |
307
|
|
|
} |
308
|
|
|
|
309
|
26 |
|
if (isset($pElem->attributes()->{'xml-key-value-pairs'})) { |
310
|
2 |
|
$pMetadata->xmlKeyValuePairs = 'true' === (string) $pElem->attributes()->{'xml-key-value-pairs'}; |
311
|
2 |
|
} |
312
|
|
|
|
313
|
|
|
if (isset($pElem->attributes()->{'max-depth'})) { |
314
|
26 |
|
$pMetadata->maxDepth = (int) $pElem->attributes()->{'max-depth'}; |
315
|
26 |
|
} |
316
|
|
|
|
317
|
|
|
//we need read-only before setter and getter set, because that method depends on flag being set |
318
|
26 |
|
if (null !== $readOnly = $pElem->attributes()->{'read-only'}) { |
319
|
21 |
|
$pMetadata->readOnly = 'true' === strtolower((string) $readOnly); |
320
|
|
|
} else { |
321
|
|
|
$pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass; |
322
|
26 |
|
} |
323
|
26 |
|
|
324
|
|
|
$getter = $pElem->attributes()->{'accessor-getter'}; |
325
|
|
|
$setter = $pElem->attributes()->{'accessor-setter'}; |
326
|
|
|
$pMetadata->setAccessor( |
327
|
26 |
|
(string) ($pElem->attributes()->{'access-type'} ?: $classAccessType), |
328
|
|
|
$getter ? (string) $getter : null, |
329
|
|
|
$setter ? (string) $setter : null, |
330
|
|
|
); |
331
|
|
|
|
332
|
30 |
|
if (null !== $inline = $pElem->attributes()->inline) { |
333
|
|
|
$pMetadata->inline = 'true' === strtolower((string) $inline); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
if ($pMetadata->inline) { |
338
|
|
|
$metadata->isList = $metadata->isList || PropertyMetadata::isCollectionList($pMetadata->type); |
339
|
|
|
$metadata->isMap = $metadata->isMap || PropertyMetadata::isCollectionMap($pMetadata->type); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if (!$pMetadata->serializedName) { |
343
|
|
|
$pMetadata->serializedName = $this->namingStrategy->translateName($pMetadata); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if (!empty($pElem) && null !== $name = $pElem->attributes()->name) { |
347
|
|
|
$pMetadata->name = (string) $name; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if ( |
351
|
|
|
(ExclusionPolicy::NONE === (string) $exclusionPolicy && !$isExclude) |
352
|
|
|
|| (ExclusionPolicy::ALL === (string) $exclusionPolicy && $isExpose) |
353
|
|
|
) { |
354
|
|
|
$metadata->addPropertyMetadata($pMetadata); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
foreach ($elem->xpath('./callback-method') as $method) { |
360
|
|
|
if (!isset($method->attributes()->type)) { |
361
|
|
|
throw new InvalidMetadataException('The type attribute must be set for all callback-method elements.'); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
if (!isset($method->attributes()->name)) { |
365
|
|
|
throw new InvalidMetadataException('The name attribute must be set for all callback-method elements.'); |
366
|
|
|
} |
367
|
|
|
|
368
|
30 |
|
switch ((string) $method->attributes()->type) { |
369
|
|
|
case 'pre-serialize': |
370
|
|
|
$metadata->addPreSerializeMethod(new MethodMetadata($class->name, (string) $method->attributes()->name)); |
371
|
30 |
|
break; |
372
|
|
|
|
373
|
30 |
|
case 'post-serialize': |
374
|
|
|
$metadata->addPostSerializeMethod(new MethodMetadata($class->name, (string) $method->attributes()->name)); |
375
|
|
|
break; |
376
|
|
|
|
377
|
|
|
case 'post-deserialize': |
378
|
|
|
$metadata->addPostDeserializeMethod(new MethodMetadata($class->name, (string) $method->attributes()->name)); |
379
|
|
|
break; |
380
|
|
|
|
381
|
|
|
case 'handler': |
382
|
|
|
if (!isset($method->attributes()->format)) { |
383
|
|
|
throw new InvalidMetadataException('The format attribute must be set for "handler" callback methods.'); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
if (!isset($method->attributes()->direction)) { |
387
|
|
|
throw new InvalidMetadataException('The direction attribute must be set for "handler" callback methods.'); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
break; |
391
|
|
|
|
392
|
|
|
default: |
393
|
|
|
throw new InvalidMetadataException(sprintf('The type "%s" is not supported.', $method->attributes()->name)); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return $metadata; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
protected function getExtension(): string |
401
|
|
|
{ |
402
|
|
|
return 'xml'; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|