Passed
Push — master ( faa376...c7ca4c )
by Tim
17:29 queued 14:52
created

Selector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 88
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\IDValue;
15
use SimpleSAML\XMLSchema\Type\StringValue;
16
17
use function strval;
18
19
/**
20
 * Class representing the selector-element.
21
 *
22
 * @package simplesamlphp/xml-common
23
 */
24
final class Selector extends AbstractAnnotated implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
27
28
29
    public const string LOCALNAME = 'selector';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 29 at column 24
Loading history...
30
31
32
    public static string $selector_regex = '/^
33
        (
34
            (\.\/\/)?
35
            (((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)
36
            (\/(((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.))*
37
            (\|(\.\/\/)?(((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.)
38
            (\/(((child::)?(([_:A-Za-z][-._:A-Za-z0-9]*:)?([_:A-Za-z][-._:A-Za-z0-9]*|\*)))|\.))*)*
39
        )$/Dx';
40
41
42
    /**
43
     * Selector constructor
44
     *
45
     * @param \SimpleSAML\XMLSchema\Type\StringValue $xpath
46
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
47
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
48
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
49
     */
50
    public function __construct(
51
        protected StringValue $xpath,
52
        ?Annotation $annotation = null,
53
        ?IDValue $id = null,
54
        array $namespacedAttributes = [],
55
    ) {
56
        Assert::regex(strval($xpath), self::$selector_regex, SchemaViolationException::class);
57
58
        parent::__construct($annotation, $id, $namespacedAttributes);
59
    }
60
61
62
    /**
63
     * Collect the value of the xpath-property
64
     *
65
     * @return \SimpleSAML\XMLSchema\Type\StringValue
66
     */
67
    public function getXPath(): StringValue
68
    {
69
        return $this->xpath;
70
    }
71
72
73
    /**
74
     * Add this Selector to an XML element.
75
     *
76
     * @param \DOMElement|null $parent The element we should append this selector to.
77
     * @return \DOMElement
78
     */
79
    public function toXML(?DOMElement $parent = null): DOMElement
80
    {
81
        $e = parent::toXML($parent);
82
        $e->setAttribute('xpath', strval($this->getXPath()));
83
84
        return $e;
85
    }
86
87
88
    /**
89
     * Create an instance of this object from its XML representation.
90
     *
91
     * @param \DOMElement $xml
92
     * @return static
93
     *
94
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
95
     *   if the qualified name of the supplied element is wrong
96
     */
97
    public static function fromXML(DOMElement $xml): static
98
    {
99
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
100
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
101
102
        $annotation = Annotation::getChildrenOfClass($xml);
103
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
104
105
        return new static(
106
            self::getAttribute($xml, 'xpath', StringValue::class),
107
            array_pop($annotation),
108
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
109
            self::getAttributesNSFromXML($xml),
110
        );
111
    }
112
}
113