Parse the data and convert it to DOMElements
DOMNode $parentNode:
array|object $data data:
Boolean
private function buildXml($parentNode, $data) {
$append = true;
if (is_array($data) || $data instanceof \Traversable) {
foreach ($data as $key => $data) {
//Ah this is the magic @ attribute types.
if (0 === strpos($key, "@") && is_scalar($data) && $this
->isElementNameValid($attributeName = substr($key, 1))) {
$parentNode
->setAttribute($attributeName, $data);
}
elseif ($key === '#') {
$append = $this
->selectNodeType($parentNode, $data);
}
elseif (is_array($data) && false === is_numeric($key)) {
/**
* Is this array fully numeric keys?
*/
if (ctype_digit(implode('', array_keys($data)))) {
/**
* Create nodes to append to $parentNode based on the $key of this array
* Produces <xml><item>0</item><item>1</item></xml>
* From array("item" => array(0,1));
*/
foreach ($data as $subData) {
$append = $this
->appendNode($parentNode, $subData, $key);
}
}
else {
$append = $this
->appendNode($parentNode, $data, $key);
}
}
elseif (is_numeric($key) || !$this
->isElementNameValid($key)) {
$append = $this
->appendNode($parentNode, $data, "item", $key);
}
else {
$append = $this
->appendNode($parentNode, $data, $key);
}
}
return $append;
}
if (is_object($data)) {
$data = $this->serializer
->normalize($data, $this->format);
if (null !== $data && !is_scalar($data)) {
return $this
->buildXml($parentNode, $data);
}
// top level data object was normalized into a scalar
if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode;
$root
->removeChild($parentNode);
return $this
->appendNode($root, $data, $this->rootNodeName);
}
return $this
->appendNode($parentNode, $data, 'data');
}
throw new UnexpectedValueException('An unexpected value could not be serialized: ' . var_export($data, true));
}