<commit_before><?php
/**
 * Created by PhpStorm.
 * User: Administrador
 * Date: 24/01/2018
 * Time: 10:14 AM
 */

namespace Tests\Greenter\Xml\Builder;
use Greenter\Ubl\SchemaValidator;
use Greenter\Ubl\SchemaValidatorInterface;

/**
 * Trait XsdValidatorTrait
 * @method assertTrue($state)
 */
trait XsdValidatorTrait
{

    public function assertSchema($xml, $version = '2.0')
    {
        $validator = $this->getValidator($version);

        $success = $validator->validate($xml);

        if ($success === false) {
            echo $validator->getMessage().PHP_EOL;
        }

        $this->assertTrue($success);
    }

    public function assertSchemaV21($xml)
    {
        $doc = new \DOMDocument();
        $doc->loadXML($xml);
        $items = $doc->getElementsByTagNameNS('urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2','ExtensionContent');

        if ($items->length > 0) {
            $node = $doc->createElement('sign', '');
            $items->item(0)->appendChild($node);
            $xml = $doc->saveXML();
        }

        $this->assertSchema($xml, '2.1');
    }

    /**
     * @param string $version
     * @return SchemaValidatorInterface
     */
    private function getValidator($version = '2.0')
    {
        $validator = new SchemaValidator();
        $validator->setVersion($version);

        return $validator;
    }
}<commit_msg>assert ubl schema from xml ubl version ID<commit_after><?php
/**
 * Created by PhpStorm.
 * User: Administrador
 * Date: 24/01/2018
 * Time: 10:14 AM
 */

namespace Tests\Greenter\Xml\Builder;
use Greenter\Ubl\SchemaValidator;
use Greenter\Ubl\SchemaValidatorInterface;

/**
 * Trait XsdValidatorTrait
 * @method assertTrue($state)
 */
trait XsdValidatorTrait
{
    private $CbcNs = 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2';
    private $ExtNs = 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2';

    public function assertSchema($xml)
    {
        $version = $this->getUblVersion($xml);
        if ($version === '2.1') {
            $xml = $this->getFillSignNode($xml);
        }

        $validator = $this->getValidator($version);

        $success = $validator->validate($xml);

        if ($success === false) {
            echo $validator->getMessage().PHP_EOL;
        }

        $this->assertTrue($success);
    }

    public function getFillSignNode($xml)
    {
        $doc = new \DOMDocument();
        $doc->loadXML($xml);
        $items = $doc->getElementsByTagNameNS($this->ExtNs,'ExtensionContent');

        if ($items->length === 0) {
            return $xml;
        }

        $node = $doc->createElement('sign', '');
        $items->item(0)->appendChild($node);
        return $doc->saveXML();
    }

    /**
     * @param string $version
     * @return SchemaValidatorInterface
     */
    private function getValidator($version = '2.0')
    {
        $validator = new SchemaValidator();
        $validator->setVersion($version);

        return $validator;
    }

    private function getUblVersion($xml)
    {
        $doc = new \DOMDocument();
        $doc->loadXML($xml);
        $items = $doc->getElementsByTagNameNS($this->CbcNs,'UBLVersionID');

        return $items->length === 0 ? '2.0' : $items->item(0)->textContent;
    }
}
{'fraction_non_alphanumeric': 0.1573373676248109, 'fraction_numerical': 0.01422087745839637, 'mean_word_length': 2.697986577181208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 17}, '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': '5446122', 'n_tokens_mistral': 1027, 'n_tokens_neox': 1003, 'n_words': 176}