1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
6
|
|
|
|
7
|
|
|
use DOMAttr; |
8
|
|
|
use DOMElement; |
9
|
|
|
use SimpleSAML\Assert\Assert; |
10
|
|
|
|
11
|
|
|
use function array_keys; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class to represent an arbitrary namespaced attribute. |
15
|
|
|
* |
16
|
|
|
* @package simplesamlphp/xml-common |
17
|
|
|
*/ |
18
|
|
|
final class Attribute implements ArrayizableElementInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Create an Attribute class |
22
|
|
|
* |
23
|
|
|
* @param string|null $namespaceURI |
24
|
|
|
* @param string $namespacePrefix |
25
|
|
|
* @param string $attrName |
26
|
|
|
* @param string $attrValue |
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
protected ?string $namespaceURI, |
30
|
|
|
protected string $namespacePrefix, |
31
|
|
|
protected string $attrName, |
32
|
|
|
protected string $attrValue, |
33
|
|
|
) { |
34
|
|
|
Assert::nullOrStringNotEmpty($namespaceURI); |
35
|
|
|
Assert::string($namespacePrefix); |
36
|
|
|
Assert::notSame('xmlns', $namespacePrefix); |
37
|
|
|
Assert::stringNotEmpty($attrName); |
38
|
|
|
Assert::string($attrValue); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Collect the value of the namespaceURI-property |
44
|
|
|
* |
45
|
|
|
* @return string|null |
46
|
|
|
*/ |
47
|
|
|
public function getNamespaceURI(): ?string |
48
|
|
|
{ |
49
|
|
|
return $this->namespaceURI; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Collect the value of the namespacePrefix-property |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getNamespacePrefix(): string |
59
|
|
|
{ |
60
|
|
|
return $this->namespacePrefix; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Collect the value of the localName-property |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getAttrName(): string |
70
|
|
|
{ |
71
|
|
|
return $this->attrName; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Collect the value of the value-property |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getAttrValue(): string |
81
|
|
|
{ |
82
|
|
|
return $this->attrValue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create a class from XML |
88
|
|
|
* |
89
|
|
|
* @param \DOMAttr $xml |
90
|
|
|
* @return static |
91
|
|
|
*/ |
92
|
|
|
public static function fromXML(DOMAttr $attr): static |
93
|
|
|
{ |
94
|
|
|
return new static($attr->namespaceURI, $attr->prefix, $attr->localName, $attr->value); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create XML from this class |
101
|
|
|
* |
102
|
|
|
* @param \DOMElement $parent |
103
|
|
|
* @return \DOMElement |
104
|
|
|
*/ |
105
|
|
|
public function toXML(DOMElement $parent): DOMElement |
106
|
|
|
{ |
107
|
|
|
$parent->setAttributeNS( |
108
|
|
|
$this->getNamespaceURI(), |
109
|
|
|
$this->getNamespacePrefix() . ':' . $this->getAttrName(), |
110
|
|
|
$this->getAttrValue(), |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $parent; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Create a class from an array |
119
|
|
|
* |
120
|
|
|
* @param array $data |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public static function fromArray(array $data): static |
124
|
|
|
{ |
125
|
|
|
self::validateArray($data); |
126
|
|
|
|
127
|
|
|
return new static( |
128
|
|
|
$data['namespaceURI'], |
129
|
|
|
$data['namespacePrefix'], |
130
|
|
|
$data['attrName'], |
131
|
|
|
$data['attrValue'], |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Validate an array |
138
|
|
|
* |
139
|
|
|
* @param array $data |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public static function validateArray(array $data): void |
143
|
|
|
{ |
144
|
|
|
Assert::allOneOf( |
145
|
|
|
array_keys($data), |
146
|
|
|
['namespaceURI', 'namespacePrefix', 'attrName', 'attrValue'], |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
Assert::keyExists($data, 'namespaceURI'); |
150
|
|
|
Assert::keyExists($data, 'namespacePrefix'); |
151
|
|
|
Assert::keyExists($data, 'attrName'); |
152
|
|
|
Assert::keyExists($data, 'attrValue'); |
153
|
|
|
|
154
|
|
|
Assert::nullOrStringNotEmpty($data['namespaceURI']); |
155
|
|
|
Assert::string($data['namespacePrefix']); |
156
|
|
|
Assert::stringNotEmpty($data['attrName']); |
157
|
|
|
Assert::string($data['attrValue']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Create an array from this class |
163
|
|
|
* |
164
|
|
|
* @return array{attrName: string, attrValue: string, namespacePrefix: string, namespaceURI: null|string} |
165
|
|
|
*/ |
166
|
|
|
public function toArray(): array |
167
|
|
|
{ |
168
|
|
|
return [ |
169
|
|
|
'namespaceURI' => $this->getNamespaceURI(), |
170
|
|
|
'namespacePrefix' => $this->getNamespacePrefix(), |
171
|
|
|
'attrName' => $this->getAttrName(), |
172
|
|
|
'attrValue' => $this->getAttrValue(), |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|