Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
namespace CfdiUtils\Validate;
4
5
use CfdiUtils\CadenaOrigen\DOMBuilder;
6
use CfdiUtils\CadenaOrigen\XsltBuilderInterface;
7
use CfdiUtils\CadenaOrigen\XsltBuilderPropertyTrait;
8
use CfdiUtils\Nodes\NodeInterface;
9
use CfdiUtils\Nodes\XmlNodeUtils;
10
use CfdiUtils\XmlResolver\XmlResolver;
11
use CfdiUtils\XmlResolver\XmlResolverPropertyTrait;
12
13
trait CfdiValidatorTrait
14
{
15
    use XmlResolverPropertyTrait;
16
    use XsltBuilderPropertyTrait;
17
18
    abstract protected function createVersionedMultiValidator(): MultiValidator;
19
20
    /**
21
     * This class uses a default XmlResolver if not provided or null.
22
     * If you really want to remove the XmlResolver then use the method setXmlResolver after construction.
23
     *
24
     * @param XmlResolver|null $xmlResolver
25
     * @param XsltBuilderInterface|null $xsltBuilder
26
     */
27 23
    public function __construct(XmlResolver $xmlResolver = null, XsltBuilderInterface $xsltBuilder = null)
28
    {
29 23
        $this->setXmlResolver($xmlResolver ?: new XmlResolver());
30 23
        $this->setXsltBuilder($xsltBuilder ?: new DOMBuilder());
31
    }
32
33
    /**
34
     * Validate and return the asserts from the validation process.
35
     * This method can use a xml string and a NodeInterface,
36
     * is your responsibility that the node is the representation of the content.
37
     *
38
     * @param string $xmlString
39
     * @param NodeInterface $node
40
     * @return Asserts|Assert[]
41
     */
42 19
    public function validate(string $xmlString, NodeInterface $node): Asserts
43
    {
44 19
        if ('' === $xmlString) {
45 2
            throw new \UnexpectedValueException('The xml string to validate cannot be empty');
46
        }
47
48 17
        $validator = $this->createVersionedMultiValidator();
49
50 17
        $hydrater = new Hydrater();
51 17
        $hydrater->setXmlString($xmlString);
52 17
        $hydrater->setXmlResolver(($this->hasXmlResolver()) ? $this->getXmlResolver() : null);
53 17
        $hydrater->setXsltBuilder($this->getXsltBuilder());
54 17
        $validator->hydrate($hydrater);
55
56 17
        $asserts = new Asserts();
57 17
        $validator->validate($node, $asserts);
58
59 17
        return $asserts;
60
    }
61
62
    /**
63
     * Validate and return the asserts from the validation process based on a xml string
64
     *
65
     * @param string $xmlString
66
     * @return Asserts|Assert[]
67
     */
68 11
    public function validateXml(string $xmlString): Asserts
69
    {
70 11
        return $this->validate($xmlString, XmlNodeUtils::nodeFromXmlString($xmlString));
71
    }
72
73
    /**
74
     * Validate and return the asserts from the validation process based on a node interface object
75
     *
76
     * @param NodeInterface $node
77
     * @return Asserts|Assert[]
78
     */
79 2
    public function validateNode(NodeInterface $node): Asserts
80
    {
81 2
        return $this->validate(XmlNodeUtils::nodeToXmlString($node), $node);
82
    }
83
}
84