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