|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSchema\XML; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use DOMNodeList; |
|
9
|
|
|
use SimpleSAML\XML\Assert\Assert; |
|
10
|
|
|
use SimpleSAML\XML\Constants as C; |
|
11
|
|
|
use SimpleSAML\XML\ExtendableAttributesTrait; |
|
12
|
|
|
use SimpleSAML\XML\SchemaValidatableElementInterface; |
|
13
|
|
|
use SimpleSAML\XML\SchemaValidatableElementTrait; |
|
14
|
|
|
use SimpleSAML\XML\Type\LangValue; |
|
15
|
|
|
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
|
16
|
|
|
use SimpleSAML\XMLSchema\Type\AnyURIValue; |
|
17
|
|
|
use SimpleSAML\XMLSchema\XML\Constants\NS; |
|
18
|
|
|
|
|
19
|
|
|
use function strval; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class representing the documentation element |
|
23
|
|
|
* |
|
24
|
|
|
* @package simplesamlphp/xml-common |
|
25
|
|
|
*/ |
|
26
|
|
|
final class Documentation extends AbstractXsElement implements SchemaValidatableElementInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use ExtendableAttributesTrait; |
|
|
|
|
|
|
29
|
|
|
use SchemaValidatableElementTrait; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
public const LOCALNAME = 'documentation'; |
|
34
|
|
|
|
|
35
|
|
|
/** The namespace-attribute for the xs:anyAttribute element */ |
|
36
|
|
|
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER; |
|
37
|
|
|
|
|
38
|
|
|
/** The exclusions for the xs:anyAttribute element */ |
|
39
|
|
|
public const XS_ANY_ATTR_EXCLUSIONS = [ |
|
40
|
|
|
[C::NS_XML, 'lang'], |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Documentation constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @param \DOMNodeList<\DOMNode> $content |
|
48
|
|
|
* @param \SimpleSAML\XML\Type\LangValue|null $lang |
|
49
|
|
|
* @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $source |
|
50
|
|
|
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes |
|
51
|
|
|
*/ |
|
52
|
|
|
final public function __construct( |
|
53
|
|
|
protected DOMNodeList $content, |
|
54
|
|
|
protected ?LangValue $lang = null, |
|
55
|
|
|
protected ?AnyURIValue $source = null, |
|
56
|
|
|
array $namespacedAttributes = [], |
|
57
|
|
|
) { |
|
58
|
|
|
Assert::lessThanEq($content->count(), C::UNBOUNDED_LIMIT); |
|
59
|
|
|
$this->setAttributesNS($namespacedAttributes); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the content property. |
|
65
|
|
|
* |
|
66
|
|
|
* @return \DOMNodeList<\DOMNode> |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getContent(): DOMNodeList |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->content; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the lang property. |
|
76
|
|
|
* |
|
77
|
|
|
* @return \SimpleSAML\XML\Type\LangValue|null |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getLang(): ?LangValue |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->lang; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the source property. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getSource(): ?AnyURIValue |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->source; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Test if an object, at the state it's in, would produce an empty XML-element |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function isEmptyElement(): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->getContent()->count() === 0 |
|
104
|
|
|
&& empty($this->getLang()) |
|
105
|
|
|
&& empty($this->getSource()) |
|
106
|
|
|
&& empty($this->getAttributesNS()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Create an instance of this object from its XML representation. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \DOMElement $xml |
|
114
|
|
|
* @return static |
|
115
|
|
|
* |
|
116
|
|
|
* @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
|
117
|
|
|
* if the qualified name of the supplied element is wrong |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function fromXML(DOMElement $xml): static |
|
120
|
|
|
{ |
|
121
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
|
122
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
123
|
|
|
|
|
124
|
|
|
$lang = $xml->hasAttributeNS(C::NS_XML, 'lang') |
|
125
|
|
|
? LangValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang')) |
|
126
|
|
|
: null; |
|
127
|
|
|
|
|
128
|
|
|
return new static( |
|
129
|
|
|
$xml->childNodes, |
|
130
|
|
|
$lang, |
|
131
|
|
|
self::getOptionalAttribute($xml, 'source', AnyURIValue::class, null), |
|
132
|
|
|
self::getAttributesNSFromXML($xml), |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Add this Documentation to an XML element. |
|
139
|
|
|
* |
|
140
|
|
|
* @param \DOMElement|null $parent The element we should append this Documentation to. |
|
141
|
|
|
* @return \DOMElement |
|
142
|
|
|
*/ |
|
143
|
|
|
public function toXML(?DOMElement $parent = null): DOMElement |
|
144
|
|
|
{ |
|
145
|
|
|
$e = parent::instantiateParentElement($parent); |
|
146
|
|
|
|
|
147
|
|
|
if ($this->getSource() !== null) { |
|
148
|
|
|
$e->setAttribute('source', strval($this->getSource())); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->getLang()?->toAttribute()->toXML($e); |
|
152
|
|
|
|
|
153
|
|
|
foreach ($this->getAttributesNS() as $attr) { |
|
154
|
|
|
$attr->toXML($e); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
foreach ($this->getContent() as $i) { |
|
158
|
|
|
$e->appendChild($e->ownerDocument->importNode($i, true)); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $e; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|