AbstractLocalizedName   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 11 1
A fromArray() 0 11 1
A getLanguage() 0 3 1
A toArray() 0 3 1
A toXML() 0 7 1
A __construct() 0 7 1
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\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\XML\StringElementTrait;
12
use SimpleSAML\XML\ArrayizableElementInterface;
13
use SimpleSAML\XML\Constants as C;
14
use SimpleSAML\XML\Exception\InvalidDOMElementException;
15
use SimpleSAML\XML\Exception\MissingAttributeException;
16
17
use function array_key_first;
18
19
/**
20
 * Abstract class implementing LocalizedNameType.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
abstract class AbstractLocalizedName extends AbstractMdElement implements ArrayizableElementInterface
25
{
26
    use StringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\SAML2\XML\StringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\AbstractLocalizedName: $localName, $namespaceURI
Loading history...
27
28
29
    /**
30
     * LocalizedNameType constructor.
31
     *
32
     * @param string $language The language this string is localized in.
33
     * @param string $value The localized string.
34
     */
35
    final public function __construct(
36
        protected string $language,
37
        string $value,
38
    ) {
39
        Assert::notWhitespaceOnly($language, ProtocolViolationException::class);
40
41
        $this->setContent($value);
42
    }
43
44
45
    /**
46
     * Get the language this string is localized in.
47
     *
48
     * @return string
49
     */
50
    public function getLanguage(): string
51
    {
52
        return $this->language;
53
    }
54
55
56
    /**
57
     * Create an instance of this object from its XML representation.
58
     *
59
     * @param \DOMElement $xml
60
     * @return static
61
     *
62
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
63
     *   if the qualified name of the supplied element is wrong
64
     */
65
    public static function fromXML(DOMElement $xml): static
66
    {
67
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
68
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
69
        Assert::true(
70
            $xml->hasAttributeNS(C::NS_XML, 'lang'),
71
            'Missing xml:lang from ' . static::getLocalName(),
72
            MissingAttributeException::class,
73
        );
74
75
        return new static($xml->getAttributeNS(C::NS_XML, 'lang'), $xml->textContent);
76
    }
77
78
79
    /**
80
     * @param \DOMElement|null $parent
81
     * @return \DOMElement
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());
87
        $e->textContent = $this->getContent();
88
89
        return $e;
90
    }
91
92
93
    /**
94
     * Create a class from an array
95
     *
96
     * @param array $data
97
     * @return static
98
     */
99
    public static function fromArray(array $data): static
100
    {
101
        Assert::count($data, 1, ArrayValidationException::class);
102
103
        $lang = array_key_first($data);
104
        Assert::stringNotEmpty($lang, ArrayValidationException::class);
105
106
        $value = $data[$lang];
107
        Assert::stringNotEmpty($value, ArrayValidationException::class);
108
109
        return new static($lang, $value);
110
    }
111
112
113
    /**
114
     * Create an array from this class
115
     *
116
     * @return array
117
     */
118
    public function toArray(): array
119
    {
120
        return [$this->language => $this->getContent()];
121
    }
122
}
123