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