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

AbstractLocalizedURI   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 3
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
    public const string TEXTCONTENT_TYPE = SAMLAnyURIValue::class;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 25 at column 24
Loading history...
26
27
28
    /**
29
     * LocalizedNameType constructor.
30
     *
31
     * @param \SimpleSAML\XMLSchema\Type\LanguageValue $language The language this string is localized in.
32
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue $content The localized string.
33
     */
34
    final public function __construct(
35
        protected LanguageValue $language,
36
        SAMLAnyURIValue $content,
37
    ) {
38
        $content = SAMLStringValue::fromString($content->getValue());
39
40
        parent::__construct($language, $content);
41
    }
42
43
44
    /**
45
     * Create an instance of this object from its XML representation.
46
     *
47
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
48
     *   if the qualified name of the supplied element is wrong
49
     */
50
    public static function fromXML(DOMElement $xml): static
51
    {
52
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
53
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
54
        Assert::true(
55
            $xml->hasAttributeNS(C::NS_XML, 'lang'),
56
            'Missing xml:lang from ' . static::getLocalName(),
57
            MissingAttributeException::class,
58
        );
59
60
        return new static(
61
            LanguageValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang')),
62
            SAMLAnyURIValue::fromString($xml->textContent),
63
        );
64
    }
65
66
67
    /**
68
     * Create a class from an array
69
     *
70
     * @param array<string, string> $data
71
     */
72
    public static function fromArray(array $data): static
73
    {
74
        Assert::count($data, 1);
75
76
        $lang = LanguageValue::fromString(array_key_first($data));
77
        $value = SAMLAnyURIValue::fromString(
78
            $data[$lang->getValue()],
79
        );
80
81
        return new static($lang, $value);
82
    }
83
}
84