-
Notifications
You must be signed in to change notification settings - Fork 135
/
DiscoHints.php
289 lines (240 loc) · 8.18 KB
/
DiscoHints.php
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
declare(strict_types=1);
namespace SimpleSAML\SAML2\XML\mdui;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML2\Exception\ArrayValidationException;
use SimpleSAML\SAML2\Utils\XPath;
use SimpleSAML\XML\ArrayizableElementInterface;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SerializableElementInterface;
use function array_filter;
use function array_key_exists;
use function array_keys;
/**
* Class for handling the metadata extensions for login and discovery user interface
*
* @link: http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-metadata-ui/v1.0/sstc-saml-metadata-ui-v1.0.pdf
* @package simplesamlphp/saml2
*/
final class DiscoHints extends AbstractMduiElement implements ArrayizableElementInterface
{
use ExtendableElementTrait;
/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_OTHER;
/**
* Create a DiscoHints element.
*
* @param \SimpleSAML\XML\Chunk[] $children
* @param \SimpleSAML\SAML2\XML\mdui\IPHint[] $ipHint
* @param \SimpleSAML\SAML2\XML\mdui\DomainHint[] $domainHint
* @param \SimpleSAML\SAML2\XML\mdui\GeolocationHint[] $geolocationHint
*/
public function __construct(
array $children = [],
protected array $ipHint = [],
protected array $domainHint = [],
protected array $geolocationHint = [],
) {
Assert::maxCount($ipHint, C::UNBOUNDED_LIMIT);
Assert::maxCount($domainHint, C::UNBOUNDED_LIMIT);
Assert::maxCount($geolocationHint, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOf($ipHint, IPHint::class);
Assert::allIsInstanceOf($domainHint, DomainHint::class);
Assert::allIsInstanceOf($geolocationHint, GeolocationHint::class);
$this->setElements($children);
}
/**
* Collect the value of the IPHint-property
*
* @return \SimpleSAML\SAML2\XML\mdui\IPHint[]
*/
public function getIPHint(): array
{
return $this->ipHint;
}
/**
* Collect the value of the DomainHint-property
*
* @return \SimpleSAML\SAML2\XML\mdui\DomainHint[]
*/
public function getDomainHint(): array
{
return $this->domainHint;
}
/**
* Collect the value of the GeolocationHint-property
*
* @return \SimpleSAML\SAML2\XML\mdui\GeolocationHint[]
*/
public function getGeolocationHint(): array
{
return $this->geolocationHint;
}
/**
* Add the value to the elements-property
*
* @param \SimpleSAML\XML\Chunk $child
*/
public function addChild(Chunk $child): void
{
$this->elements[] = $child;
}
/**
* Test if an object, at the state it's in, would produce an empty XML-element
*
* @return bool
*/
public function isEmptyElement(): bool
{
return empty($this->elements)
&& empty($this->ipHint)
&& empty($this->domainHint)
&& empty($this->geolocationHint);
}
/**
* Convert XML into a DiscoHints
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* if the qualified name of the supplied element is wrong
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'DiscoHints', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, DiscoHints::NS, InvalidDOMElementException::class);
$IPHint = IPHint::getChildrenOfClass($xml);
$DomainHint = DomainHint::getChildrenOfClass($xml);
$GeolocationHint = GeolocationHint::getChildrenOfClass($xml);
$children = [];
/** @var \DOMElement[] $nodes */
$nodes = XPath::xpQuery($xml, "./*[namespace-uri()!='" . DiscoHints::NS . "']", XPath::getXPath($xml));
foreach ($nodes as $node) {
$children[] = new Chunk($node);
}
return new static($children, $IPHint, $DomainHint, $GeolocationHint);
}
/**
* Convert this DiscoHints to XML.
*
* @param \DOMElement|null $parent The element we should append to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
foreach ($this->getIPHint() as $hint) {
$hint->toXML($e);
}
foreach ($this->getDomainHint() as $hint) {
$hint->toXML($e);
}
foreach ($this->getGeolocationHint() as $hint) {
$hint->toXML($e);
}
/** @var \SimpleSAML\XML\SerializableElementInterface $child */
foreach ($this->getElements() as $child) {
$child->toXML($e);
}
return $e;
}
/**
* Create a class from an array
*
* @param array $data
* @return static
*/
public static function fromArray(array $data): static
{
$data = self::processArrayContents($data);
return new static(
$data['children'] ?? [],
$data['IPHint'] ?? [],
$data['DomainHint'] ?? [],
$data['GeolocationHint'] ?? [],
);
}
/**
* Validates an array representation of this object and returns the same array with
* rationalized keys (casing) and parsed sub-elements.
*
* @param array $data
* @return array $data
*/
private static function processArrayContents(array $data): array
{
$data = array_change_key_case($data, CASE_LOWER);
// Make sure the array keys are known for this kind of object
Assert::allOneOf(
array_keys($data),
[
'iphint',
'domainhint',
'geolocationhint',
'children',
],
ArrayValidationException::class,
);
$retval = [];
if (array_key_exists('iphint', $data)) {
Assert::isArray($data['iphint'], ArrayValidationException::class);
Assert::allString($data['iphint'], ArrayValidationException::class);
foreach ($data['iphint'] as $hint) {
$retval['IPHint'][] = new IPHint($hint);
}
}
if (array_key_exists('domainhint', $data)) {
Assert::isArray($data['domainhint'], ArrayValidationException::class);
Assert::allString($data['domainhint'], ArrayValidationException::class);
foreach ($data['domainhint'] as $hint) {
$retval['DomainHint'][] = new DomainHint($hint);
}
}
if (array_key_exists('geolocationhint', $data)) {
Assert::isArray($data['geolocationhint'], ArrayValidationException::class);
Assert::allString($data['geolocationhint'], ArrayValidationException::class);
foreach ($data['geolocationhint'] as $hint) {
$retval['GeolocationHint'][] = new GeolocationHint($hint);
}
}
if (array_key_exists('children', $data)) {
Assert::isArray($data['children'], ArrayValidationException::class);
Assert::allIsInstanceOf(
$data['children'],
SerializableElementInterface::class,
ArrayValidationException::class,
);
$retval['children'] = $data['children'];
}
return $retval;
}
/**
* Create an array from this class
*
* @return array
*/
public function toArray(): array
{
$data = [
'IPHint' => [],
'DomainHint' => [],
'GeolocationHint' => [],
'children' => $this->getElements(),
];
foreach ($this->getIPHint() as $hint) {
$data['IPHint'][] = $hint->getContent();
}
foreach ($this->getDomainHint() as $hint) {
$data['DomainHint'][] = $hint->getContent();
}
foreach ($this->getGeolocationHint() as $hint) {
$data['GeolocationHint'][] = $hint->getContent();
}
return array_filter($data);
}
}