<commit_before><?php
/**
 * This file is part of the Hal library
 *
 * (c) <NAME> <<EMAIL>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package Nocarrier
 */
namespace Nocarrier;

/**
 * HalXmlRenderer
 *
 * @uses HalRenderer
 * @package Nocarrier
 * @author <NAME> <<EMAIL>>
 */
class HalXmlRenderer implements HalRenderer
{
    /**
     * render
     *
     * @param Hal $resource
     * @param bool $pretty
     * @return string
     */
    public function render(Hal $resource, $pretty)
    {
        $doc = new \SimpleXMLElement('<resource></resource>');
        $doc->addAttribute('href', $resource->getUri());
        $this->linksForXml($doc, $resource->getLinks());

        $this->arrayToXml($resource->getData(), $doc);

        foreach($resource->getResources() as $rel => $resources) {
            $this->resourcesForXml($doc, $rel, $resources);
        }

        $dom = dom_import_simplexml($doc);
        if ($pretty) {
            $dom->ownerDocument->preserveWhiteSpace = false;
            $dom->ownerDocument->formatOutput = true;
        }
        return $dom->ownerDocument->saveXML();
    }
    /**
     * linksForXml
     * Add links in hal+xml format to a SimpleXmlElement object
     *
     * @param SimpleXmlElement $doc
     * @param array $links
     * @return void
     */
    protected function linksForXml(\SimpleXmlElement $doc, array $links)
    {
        foreach($links as $rel => $links) {
            foreach ($links as $link) {
                $element = $doc->addChild('link');
                $element->addAttribute('rel', $rel);
                $element->addAttribute('href', $link['uri']);
                if (!is_null($link['title'])) {
                    $element->addAttribute('title', $link['title']);
                }
                foreach ($link['attributes'] as $attribute => $value) {
                    $element->addAttribute($attribute, $value);
                }
            }
        }
    }

    /**
     * arrayToXml
     *
     * @param array $data
     * @param SimpleXmlElement $element
     * @param mixed $parent
     * @access protected
     * @return void
     */
    protected function arrayToXml(array $data, \SimpleXmlElement $element, $parent=null) {
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                if (!is_numeric($key)) {
                    if (count($value) > 0 && isset($value[0])) {
                        $this->arrayToXml($value, $element, $key);
                    } else {
                        $subnode = $element->addChild($key);
                        $this->arrayToXml($value, $subnode, $key);
                    }
                } else {
                    $subnode = $element->addChild($parent);
                    $this->arrayToXml($value, $subnode, $parent);
                }
            } else {
                if (!is_numeric($key)) {
                    if (substr($key, 0, 1) === '@') {
                        $element->addAttribute(substr($key, 1), $value);
                    } else {
                        $element->addChild($key, $value);
                    }
                } else {
                    $element->addChild($parent, $value);
                }
            }
        }
    }

    /**
     * resourcesForXml
     * Add resources in hal+xml format (identified by $rel) to a SimpleXmlElement object
     *
     * @param SimpleXmlElement $doc
     * @param mixed $rel
     * @param array $resources
     */
    protected function resourcesForXml(\SimpleXmlElement $doc, $rel, array $resources)
    {
        foreach($resources as $resource) {
            $element = $doc->addChild('resource');<commit_msg>Encoded the value when adding child nodes in the XmlRenderer<commit_after><?php
/**
 * This file is part of the Hal library
 *
 * (c) <NAME> <<EMAIL>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package Nocarrier
 */
namespace Nocarrier;

/**
 * HalXmlRenderer
 *
 * @uses HalRenderer
 * @package Nocarrier
 * @author <NAME> <<EMAIL>.uk>
 */
class HalXmlRenderer implements HalRenderer
{
    /**
     * render
     *
     * @param Hal $resource
     * @param bool $pretty
     * @return string
     */
    public function render(Hal $resource, $pretty)
    {
        $doc = new \SimpleXMLElement('<resource></resource>');
        $doc->addAttribute('href', $resource->getUri());
        $this->linksForXml($doc, $resource->getLinks());

        $this->arrayToXml($resource->getData(), $doc);

        foreach($resource->getResources() as $rel => $resources) {
            $this->resourcesForXml($doc, $rel, $resources);
        }

        $dom = dom_import_simplexml($doc);
        if ($pretty) {
            $dom->ownerDocument->preserveWhiteSpace = false;
            $dom->ownerDocument->formatOutput = true;
        }
        return $dom->ownerDocument->saveXML();
    }
    /**
     * linksForXml
     * Add links in hal+xml format to a SimpleXmlElement object
     *
     * @param SimpleXmlElement $doc
     * @param array $links
     * @return void
     */
    protected function linksForXml(\SimpleXmlElement $doc, array $links)
    {
        foreach($links as $rel => $links) {
            foreach ($links as $link) {
                $element = $doc->addChild('link');
                $element->addAttribute('rel', $rel);
                $element->addAttribute('href', $link['uri']);
                if (!is_null($link['title'])) {
                    $element->addAttribute('title', $link['title']);
                }
                foreach ($link['attributes'] as $attribute => $value) {
                    $element->addAttribute($attribute, $value);
                }
            }
        }
    }

    /**
     * arrayToXml
     *
     * @param array $data
     * @param SimpleXmlElement $element
     * @param mixed $parent
     * @access protected
     * @return void
     */
    protected function arrayToXml(array $data, \SimpleXmlElement $element, $parent=null) {
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                if (!is_numeric($key)) {
                    if (count($value) > 0 && isset($value[0])) {
                        $this->arrayToXml($value, $element, $key);
                    } else {
                        $subnode = $element->addChild($key);
                        $this->arrayToXml($value, $subnode, $key);
                    }
                } else {
                    $subnode = $element->addChild($parent);
                    $this->arrayToXml($value, $subnode, $parent);
                }
            } else {
                if (!is_numeric($key)) {
                    if (substr($key, 0, 1) === '@') {
                        $element->addAttribute(substr($key, 1), $value);
                    } else {
                        $element->addChild($key, htmlentities($value));
                    }
                } else {
                    $element->addChild($parent, htmlentities($value));
                }
            }
        }
    }

    /**
     * resourcesForXml
     * Add resources in hal+xml format (identified by $rel) to a SimpleXmlElement object
     *
     * @param SimpleXmlElement $doc
     * @param mixed $rel
     * @param array $resources
     */
    protected function resourcesForXml(\SimpleXmlElement $doc, $rel, array $resources)
    {
        foreach($resources as $resource) {
            $element = $doc->addChild('resource');
{'fraction_non_alphanumeric': 0.1403623949579832, 'fraction_numerical': 0.0013130252100840337, 'mean_word_length': 1.7468445726649837, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '500575', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2015, 'n_words': 478}