<commit_before><?php
/**
 * Created by PhpStorm.
 * User: giansalex
 * Date: 30/01/2019
 * Time: 10:01.
 */

namespace Greenter\Ws\Reader;

/**
 * Class XmlReader.
 */
class XmlReader
{
    /**
     * Prefix namespace.
     */
    const ROOT_PREFIX = 'x';

    /**
     * @var \DOMXPath
     */
    private $xpath;

    /**
     * @return \DOMXPath
     */
    public function getXpath()
    {
        return $this->xpath;
    }

    /**
     * @var string
     */
    private $rootNs;

    /**
     * Load document from XML.
     *
     * @param string $content
     *
     * @return \DOMDocument
     */
    public function getDocument($content)
    {
        $doc = new \DOMDocument();
        $doc->loadXML($content);

        return $doc;
    }

    /**
     * Parse value to document.
     *
     * @param \DOMDocument|string $value
     *
     * @return \DOMDocument
     */
    public function parseToDocument($value)
    {
        if ($value instanceof \DOMDocument) {
            return $value;
        }

        return $this->getDocument($value);
    }

    /**
     * Init XPath.
     *
     * @param \DOMDocument|string $value
     */
    public function loadXpath($value)
    {
        $doc = $this->parseToDocument($value);

        $this->loadXpathFromDoc($doc);
    }

    /**
     * Init XPath from document.
     *
     * @param \DOMDocument $doc
     */
    public function loadXpathFromDoc(\DOMDocument $doc)
    {
        $docName = $doc->documentElement->nodeName;
        $this->rootNs = '/'.self::ROOT_PREFIX.':'.$docName;
        $this->xpath = new \DOMXPath($doc);
        $this->xpath->registerNamespace(self::ROOT_PREFIX, $doc->documentElement->namespaceURI);
    }

    /***
     * Obtiene el primer valor del nodo.
     *
     * @param string $query Relativo al root namespace
     * @return null|string
     */
    public function getValue($query)
    {
        $nodes = $this->xpath->query($this->rootNs.'/'.$query);
        if ($nodes->length > 0) {
            return $nodes->item(0)->nodeValue;
        }

        return null;
    }
}<commit_msg>add get root<commit_after><?php
/**
 * Created by PhpStorm.
 * User: giansalex
 * Date: 30/01/2019
 * Time: 10:01.
 */

namespace Greenter\Ws\Reader;

/**
 * Class XmlReader.
 */
class XmlReader
{
    /**
     * Prefix namespace.
     */
    const ROOT_PREFIX = 'x';

    /**
     * @var \DOMXPath
     */
    private $xpath;
    /**
     * @var string
     */
    private $root;

    /**
     * Get Root Prefix.
     *
     * @return string
     */
    public function getRoot()
    {
        return $this->root;
    }

    /**
     * @return \DOMXPath
     */
    public function getXpath()
    {
        return $this->xpath;
    }

    /**
     * Load document from XML.
     *
     * @param string $content
     *
     * @return \DOMDocument
     */
    public function getDocument($content)
    {
        $doc = new \DOMDocument();
        $doc->loadXML($content);

        return $doc;
    }

    /**
     * Parse value to document.
     *
     * @param \DOMDocument|string $value
     *
     * @return \DOMDocument
     */
    public function parseToDocument($value)
    {
        if ($value instanceof \DOMDocument) {
            return $value;
        }

        return $this->getDocument($value);
    }

    /**
     * Init XPath.
     *
     * @param \DOMDocument|string $value
     */
    public function loadXpath($value)
    {
        $doc = $this->parseToDocument($value);

        $this->loadXpathFromDoc($doc);
    }

    /**
     * Init XPath from document.
     *
     * @param \DOMDocument $doc
     */
    public function loadXpathFromDoc(\DOMDocument $doc)
    {
        $docName = $doc->documentElement->nodeName;
        $this->root = '/'.self::ROOT_PREFIX.':'.$docName;
        $this->xpath = new \DOMXPath($doc);
        $this->xpath->registerNamespace(self::ROOT_PREFIX, $doc->documentElement->namespaceURI);
    }

    /***
     * Get value from first node result.
     *
     * @param string $query relative to root namespace
     * @return null|string
     */
    public function getValue($query)
    {
        $nodes = $this->xpath->query($this->root.'/'.$query);
        if ($nodes->length > 0) {
            return $nodes->item(0)->nodeValue;
        }

        return null;
    }
}
{'fraction_non_alphanumeric': 0.14844113541181944, 'fraction_numerical': 0.006514657980456026, 'mean_word_length': 1.7557692307692307, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6599440', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1351, 'n_words': 276}