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