-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathChunk.php
More file actions
275 lines (228 loc) · 6.88 KB
/
Copy pathChunk.php
File metadata and controls
275 lines (228 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
declare(strict_types=1);
namespace SimpleSAML\XML;
use Dom;
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\SerializableElementTrait;
use SimpleSAML\XMLSchema\Exception\MissingAttributeException;
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface;
use SimpleSAML\XMLSchema\Type\StringValue;
/**
* Serializable class used to hold an XML element.
*
* @package simplesamlphp/xml-common
*/
final class Chunk implements
SerializableElementInterface,
ElementInterface
{
use SerializableElementTrait;
/**
* Whether the element may be normalized
*
* @var bool $normalization
*/
protected bool $normalization = true;
/**
* The localName of the element.
*
* @var string
*/
protected string $localName;
/**
* The namespaceURI of this element.
*
* @var string|null
*/
protected ?string $namespaceURI;
/**
* The prefix of this element.
*
* @var string
*/
protected string $prefix;
/**
* Create an XML Chunk from a copy of the given \DOMElement.
*
* @param \Dom\Element $xml The element we should copy.
*/
public function __construct(
protected Dom\Element $xml,
) {
$this->setLocalName($xml->localName);
$this->setNamespaceURI($xml->namespaceURI);
$this->setPrefix($xml->prefix);
}
/**
* Collect the value of the normalization-property
*/
public function getNormalization(): bool
{
return $this->normalization;
}
/**
* Set the value of the normalization-property
*
* @param bool $normalization
*/
public function setNormalization(bool $normalization): void
{
$this->normalization = $normalization;
}
/**
* Collect the value of the localName-property
*/
public function getLocalName(): string
{
return $this->localName;
}
/**
* Set the value of the localName-property
*
* @param string $localName
* @throws \SimpleSAML\Assert\AssertionFailedException if $localName is an empty string
*/
public function setLocalName(string $localName): void
{
Assert::validNCName($localName, SchemaViolationException::class); // Covers the empty string
$this->localName = $localName;
}
/**
* Collect the value of the namespaceURI-property
*/
public function getNamespaceURI(): ?string
{
return $this->namespaceURI;
}
/**
* Set the value of the namespaceURI-property
*
* @param string|null $namespaceURI
*/
protected function setNamespaceURI(?string $namespaceURI = null): void
{
Assert::nullOrValidURI($namespaceURI, SchemaViolationException::class);
$this->namespaceURI = $namespaceURI;
}
/**
* Get this \Dom\Element.
*/
public function getXML(): Dom\Element
{
return $this->xml;
}
/**
* Collect the value of the prefix-property
*/
public function getPrefix(): string
{
return $this->prefix;
}
/**
* Set the value of the prefix-property
*
* @param string|null $prefix
*/
protected function setPrefix(?string $prefix = null): void
{
$this->prefix = strval($prefix);
}
/**
* Get the XML qualified name (prefix:name, or just name when not prefixed)
* of the element represented by this class.
*/
public function getQualifiedName(): string
{
$prefix = $this->getPrefix();
if (empty($prefix)) {
return $this->getLocalName();
} else {
return $prefix . ':' . $this->getLocalName();
}
}
/**
* Get the value of an attribute from a given element.
*
* @template T of \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
* @param \Dom\Element $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param class-string<T> $type The type of the attribute value.
* @return T
*
* @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException if the attribute is missing from the element
*/
public static function getAttribute(
Dom\Element $xml,
string $name,
string $type = StringValue::class,
): ValueTypeInterface {
Assert::isAOf($type, ValueTypeInterface::class);
Assert::true(
$xml->hasAttribute($name),
'Missing \'' . $name . '\' attribute on ' . $xml->prefix . ':' . $xml->localName . '.',
MissingAttributeException::class,
);
$value = $xml->getAttribute($name);
return $type::fromString($value);
}
/**
* Get the value of an attribute from a given element.
*
* @template T of \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
* @param \Dom\Element $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param class-string<T> $type The type of the attribute value.
* @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface|null $default
* The default to return in case the attribute does not exist and it is optional.
* @return ($default is \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface ? T : T|null)
*/
public static function getOptionalAttribute(
Dom\Element $xml,
string $name,
string $type = StringValue::class,
?ValueTypeInterface $default = null,
): ?ValueTypeInterface {
if (!$xml->hasAttribute($name)) {
return $default;
}
return self::getAttribute($xml, $name, $type);
}
/**
* Test if an object, at the state it's in, would produce an empty XML-element
*/
public function isEmptyElement(): bool
{
/** @var \Dom\Element $xml */
$xml = $this->getXML();
return ($xml->childNodes->length === 0) && ($xml->attributes->count() === 0);
}
/**
* @param \Dom\Element $xml
*/
public static function fromXML(Dom\Element $xml): static
{
return new static($xml);
}
/**
* Append this XML element to a different XML element.
*
* @param \Dom\Element|null $parent The element we should append this element to.
* @return \Dom\Element The new element.
*/
public function toXML(?Dom\Element $parent = null): Dom\Element
{
if ($parent === null) {
$doc = DOMDocumentFactory::create();
} else {
$doc = $parent->ownerDocument;
Assert::notNull($doc);
}
if ($parent === null) {
$parent = $doc;
}
$parent->appendChild($doc->importNode($this->getXML(), true));
return $doc->documentElement;
}
}