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