-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAbstractElement.php
More file actions
242 lines (203 loc) · 7.23 KB
/
AbstractElement.php
File metadata and controls
242 lines (203 loc) · 7.23 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
<?php
declare(strict_types=1);
namespace SimpleSAML\XML;
use DOMElement;
use RuntimeException;
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\SerializableElementTrait;
use SimpleSAML\XMLSchema\Exception\MissingAttributeException;
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface;
use SimpleSAML\XMLSchema\Type\QNameValue;
use SimpleSAML\XMLSchema\Type\StringValue;
use function array_slice;
use function defined;
use function explode;
use function join;
use function strval;
/**
* Abstract class to be implemented by all classes
*
* @package simplesamlphp/xml-common
*/
abstract class AbstractElement implements
SerializableElementInterface,
ElementInterface
{
use SerializableElementTrait;
/**
* Create a document structure for this element
*
* @param \DOMElement|null $parent The element we should append to.
*/
public function instantiateParentElement(?DOMElement $parent = null): DOMElement
{
$qualifiedName = $this->getQualifiedName();
$namespace = static::getNamespaceURI();
if ($parent === null) {
$parent = DOMDocumentFactory::create();
$e = $parent->createElementNS($namespace, $qualifiedName);
} else {
$doc = $parent->ownerDocument;
Assert::notNull($doc);
$e = $doc->createElementNS($namespace, $qualifiedName);
}
$parent->appendChild($e);
return $e;
}
/**
* @template T of \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
* @param \DOMElement $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(
DOMElement $xml,
string $name,
string $type = StringValue::class,
): ValueTypeInterface {
Assert::isAOf($type, ValueTypeInterface::class);
$prefix = static::getNamespacePrefix();
$localName = static::getLocalName();
$qName = $prefix ? ($prefix . ':' . $localName) : $localName;
Assert::true(
$xml->hasAttribute($name),
sprintf('Missing \'%s\' attribute on %s.', $name, $qName),
MissingAttributeException::class,
);
$value = $xml->getAttribute($name);
return ($type === QNameValue::class) ? QNameValue::fromDocument($value, $xml) : $type::fromString($value);
}
/**
* Get the value of an attribute from a given element.
*
* @template T of \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
* @param \DOMElement $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(
DOMElement $xml,
string $name,
string $type = StringValue::class,
?ValueTypeInterface $default = null,
): ?ValueTypeInterface {
Assert::nullOrIsInstanceOf($default, $type);
if (!$xml->hasAttribute($name)) {
return $default;
}
return self::getAttribute($xml, $name, $type);
}
/**
* Static method that processes a fully namespaced class name and returns the name of the class from it.
*
* @param string $class
*/
public static function getClassName(string $class): string
{
$ncName = join('', array_slice(explode('\\', $class), -1));
Assert::validNCName($ncName, SchemaViolationException::class);
return $ncName;
}
/**
* Get the XML qualified name (prefix:name) of the element represented by this class.
*/
public function getQualifiedName(): string
{
$prefix = static::getNamespacePrefix();
$qName = $prefix ? ($prefix . ':' . static::getLocalName()) : static::getLocalName();
Assert::validQName($qName, SchemaViolationException::class);
return $qName;
}
/**
* Extract localized names from the children of a given element.
*
* @param \DOMElement $parent The element we want to search.
* @return array<static> An array of objects of this class.
*/
public static function getChildrenOfClass(DOMElement $parent): array
{
$ret = [];
foreach ($parent->childNodes as $node) {
if (
$node instanceof DOMElement
&& $node->namespaceURI === static::getNamespaceURI()
&& $node->localName === static::getLocalName()
) {
$ret[] = static::fromXML($node);
}
}
return $ret;
}
/**
* Get the namespace for the element.
*/
public static function getNamespaceURI(): ?string
{
Assert::true(
defined('static::NS'),
self::getClassName(static::class)
. '::NS constant must be defined and set to the namespace for the XML-class it represents.',
RuntimeException::class,
);
// @phpstan-ignore classConstant.notFound
Assert::nullOrValidURI(static::NS, SchemaViolationException::class); // @phpstan-ignore-line
// @phpstan-ignore classConstant.notFound
return static::NS; // @phpstan-ignore-line
}
/**
* Get the namespace-prefix for the element.
*/
public static function getNamespacePrefix(): string
{
Assert::true(
defined('static::NS_PREFIX'),
self::getClassName(static::class)
. '::NS_PREFIX constant must be defined and set to the namespace prefix for the XML-class it represents.',
RuntimeException::class,
);
// @phpstan-ignore classConstant.notFound
return strval(static::NS_PREFIX); // @phpstan-ignore-line
}
/**
* Get the local name for the element.
*/
public static function getLocalName(): string
{
if (defined('static::LOCALNAME')) {
$ncName = static::LOCALNAME;
} else {
$ncName = self::getClassName(static::class);
}
Assert::validNCName($ncName, SchemaViolationException::class);
return $ncName;
}
/**
* Whether the element may be normalized.
*/
public static function getNormalization(): bool
{
if (defined('static::NORMALIZATION')) {
$normalization = static::NORMALIZATION;
} else {
$normalization = true;
}
Assert::boolean($normalization, RuntimeException::class);
return $normalization;
}
/**
* Test if an object, at the state it's in, would produce an empty XML-element
*
* @codeCoverageIgnore
*/
public function isEmptyElement(): bool
{
return false;
}
}