Passed
Push — master ( a3fda0...7c0590 )
by Tim
14:58 queued 13:28
created

AbstractLocalizedName   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 6
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
    public const string TEXTCONTENT_TYPE = SAMLStringValue::class;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 30 at column 24
Loading history...
31
32
33
    /**
34
     * LocalizedNameType constructor.
35
     *
36
     * @param \SimpleSAML\XMLSchema\Type\LanguageValue $language The language this string is localized in.
37
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $content The localized string.
38
     */
39
    public function __construct(
40
        protected LanguageValue $language,
41
        SAMLStringValue $content,
42
    ) {
43
        $this->setContent($content);
44
    }
45
46
47
    /**
48
     * Get the language this string is localized in.
49
     *
50
     * @return \SimpleSAML\XMLSchema\Type\LanguageValue
51
     */
52
    public function getLanguage(): LanguageValue
53
    {
54
        return $this->language;
55
    }
56
57
58
    /**
59
     * Create an instance of this object from its XML representation.
60
     *
61
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
62
     *   if the qualified name of the supplied element is wrong
63
     */
64
    public static function fromXML(DOMElement $xml): static
65
    {
66
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
67
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
68
        Assert::true(
69
            $xml->hasAttributeNS(C::NS_XML, 'lang'),
70
            'Missing xml:lang from ' . static::getLocalName(),
71
            MissingAttributeException::class,
72
        );
73
74
        return new static(
75
            LanguageValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang')),
76
            SAMLStringValue::fromString($xml->textContent),
77
        );
78
    }
79
80
81
    /**
82
     */
83
    final public function toXML(?DOMElement $parent = null): DOMElement
84
    {
85
        $e = $this->instantiateParentElement($parent);
86
        $e->setAttributeNS(C::NS_XML, 'xml:lang', $this->getLanguage()->getValue());
87
        $e->textContent = $this->getContent()->getValue();
88
89
        return $e;
90
    }
91
92
93
    /**
94
     * Create a class from an array
95
     *
96
     * @param array<string, string> $data
97
     */
98
    public static function fromArray(array $data): static
99
    {
100
        Assert::count($data, 1, ArrayValidationException::class);
101
102
        $lang = LanguageValue::fromString(array_key_first($data));
103
        $value = SAMLStringValue::fromString($data[$lang->getValue()]);
104
105
        return new static($lang, $value);
106
    }
107
108
109
    /**
110
     * Create an array from this class
111
     *
112
     * @return array<string, string>
113
     */
114
    public function toArray(): array
115
    {
116
        return [$this->getLanguage()->getValue() => $this->getContent()->getValue()];
117
    }
118
}
119