Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Serializer\Encoder;
|
Chris@0
|
13
|
Chris@14
|
14 use Symfony\Component\Serializer\Exception\NotEncodableValueException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Encodes XML data.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Jordi Boggiano <j.boggiano@seld.be>
|
Chris@0
|
20 * @author John Wards <jwards@whiteoctober.co.uk>
|
Chris@0
|
21 * @author Fabian Vogler <fabian@equivalence.ch>
|
Chris@0
|
22 * @author Kévin Dunglas <dunglas@gmail.com>
|
Chris@0
|
23 */
|
Chris@0
|
24 class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
|
Chris@0
|
25 {
|
Chris@14
|
26 const FORMAT = 'xml';
|
Chris@14
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @var \DOMDocument
|
Chris@0
|
30 */
|
Chris@0
|
31 private $dom;
|
Chris@0
|
32 private $format;
|
Chris@0
|
33 private $context;
|
Chris@0
|
34 private $rootNodeName = 'response';
|
Chris@0
|
35 private $loadOptions;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Construct new XmlEncoder and allow to change the root node element name.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param string $rootNodeName
|
Chris@0
|
41 * @param int|null $loadOptions A bit field of LIBXML_* constants
|
Chris@0
|
42 */
|
Chris@0
|
43 public function __construct($rootNodeName = 'response', $loadOptions = null)
|
Chris@0
|
44 {
|
Chris@0
|
45 $this->rootNodeName = $rootNodeName;
|
Chris@0
|
46 $this->loadOptions = null !== $loadOptions ? $loadOptions : LIBXML_NONET | LIBXML_NOBLANKS;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * {@inheritdoc}
|
Chris@0
|
51 */
|
Chris@17
|
52 public function encode($data, $format, array $context = [])
|
Chris@0
|
53 {
|
Chris@0
|
54 if ($data instanceof \DOMDocument) {
|
Chris@0
|
55 return $data->saveXML();
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 $xmlRootNodeName = $this->resolveXmlRootName($context);
|
Chris@0
|
59
|
Chris@0
|
60 $this->dom = $this->createDomDocument($context);
|
Chris@0
|
61 $this->format = $format;
|
Chris@0
|
62 $this->context = $context;
|
Chris@0
|
63
|
Chris@0
|
64 if (null !== $data && !is_scalar($data)) {
|
Chris@0
|
65 $root = $this->dom->createElement($xmlRootNodeName);
|
Chris@0
|
66 $this->dom->appendChild($root);
|
Chris@0
|
67 $this->buildXml($root, $data, $xmlRootNodeName);
|
Chris@0
|
68 } else {
|
Chris@0
|
69 $this->appendNode($this->dom, $data, $xmlRootNodeName);
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 return $this->dom->saveXML();
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * {@inheritdoc}
|
Chris@0
|
77 */
|
Chris@17
|
78 public function decode($data, $format, array $context = [])
|
Chris@0
|
79 {
|
Chris@0
|
80 if ('' === trim($data)) {
|
Chris@14
|
81 throw new NotEncodableValueException('Invalid XML data, it can not be empty.');
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 $internalErrors = libxml_use_internal_errors(true);
|
Chris@0
|
85 $disableEntities = libxml_disable_entity_loader(true);
|
Chris@0
|
86 libxml_clear_errors();
|
Chris@0
|
87
|
Chris@0
|
88 $dom = new \DOMDocument();
|
Chris@0
|
89 $dom->loadXML($data, $this->loadOptions);
|
Chris@0
|
90
|
Chris@0
|
91 libxml_use_internal_errors($internalErrors);
|
Chris@0
|
92 libxml_disable_entity_loader($disableEntities);
|
Chris@0
|
93
|
Chris@0
|
94 if ($error = libxml_get_last_error()) {
|
Chris@0
|
95 libxml_clear_errors();
|
Chris@0
|
96
|
Chris@14
|
97 throw new NotEncodableValueException($error->message);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 $rootNode = null;
|
Chris@0
|
101 foreach ($dom->childNodes as $child) {
|
Chris@14
|
102 if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
|
Chris@14
|
103 throw new NotEncodableValueException('Document types are not allowed.');
|
Chris@0
|
104 }
|
Chris@14
|
105 if (!$rootNode && XML_PI_NODE !== $child->nodeType) {
|
Chris@0
|
106 $rootNode = $child;
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 // todo: throw an exception if the root node name is not correctly configured (bc)
|
Chris@0
|
111
|
Chris@0
|
112 if ($rootNode->hasChildNodes()) {
|
Chris@0
|
113 $xpath = new \DOMXPath($dom);
|
Chris@17
|
114 $data = [];
|
Chris@0
|
115 foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) {
|
Chris@0
|
116 $data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 unset($data['@xmlns:xml']);
|
Chris@0
|
120
|
Chris@0
|
121 if (empty($data)) {
|
Chris@14
|
122 return $this->parseXml($rootNode, $context);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@14
|
125 return array_merge($data, (array) $this->parseXml($rootNode, $context));
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 if (!$rootNode->hasAttributes()) {
|
Chris@0
|
129 return $rootNode->nodeValue;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@17
|
132 $data = [];
|
Chris@0
|
133
|
Chris@0
|
134 foreach ($rootNode->attributes as $attrKey => $attr) {
|
Chris@0
|
135 $data['@'.$attrKey] = $attr->nodeValue;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 $data['#'] = $rootNode->nodeValue;
|
Chris@0
|
139
|
Chris@0
|
140 return $data;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * {@inheritdoc}
|
Chris@0
|
145 */
|
Chris@0
|
146 public function supportsEncoding($format)
|
Chris@0
|
147 {
|
Chris@14
|
148 return self::FORMAT === $format;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * {@inheritdoc}
|
Chris@0
|
153 */
|
Chris@0
|
154 public function supportsDecoding($format)
|
Chris@0
|
155 {
|
Chris@14
|
156 return self::FORMAT === $format;
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Sets the root node name.
|
Chris@0
|
161 *
|
Chris@14
|
162 * @param string $name Root node name
|
Chris@0
|
163 */
|
Chris@0
|
164 public function setRootNodeName($name)
|
Chris@0
|
165 {
|
Chris@0
|
166 $this->rootNodeName = $name;
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * Returns the root node name.
|
Chris@0
|
171 *
|
Chris@0
|
172 * @return string
|
Chris@0
|
173 */
|
Chris@0
|
174 public function getRootNodeName()
|
Chris@0
|
175 {
|
Chris@0
|
176 return $this->rootNodeName;
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 /**
|
Chris@0
|
180 * @param \DOMNode $node
|
Chris@0
|
181 * @param string $val
|
Chris@0
|
182 *
|
Chris@0
|
183 * @return bool
|
Chris@0
|
184 */
|
Chris@0
|
185 final protected function appendXMLString(\DOMNode $node, $val)
|
Chris@0
|
186 {
|
Chris@14
|
187 if (\strlen($val) > 0) {
|
Chris@0
|
188 $frag = $this->dom->createDocumentFragment();
|
Chris@0
|
189 $frag->appendXML($val);
|
Chris@0
|
190 $node->appendChild($frag);
|
Chris@0
|
191
|
Chris@0
|
192 return true;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 return false;
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 /**
|
Chris@0
|
199 * @param \DOMNode $node
|
Chris@0
|
200 * @param string $val
|
Chris@0
|
201 *
|
Chris@0
|
202 * @return bool
|
Chris@0
|
203 */
|
Chris@0
|
204 final protected function appendText(\DOMNode $node, $val)
|
Chris@0
|
205 {
|
Chris@0
|
206 $nodeText = $this->dom->createTextNode($val);
|
Chris@0
|
207 $node->appendChild($nodeText);
|
Chris@0
|
208
|
Chris@0
|
209 return true;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * @param \DOMNode $node
|
Chris@0
|
214 * @param string $val
|
Chris@0
|
215 *
|
Chris@0
|
216 * @return bool
|
Chris@0
|
217 */
|
Chris@0
|
218 final protected function appendCData(\DOMNode $node, $val)
|
Chris@0
|
219 {
|
Chris@0
|
220 $nodeText = $this->dom->createCDATASection($val);
|
Chris@0
|
221 $node->appendChild($nodeText);
|
Chris@0
|
222
|
Chris@0
|
223 return true;
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * @param \DOMNode $node
|
Chris@0
|
228 * @param \DOMDocumentFragment $fragment
|
Chris@0
|
229 *
|
Chris@0
|
230 * @return bool
|
Chris@0
|
231 */
|
Chris@0
|
232 final protected function appendDocumentFragment(\DOMNode $node, $fragment)
|
Chris@0
|
233 {
|
Chris@0
|
234 if ($fragment instanceof \DOMDocumentFragment) {
|
Chris@0
|
235 $node->appendChild($fragment);
|
Chris@0
|
236
|
Chris@0
|
237 return true;
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 return false;
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 /**
|
Chris@0
|
244 * Checks the name is a valid xml element name.
|
Chris@0
|
245 *
|
Chris@0
|
246 * @param string $name
|
Chris@0
|
247 *
|
Chris@0
|
248 * @return bool
|
Chris@0
|
249 */
|
Chris@0
|
250 final protected function isElementNameValid($name)
|
Chris@0
|
251 {
|
Chris@0
|
252 return $name &&
|
Chris@0
|
253 false === strpos($name, ' ') &&
|
Chris@0
|
254 preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Parse the input DOMNode into an array or a string.
|
Chris@0
|
259 *
|
Chris@0
|
260 * @return array|string
|
Chris@0
|
261 */
|
Chris@17
|
262 private function parseXml(\DOMNode $node, array $context = [])
|
Chris@0
|
263 {
|
Chris@14
|
264 $data = $this->parseXmlAttributes($node, $context);
|
Chris@0
|
265
|
Chris@14
|
266 $value = $this->parseXmlValue($node, $context);
|
Chris@0
|
267
|
Chris@14
|
268 if (!\count($data)) {
|
Chris@0
|
269 return $value;
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@14
|
272 if (!\is_array($value)) {
|
Chris@0
|
273 $data['#'] = $value;
|
Chris@0
|
274
|
Chris@0
|
275 return $data;
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@14
|
278 if (1 === \count($value) && key($value)) {
|
Chris@0
|
279 $data[key($value)] = current($value);
|
Chris@0
|
280
|
Chris@0
|
281 return $data;
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 foreach ($value as $key => $val) {
|
Chris@0
|
285 $data[$key] = $val;
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 return $data;
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 /**
|
Chris@0
|
292 * Parse the input DOMNode attributes into an array.
|
Chris@0
|
293 *
|
Chris@0
|
294 * @return array
|
Chris@0
|
295 */
|
Chris@17
|
296 private function parseXmlAttributes(\DOMNode $node, array $context = [])
|
Chris@0
|
297 {
|
Chris@0
|
298 if (!$node->hasAttributes()) {
|
Chris@17
|
299 return [];
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@17
|
302 $data = [];
|
Chris@14
|
303 $typeCastAttributes = $this->resolveXmlTypeCastAttributes($context);
|
Chris@0
|
304
|
Chris@0
|
305 foreach ($node->attributes as $attr) {
|
Chris@14
|
306 if (!is_numeric($attr->nodeValue) || !$typeCastAttributes) {
|
Chris@0
|
307 $data['@'.$attr->nodeName] = $attr->nodeValue;
|
Chris@0
|
308
|
Chris@0
|
309 continue;
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 if (false !== $val = filter_var($attr->nodeValue, FILTER_VALIDATE_INT)) {
|
Chris@0
|
313 $data['@'.$attr->nodeName] = $val;
|
Chris@0
|
314
|
Chris@0
|
315 continue;
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 $data['@'.$attr->nodeName] = (float) $attr->nodeValue;
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 return $data;
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 /**
|
Chris@0
|
325 * Parse the input DOMNode value (content and children) into an array or a string.
|
Chris@0
|
326 *
|
Chris@0
|
327 * @return array|string
|
Chris@0
|
328 */
|
Chris@17
|
329 private function parseXmlValue(\DOMNode $node, array $context = [])
|
Chris@0
|
330 {
|
Chris@0
|
331 if (!$node->hasChildNodes()) {
|
Chris@0
|
332 return $node->nodeValue;
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@17
|
335 if (1 === $node->childNodes->length && \in_array($node->firstChild->nodeType, [XML_TEXT_NODE, XML_CDATA_SECTION_NODE])) {
|
Chris@0
|
336 return $node->firstChild->nodeValue;
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@17
|
339 $value = [];
|
Chris@0
|
340
|
Chris@0
|
341 foreach ($node->childNodes as $subnode) {
|
Chris@14
|
342 if (XML_PI_NODE === $subnode->nodeType) {
|
Chris@0
|
343 continue;
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@14
|
346 $val = $this->parseXml($subnode, $context);
|
Chris@0
|
347
|
Chris@0
|
348 if ('item' === $subnode->nodeName && isset($val['@key'])) {
|
Chris@0
|
349 if (isset($val['#'])) {
|
Chris@0
|
350 $value[$val['@key']] = $val['#'];
|
Chris@0
|
351 } else {
|
Chris@0
|
352 $value[$val['@key']] = $val;
|
Chris@0
|
353 }
|
Chris@0
|
354 } else {
|
Chris@0
|
355 $value[$subnode->nodeName][] = $val;
|
Chris@0
|
356 }
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@0
|
359 foreach ($value as $key => $val) {
|
Chris@14
|
360 if (\is_array($val) && 1 === \count($val)) {
|
Chris@0
|
361 $value[$key] = current($val);
|
Chris@0
|
362 }
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 return $value;
|
Chris@0
|
366 }
|
Chris@0
|
367
|
Chris@0
|
368 /**
|
Chris@0
|
369 * Parse the data and convert it to DOMElements.
|
Chris@0
|
370 *
|
Chris@0
|
371 * @param \DOMNode $parentNode
|
Chris@0
|
372 * @param array|object $data
|
Chris@0
|
373 * @param string|null $xmlRootNodeName
|
Chris@0
|
374 *
|
Chris@0
|
375 * @return bool
|
Chris@0
|
376 *
|
Chris@14
|
377 * @throws NotEncodableValueException
|
Chris@0
|
378 */
|
Chris@0
|
379 private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
|
Chris@0
|
380 {
|
Chris@0
|
381 $append = true;
|
Chris@0
|
382
|
Chris@14
|
383 if (\is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
|
Chris@0
|
384 foreach ($data as $key => $data) {
|
Chris@0
|
385 //Ah this is the magic @ attribute types.
|
Chris@0
|
386 if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
Chris@0
|
387 if (!is_scalar($data)) {
|
Chris@0
|
388 $data = $this->serializer->normalize($data, $this->format, $this->context);
|
Chris@0
|
389 }
|
Chris@0
|
390 $parentNode->setAttribute($attributeName, $data);
|
Chris@14
|
391 } elseif ('#' === $key) {
|
Chris@0
|
392 $append = $this->selectNodeType($parentNode, $data);
|
Chris@14
|
393 } elseif (\is_array($data) && false === is_numeric($key)) {
|
Chris@0
|
394 // Is this array fully numeric keys?
|
Chris@0
|
395 if (ctype_digit(implode('', array_keys($data)))) {
|
Chris@0
|
396 /*
|
Chris@0
|
397 * Create nodes to append to $parentNode based on the $key of this array
|
Chris@0
|
398 * Produces <xml><item>0</item><item>1</item></xml>
|
Chris@17
|
399 * From ["item" => [0,1]];.
|
Chris@0
|
400 */
|
Chris@0
|
401 foreach ($data as $subData) {
|
Chris@0
|
402 $append = $this->appendNode($parentNode, $subData, $key);
|
Chris@0
|
403 }
|
Chris@0
|
404 } else {
|
Chris@0
|
405 $append = $this->appendNode($parentNode, $data, $key);
|
Chris@0
|
406 }
|
Chris@0
|
407 } elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
|
Chris@0
|
408 $append = $this->appendNode($parentNode, $data, 'item', $key);
|
Chris@14
|
409 } elseif (null !== $data || !isset($this->context['remove_empty_tags']) || false === $this->context['remove_empty_tags']) {
|
Chris@0
|
410 $append = $this->appendNode($parentNode, $data, $key);
|
Chris@0
|
411 }
|
Chris@0
|
412 }
|
Chris@0
|
413
|
Chris@0
|
414 return $append;
|
Chris@0
|
415 }
|
Chris@0
|
416
|
Chris@14
|
417 if (\is_object($data)) {
|
Chris@0
|
418 $data = $this->serializer->normalize($data, $this->format, $this->context);
|
Chris@0
|
419 if (null !== $data && !is_scalar($data)) {
|
Chris@0
|
420 return $this->buildXml($parentNode, $data, $xmlRootNodeName);
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 // top level data object was normalized into a scalar
|
Chris@0
|
424 if (!$parentNode->parentNode->parentNode) {
|
Chris@0
|
425 $root = $parentNode->parentNode;
|
Chris@0
|
426 $root->removeChild($parentNode);
|
Chris@0
|
427
|
Chris@0
|
428 return $this->appendNode($root, $data, $xmlRootNodeName);
|
Chris@0
|
429 }
|
Chris@0
|
430
|
Chris@0
|
431 return $this->appendNode($parentNode, $data, 'data');
|
Chris@0
|
432 }
|
Chris@0
|
433
|
Chris@14
|
434 throw new NotEncodableValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
|
Chris@0
|
435 }
|
Chris@0
|
436
|
Chris@0
|
437 /**
|
Chris@0
|
438 * Selects the type of node to create and appends it to the parent.
|
Chris@0
|
439 *
|
Chris@0
|
440 * @param \DOMNode $parentNode
|
Chris@0
|
441 * @param array|object $data
|
Chris@0
|
442 * @param string $nodeName
|
Chris@0
|
443 * @param string $key
|
Chris@0
|
444 *
|
Chris@0
|
445 * @return bool
|
Chris@0
|
446 */
|
Chris@0
|
447 private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null)
|
Chris@0
|
448 {
|
Chris@0
|
449 $node = $this->dom->createElement($nodeName);
|
Chris@0
|
450 if (null !== $key) {
|
Chris@0
|
451 $node->setAttribute('key', $key);
|
Chris@0
|
452 }
|
Chris@0
|
453 $appendNode = $this->selectNodeType($node, $data);
|
Chris@0
|
454 // we may have decided not to append this node, either in error or if its $nodeName is not valid
|
Chris@0
|
455 if ($appendNode) {
|
Chris@0
|
456 $parentNode->appendChild($node);
|
Chris@0
|
457 }
|
Chris@0
|
458
|
Chris@0
|
459 return $appendNode;
|
Chris@0
|
460 }
|
Chris@0
|
461
|
Chris@0
|
462 /**
|
Chris@0
|
463 * Checks if a value contains any characters which would require CDATA wrapping.
|
Chris@0
|
464 *
|
Chris@0
|
465 * @param string $val
|
Chris@0
|
466 *
|
Chris@0
|
467 * @return bool
|
Chris@0
|
468 */
|
Chris@0
|
469 private function needsCdataWrapping($val)
|
Chris@0
|
470 {
|
Chris@0
|
471 return 0 < preg_match('/[<>&]/', $val);
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 /**
|
Chris@0
|
475 * Tests the value being passed and decide what sort of element to create.
|
Chris@0
|
476 *
|
Chris@0
|
477 * @param \DOMNode $node
|
Chris@0
|
478 * @param mixed $val
|
Chris@0
|
479 *
|
Chris@0
|
480 * @return bool
|
Chris@0
|
481 *
|
Chris@14
|
482 * @throws NotEncodableValueException
|
Chris@0
|
483 */
|
Chris@0
|
484 private function selectNodeType(\DOMNode $node, $val)
|
Chris@0
|
485 {
|
Chris@14
|
486 if (\is_array($val)) {
|
Chris@0
|
487 return $this->buildXml($node, $val);
|
Chris@0
|
488 } elseif ($val instanceof \SimpleXMLElement) {
|
Chris@0
|
489 $child = $this->dom->importNode(dom_import_simplexml($val), true);
|
Chris@0
|
490 $node->appendChild($child);
|
Chris@0
|
491 } elseif ($val instanceof \Traversable) {
|
Chris@0
|
492 $this->buildXml($node, $val);
|
Chris@14
|
493 } elseif (\is_object($val)) {
|
Chris@0
|
494 return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
|
Chris@0
|
495 } elseif (is_numeric($val)) {
|
Chris@0
|
496 return $this->appendText($node, (string) $val);
|
Chris@14
|
497 } elseif (\is_string($val) && $this->needsCdataWrapping($val)) {
|
Chris@0
|
498 return $this->appendCData($node, $val);
|
Chris@14
|
499 } elseif (\is_string($val)) {
|
Chris@0
|
500 return $this->appendText($node, $val);
|
Chris@14
|
501 } elseif (\is_bool($val)) {
|
Chris@0
|
502 return $this->appendText($node, (int) $val);
|
Chris@0
|
503 } elseif ($val instanceof \DOMNode) {
|
Chris@0
|
504 $child = $this->dom->importNode($val, true);
|
Chris@0
|
505 $node->appendChild($child);
|
Chris@0
|
506 }
|
Chris@0
|
507
|
Chris@0
|
508 return true;
|
Chris@0
|
509 }
|
Chris@0
|
510
|
Chris@0
|
511 /**
|
Chris@0
|
512 * Get real XML root node name, taking serializer options into account.
|
Chris@0
|
513 *
|
Chris@0
|
514 * @return string
|
Chris@0
|
515 */
|
Chris@17
|
516 private function resolveXmlRootName(array $context = [])
|
Chris@0
|
517 {
|
Chris@0
|
518 return isset($context['xml_root_node_name'])
|
Chris@0
|
519 ? $context['xml_root_node_name']
|
Chris@0
|
520 : $this->rootNodeName;
|
Chris@0
|
521 }
|
Chris@0
|
522
|
Chris@0
|
523 /**
|
Chris@14
|
524 * Get XML option for type casting attributes Defaults to true.
|
Chris@14
|
525 *
|
Chris@14
|
526 * @param array $context
|
Chris@14
|
527 *
|
Chris@14
|
528 * @return bool
|
Chris@14
|
529 */
|
Chris@17
|
530 private function resolveXmlTypeCastAttributes(array $context = [])
|
Chris@14
|
531 {
|
Chris@14
|
532 return isset($context['xml_type_cast_attributes'])
|
Chris@14
|
533 ? (bool) $context['xml_type_cast_attributes']
|
Chris@14
|
534 : true;
|
Chris@14
|
535 }
|
Chris@14
|
536
|
Chris@14
|
537 /**
|
Chris@0
|
538 * Create a DOM document, taking serializer options into account.
|
Chris@0
|
539 *
|
Chris@14
|
540 * @param array $context Options that the encoder has access to
|
Chris@0
|
541 *
|
Chris@0
|
542 * @return \DOMDocument
|
Chris@0
|
543 */
|
Chris@0
|
544 private function createDomDocument(array $context)
|
Chris@0
|
545 {
|
Chris@0
|
546 $document = new \DOMDocument();
|
Chris@0
|
547
|
Chris@0
|
548 // Set an attribute on the DOM document specifying, as part of the XML declaration,
|
Chris@17
|
549 $xmlOptions = [
|
Chris@0
|
550 // nicely formats output with indentation and extra space
|
Chris@0
|
551 'xml_format_output' => 'formatOutput',
|
Chris@0
|
552 // the version number of the document
|
Chris@0
|
553 'xml_version' => 'xmlVersion',
|
Chris@0
|
554 // the encoding of the document
|
Chris@0
|
555 'xml_encoding' => 'encoding',
|
Chris@0
|
556 // whether the document is standalone
|
Chris@0
|
557 'xml_standalone' => 'xmlStandalone',
|
Chris@17
|
558 ];
|
Chris@0
|
559 foreach ($xmlOptions as $xmlOption => $documentProperty) {
|
Chris@0
|
560 if (isset($context[$xmlOption])) {
|
Chris@0
|
561 $document->$documentProperty = $context[$xmlOption];
|
Chris@0
|
562 }
|
Chris@0
|
563 }
|
Chris@0
|
564
|
Chris@0
|
565 return $document;
|
Chris@0
|
566 }
|
Chris@0
|
567 }
|