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

AbstractLocalizedURI::setContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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