Symfony\Component\Serializer\Encoder\XmlEncoder::buildXml PHP Méthode

buildXml() private méthode

Parse the data and convert it to DOMElements.
private buildXml ( DOMNode $parentNode, array | object $data, string | null $xmlRootNodeName = null ) : boolean
$parentNode DOMNode
$data array | object
$xmlRootNodeName string | null
Résultat boolean
    private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
    {
        $append = true;
        if (is_array($data) || $data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format)) {
            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);
                } elseif (null !== $data || !isset($this->context['remove_empty_tags']) || false === $this->context['remove_empty_tags']) {
                    $append = $this->appendNode($parentNode, $data, $key);
                }
            }
            return $append;
        }
        if (is_object($data)) {
            $data = $this->serializer->normalize($data, $this->format, $this->context);
            if (null !== $data && !is_scalar($data)) {
                return $this->buildXml($parentNode, $data, $xmlRootNodeName);
            }
            // top level data object was normalized into a scalar
            if (!$parentNode->parentNode->parentNode) {
                $root = $parentNode->parentNode;
                $root->removeChild($parentNode);
                return $this->appendNode($root, $data, $xmlRootNodeName);
            }
            return $this->appendNode($parentNode, $data, 'data');
        }
        throw new UnexpectedValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
    }