<commit_before><?php
namespace Consolidation\OutputFormatters\Transformations;

use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface;
use Consolidation\OutputFormatters\StructuredData\Xml\XmlSchema;

/**
 * Simplify a DOMDocument to an array.
 */
class DomToArraySimplifier implements SimplifyToArrayInterface
{
    public function __construct()
    {
    }

    /**
     * @param ReflectionClass $dataType
     */
    public function canSimplify(\ReflectionClass $dataType)
    {
        return
            $dataType->isSubclassOf('\Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface') ||
            $dataType->isSubclassOf('DOMDocument') ||
            ($dataType->getName() == 'DOMDocument');
    }

    public function simplifyToArray($structuredData, FormatterOptions $options)
    {
        if ($structuredData instanceof DomDataInterface) {
            $structuredData = $structuredData->getDomData();
        }
        if ($structuredData instanceof \DOMDocument) {
            // $schema = $options->getXmlSchema();
            $simplified = $this->elementToArray($structuredData);
            $structuredData = array_shift($simplified);
        }
        return $structuredData;
    }

    /**
     * Recursively convert the provided DOM element into a php array.
     *
     * @param \DOMNode $element
     * @return array
     */
    protected function elementToArray(\DOMNode $element)
    {
        if ($element->nodeType == XML_TEXT_NODE) {
            return $element->nodeValue;
        }
        $attributes = $this->getNodeAttributes($element);
        $children = $this->getNodeChildren($element);

        return array_merge($attributes, $children);
    }

    /**
     * Get all of the attributes of the provided element.
     *
     * @param \DOMNode $element
     * @return array
     */
    protected function getNodeAttributes($element)
    {
        if (empty($element->attributes)) {
            return [];
        }
        $attributes = [];
        foreach ($element->attributes as $key => $attribute) {
            $attributes[$key] = $attribute->nodeValue;
        }
        return $attributes;
    }

    /**
     * Get all of the children of the provided element, with simplification.
     *
     * @param \DOMNode $element
     * @return array
     */
    protected function getNodeChildren($element)
    {
        if (empty($element->childNodes)) {
            return [];
        }
        $uniformChildrenName = $this->hasUniformChildren($element);
        if ("{$uniformChildrenName}s" == $element->nodeName) {
            $result = $this->getUniformChildren($element->nodeName, $element);
        } else {
            $result = $this->getUniqueChildren($element->nodeName, $element);
        }
        return array_filter($result);
    }

    /**
     * Get the data from the children of the provided node in preliminary
     * form.
     *
     * @param \DOMNode $element
     * @return array<commit_msg>Also Check for plurals ending in ‘es’<commit_after><?php
namespace Consolidation\OutputFormatters\Transformations;

use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface;
use Consolidation\OutputFormatters\StructuredData\Xml\XmlSchema;

/**
 * Simplify a DOMDocument to an array.
 */
class DomToArraySimplifier implements SimplifyToArrayInterface
{
    public function __construct()
    {
    }

    /**
     * @param ReflectionClass $dataType
     */
    public function canSimplify(\ReflectionClass $dataType)
    {
        return
            $dataType->isSubclassOf('\Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface') ||
            $dataType->isSubclassOf('DOMDocument') ||
            ($dataType->getName() == 'DOMDocument');
    }

    public function simplifyToArray($structuredData, FormatterOptions $options)
    {
        if ($structuredData instanceof DomDataInterface) {
            $structuredData = $structuredData->getDomData();
        }
        if ($structuredData instanceof \DOMDocument) {
            // $schema = $options->getXmlSchema();
            $simplified = $this->elementToArray($structuredData);
            $structuredData = array_shift($simplified);
        }
        return $structuredData;
    }

    /**
     * Recursively convert the provided DOM element into a php array.
     *
     * @param \DOMNode $element
     * @return array
     */
    protected function elementToArray(\DOMNode $element)
    {
        if ($element->nodeType == XML_TEXT_NODE) {
            return $element->nodeValue;
        }
        $attributes = $this->getNodeAttributes($element);
        $children = $this->getNodeChildren($element);

        return array_merge($attributes, $children);
    }

    /**
     * Get all of the attributes of the provided element.
     *
     * @param \DOMNode $element
     * @return array
     */
    protected function getNodeAttributes($element)
    {
        if (empty($element->attributes)) {
            return [];
        }
        $attributes = [];
        foreach ($element->attributes as $key => $attribute) {
            $attributes[$key] = $attribute->nodeValue;
        }
        return $attributes;
    }

    /**
     * Get all of the children of the provided element, with simplification.
     *
     * @param \DOMNode $element
     * @return array
     */
    protected function getNodeChildren($element)
    {
        if (empty($element->childNodes)) {
            return [];
        }
        $uniformChildrenName = $this->hasUniformChildren($element);
        // Check for plurals.
        if (in_array($element->nodeName, ["{$uniformChildrenName}s", "{$uniformChildrenName}es"])) {
            $result = $this->getUniformChildren($element->nodeName, $element);
        } else {
            $result = $this->getUniqueChildren($element->nodeName, $element);
        }
        return array_filter($result);
    }

    /**
     * Get the data from the children of the provided node in preliminary
     * form.
     *
     * @param \DOMNode $element
     * @return array
{'fraction_non_alphanumeric': 0.11600777705767984, 'fraction_numerical': 0.0, 'mean_word_length': 2.4836343115124153, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '4254326', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1613, 'n_words': 353}