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

Keywords::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
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\mdui;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\Type\ListOfStringsValue;
12
use SimpleSAML\XML\ArrayizableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XML\TypedTextContentTrait;
16
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
17
use SimpleSAML\XMLSchema\Type\LanguageValue;
18
19
use function array_key_first;
20
21
/**
22
 * Class for handling the Keywords metadata extensions for login and discovery user interface
23
 *
24
 * @link: http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-metadata-ui/v1.0/sstc-saml-metadata-ui-v1.0.pdf
25
 * @package simplesamlphp/saml2
26
 */
27
final class Keywords extends AbstractMduiElement implements
28
    ArrayizableElementInterface,
29
    SchemaValidatableElementInterface
30
{
31
    use SchemaValidatableElementTrait;
32
    use TypedTextContentTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TypedTextContentTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\mdui\Keywords: $localName, $namespaceURI
Loading history...
33
34
35
    /** @var string */
36
    public const TEXTCONTENT_TYPE = ListOfStringsValue::class;
37
38
39
    /**
40
     * Initialize a Keywords.
41
     *
42
     * @param \SimpleSAML\XMLSchema\Type\LanguageValue $lang
43
     * @param \SimpleSAML\SAML2\Type\ListOfStringsValue $keywords
44
     */
45
    public function __construct(
46
        protected LanguageValue $lang,
47
        ListOfStringsValue $keywords,
48
    ) {
49
        $this->setContent($keywords);
50
    }
51
52
53
    /**
54
     * Collect the value of the lang-property
55
     *
56
     * @return \SimpleSAML\XMLSchema\Type\LanguageValue
57
     */
58
    public function getLanguage(): LanguageValue
59
    {
60
        return $this->lang;
61
    }
62
63
64
    /**
65
     * Convert XML into a Keywords
66
     *
67
     * @param \DOMElement $xml The XML element we should load
68
     * @return static
69
     *
70
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
71
     *   if the qualified name of the supplied element is wrong
72
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
73
     *   if the supplied element is missing one of the mandatory attributes
74
     */
75
    public static function fromXML(DOMElement $xml): static
76
    {
77
        Assert::same($xml->localName, 'Keywords', InvalidDOMElementException::class);
78
        Assert::same($xml->namespaceURI, Keywords::NS, InvalidDOMElementException::class);
79
        Assert::stringNotEmpty($xml->textContent, 'Missing value for Keywords.');
80
81
        $lang = self::getAttribute($xml, 'xml:lang', LanguageValue::class);
82
        $Keywords = ListOfStringsValue::fromString($xml->textContent);
83
84
        return new static($lang, $Keywords);
85
    }
86
87
88
    /**
89
     * Convert this Keywords to XML.
90
     *
91
     * @param \DOMElement|null $parent The element we should append this Keywords to.
92
     * @throws \Exception
93
     * @return \DOMElement
94
     */
95
    public function toXML(?DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
        $e->setAttribute('xml:lang', $this->getLanguage()->getValue());
99
        $e->textContent = $this->getContent()->getValue();
100
101
        return $e;
102
    }
103
104
105
    /**
106
     * Create a class from an array
107
     *
108
     * @param array $data
109
     * @return static
110
     */
111
    public static function fromArray(array $data): static
112
    {
113
        Assert::notEmpty($data, ArrayValidationException::class);
114
        Assert::count($data, 1, ArrayValidationException::class);
115
116
        $lang = LanguageValue::fromString(array_key_first($data));
117
        $keywords = $data[$lang->getValue()];
118
119
        Assert::allNotContains($keywords, '+', ProtocolViolationException::class);
120
121
        return new static($lang, ListOfStringsValue::fromArray($keywords));
122
    }
123
124
125
    /**
126
     * Create an array from this class
127
     *
128
     * @return array
129
     */
130
    public function toArray(): array
131
    {
132
        /** @var \SimpleSAML\SAML2\Type\ListOfStringsValue $content */
133
        $content = $this->getContent();
134
135
        return [$this->getLanguage()->getValue() => $content->toArray()];
136
    }
137
}
138