|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\md; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use SimpleSAML\Assert\Assert; |
|
9
|
|
|
use SimpleSAML\XML\Constants as C; |
|
10
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
|
11
|
|
|
use SimpleSAML\XML\Exception\MissingAttributeException; |
|
12
|
|
|
use SimpleSAML\XML\ArrayizableElementInterface; |
|
13
|
|
|
use SimpleSAML\XML\StringElementTrait; |
|
14
|
|
|
|
|
15
|
|
|
use function array_key_first; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Abstract class implementing LocalizedNameType. |
|
19
|
|
|
* |
|
20
|
|
|
* @package simplesamlphp/saml2 |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractLocalizedName extends AbstractMdElement implements ArrayizableElementInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use StringElementTrait; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* LocalizedNameType constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $language The language this string is localized in. |
|
31
|
|
|
* @param string $value The localized string. |
|
32
|
|
|
*/ |
|
33
|
|
|
final public function __construct( |
|
34
|
|
|
protected string $language, |
|
35
|
|
|
string $value, |
|
36
|
|
|
) { |
|
37
|
|
|
Assert::notEmpty($language, 'xml:lang cannot be empty.'); |
|
38
|
|
|
|
|
39
|
|
|
$this->setContent($value); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Validate the content of the element. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $content The value to go in the XML textContent |
|
47
|
|
|
* @throws \Exception on failure |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function validateContent(string $content): void |
|
51
|
|
|
{ |
|
52
|
|
|
Assert::notEmpty($content); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the language this string is localized in. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getLanguage(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->language; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create an instance of this object from its XML representation. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \DOMElement $xml |
|
71
|
|
|
* @return static |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
|
74
|
|
|
* if the qualified name of the supplied element is wrong |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function fromXML(DOMElement $xml): static |
|
77
|
|
|
{ |
|
78
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
|
79
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
80
|
|
|
Assert::true( |
|
81
|
|
|
$xml->hasAttributeNS(C::NS_XML, 'lang'), |
|
82
|
|
|
'Missing xml:lang from ' . static::getLocalName(), |
|
83
|
|
|
MissingAttributeException::class, |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
return new static($xml->getAttributeNS(C::NS_XML, 'lang'), $xml->textContent); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param \DOMElement|null $parent |
|
92
|
|
|
* @return \DOMElement |
|
93
|
|
|
*/ |
|
94
|
|
|
final public function toXML(DOMElement $parent = null): DOMElement |
|
95
|
|
|
{ |
|
96
|
|
|
$e = $this->instantiateParentElement($parent); |
|
97
|
|
|
$e->setAttributeNS(C::NS_XML, 'xml:lang', $this->getLanguage()); |
|
98
|
|
|
$e->textContent = $this->getContent(); |
|
99
|
|
|
|
|
100
|
|
|
return $e; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create a class from an array |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $data |
|
108
|
|
|
* @return static |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function fromArray(array $data): static |
|
111
|
|
|
{ |
|
112
|
|
|
self::validateArray($data); |
|
113
|
|
|
|
|
114
|
|
|
$lang = array_key_first($data); |
|
115
|
|
|
$value = $data[$lang]; |
|
116
|
|
|
|
|
117
|
|
|
return new static($lang, $value); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Validate an array |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $data |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function validateArray(array $data): void |
|
128
|
|
|
{ |
|
129
|
|
|
Assert::count($data, 1); |
|
130
|
|
|
$lang = array_key_first($data); |
|
131
|
|
|
Assert::stringNotEmpty($lang); |
|
132
|
|
|
Assert::stringNotEmpty($data[$lang]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Create an array from this class |
|
138
|
|
|
* |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
public function toArray(): array |
|
142
|
|
|
{ |
|
143
|
|
|
return [$this->language => $this->getContent()]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|