private function XmlEncoder::buildXml

Parse the data and convert it to DOMElements

Parameters

DOMNode $parentNode:

array|object $data data:

Return value

Boolean

2 calls to XmlEncoder::buildXml()
XmlEncoder::encode in drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Encodes data into the given format
XmlEncoder::selectNodeType in drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Tests the value being passed and decide what sort of element to create

File

drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Encoder/XmlEncoder.php, line 268

Class

XmlEncoder
Encodes XML data

Namespace

Symfony\Component\Serializer\Encoder

Code

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));
}