Passed
Pull Request — master (#374)
by Tim
02:36
created

AbstractLocalizedURI::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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\Constants as C;
10
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
11
use SimpleSAML\SAML2\Type\SAMLStringValue;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\MissingAttributeException;
14
use SimpleSAML\XMLSchema\Type\LanguageValue;
15
16
use function array_key_first;
17
18
/**
19
 * Abstract class implementing LocalizedURIType.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
abstract class AbstractLocalizedURI extends AbstractLocalizedName
24
{
25
    /** @var string */
26
    public const TEXTCONTENT_TYPE = SAMLAnyURIValue::class;
27
28
29
    /**
30
     * LocalizedNameType constructor.
31
     *
32
     * @param \SimpleSAML\XMLSchema\Type\LanguageValue $language The language this string is localized in.
33
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue $content The localized string.
34
     */
35
    final public function __construct(
36
        protected LanguageValue $language,
37
        SAMLAnyURIValue $content,
38
    ) {
39
        $content = SAMLStringValue::fromString($content->getValue());
40
41
        parent::__construct($language, $content);
42
    }
43
44
45
    /**
46
     * Create an instance of this object from its XML representation.
47
     *
48
     * @param \DOMElement $xml
49
     * @return static
50
     *
51
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
52
     *   if the qualified name of the supplied element is wrong
53
     */
54
    public static function fromXML(DOMElement $xml): static
55
    {
56
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
57
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
58
        Assert::true(
59
            $xml->hasAttributeNS(C::NS_XML, 'lang'),
60
            'Missing xml:lang from ' . static::getLocalName(),
61
            MissingAttributeException::class,
62
        );
63
64
        return new static(
65
            LanguageValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang')),
66
            SAMLAnyURIValue::fromString($xml->textContent),
67
        );
68
    }
69
70
71
    /**
72
     * Create a class from an array
73
     *
74
     * @param array $data
75
     * @return static
76
     */
77
    public static function fromArray(array $data): static
78
    {
79
        Assert::count($data, 1);
80
81
        $lang = LanguageValue::fromString(array_key_first($data));
82
        $value = SAMLAnyURIValue::fromString(
83
            $data[$lang->getValue()],
84
        );
85
86
        return new static($lang, $value);
87
    }
88
}
89