<commit_before><?php
namespace CfdiUtils\Nodes;

use DOMDocument;
use DOMElement;
use SimpleXMLElement;

class XmlNodeUtils
{
    public static function nodeToXmlElement(NodeInterface $node, DOMDocument $document = null): DOMElement
    {
        return (new XmlNodeExporter($document))->export($node);
    }

    public static function nodeToXmlString(NodeInterface $node, DOMDocument $document = null): string
    {
        $element = static::nodeToXmlElement($node, $document);
        return $element->ownerDocument->saveXML($element);
    }

    public static function nodeToSimpleXmlElement(NodeInterface $node, DOMDocument $document = null): SimpleXMLElement
    {
        $element = static::nodeToXmlElement($node, $document);
        $simpleXmlElement = simplexml_import_dom($element);
        if (false === $simpleXmlElement) {
            throw new \InvalidArgumentException('Cannot convert to SimpleXmlElement');
        }
        return $simpleXmlElement;
    }

    public static function nodeFromXmlElement(DOMElement $element): NodeInterface
    {
        return (new XmlNodeImporter())->import($element);
    }

    public static function nodeFromXmlString(string $content): NodeInterface
    {
        $document = new DOMDocument();
        $document->formatOutput = true;
        $document->preserveWhiteSpace = false;
        $document->loadXML($content);
        return static::nodeFromXmlElement($document->documentElement);
    }

    public static function nodeFromSimpleXmlElement(SimpleXMLElement $element): NodeInterface
    {
        return static::nodeFromXmlString((string) $element->asXML());
    }
}<commit_msg>Throw exceptions in nodeFromXmlString if content is empty or error loading xml<commit_after><?php
namespace CfdiUtils\Nodes;

use DOMDocument;
use DOMElement;
use SimpleXMLElement;

class XmlNodeUtils
{
    public static function nodeToXmlElement(NodeInterface $node, DOMDocument $document = null): DOMElement
    {
        return (new XmlNodeExporter($document))->export($node);
    }

    public static function nodeToXmlString(NodeInterface $node, DOMDocument $document = null): string
    {
        $element = static::nodeToXmlElement($node, $document);
        return $element->ownerDocument->saveXML($element);
    }

    public static function nodeToSimpleXmlElement(NodeInterface $node, DOMDocument $document = null): SimpleXMLElement
    {
        $element = static::nodeToXmlElement($node, $document);
        $simpleXmlElement = simplexml_import_dom($element);
        if (false === $simpleXmlElement) {
            throw new \InvalidArgumentException('Cannot convert to SimpleXmlElement');
        }
        return $simpleXmlElement;
    }

    public static function nodeFromXmlElement(DOMElement $element): NodeInterface
    {
        return (new XmlNodeImporter())->import($element);
    }

    public static function nodeFromXmlString(string $content): NodeInterface
    {
        if ('' == $content) {
            throw new \UnexpectedValueException('Content argument cannot be empty');
        }
        $document = new DOMDocument();
        $document->formatOutput = true;
        $document->preserveWhiteSpace = false;
        // this error silenced call is intentional, no need to alter libxml_use_internal_errors
        if (false === @$document->loadXML($content)) {
            throw new \UnexpectedValueException('Cannot create a DOM Document from content');
        }
        return static::nodeFromXmlElement($document->documentElement);
    }

    public static function nodeFromSimpleXmlElement(SimpleXMLElement $element): NodeInterface
    {
        return static::nodeFromXmlString((string) $element->asXML());
    }
}
{'fraction_non_alphanumeric': 0.10100189547793122, 'fraction_numerical': 0.0, 'mean_word_length': 3.6348808030112925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3502106', 'n_tokens_mistral': 951, 'n_tokens_neox': 921, 'n_words': 235}