1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML; |
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
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait grouping common functionality for simple localized string elements |
15
|
|
|
* |
16
|
|
|
* @package simplesamlphp/xml-common |
17
|
|
|
*/ |
18
|
|
|
trait LocalizedStringElementTrait |
19
|
|
|
{ |
20
|
|
|
use XMLStringElementTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The language this string is on. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected string $language; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Validate the content of the element. |
32
|
|
|
* |
33
|
|
|
* @param string $content The value to go in the XML textContent |
34
|
|
|
* @throws \Exception on failure |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
protected function validateContent(string $content): void |
38
|
|
|
{ |
39
|
|
|
Assert::notEmpty($content); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the language this string is localized in. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getLanguage(): string |
49
|
|
|
{ |
50
|
|
|
return $this->language; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the language this string is localized in. |
56
|
|
|
* |
57
|
|
|
* @param string $language |
58
|
|
|
*/ |
59
|
|
|
protected function setLanguage(string $language): void |
60
|
|
|
{ |
61
|
|
|
Assert::notEmpty($language, 'xml:lang cannot be empty.'); |
62
|
|
|
$this->language = $language; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Convert XML into a class instance |
68
|
|
|
* |
69
|
|
|
* @param \DOMElement $xml The XML element we should load |
70
|
|
|
* @return self |
71
|
|
|
* |
72
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
73
|
|
|
* If the qualified name of the supplied element is wrong |
74
|
|
|
*/ |
75
|
|
|
public static function fromXML(DOMElement $xml): object |
76
|
|
|
{ |
77
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
78
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
|
|
|
79
|
|
|
Assert::true( |
80
|
|
|
$xml->hasAttributeNS(C::NS_XML, 'lang'), |
81
|
|
|
'Missing xml:lang from ' . static::getLocalName(), |
82
|
|
|
MissingAttributeException::class, |
83
|
|
|
); |
84
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
85
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
86
|
|
|
|
87
|
|
|
return new static($xml->getAttributeNS(C::NS_XML, 'lang'), $xml->textContent); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \DOMElement|null $parent |
93
|
|
|
* @return \DOMElement |
94
|
|
|
*/ |
95
|
|
|
final public function toXML(DOMElement $parent = null): DOMElement |
96
|
|
|
{ |
97
|
|
|
$e = $this->instantiateParentElement($parent); |
98
|
|
|
$e->setAttributeNS(C::NS_XML, 'xml:lang', $this->language); |
99
|
|
|
$e->textContent = $this->getContent(); |
100
|
|
|
|
101
|
|
|
return $e; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|