Passed
Pull Request — master (#374)
by Tim
02:24
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
    /**
41
     * Initialize a Keywords.
42
     *
43
     * @param \SimpleSAML\XMLSchema\Type\LanguageValue $lang
44
     * @param \SimpleSAML\SAML2\Type\ListOfStringsValue $keywords
45
     */
46
    public function __construct(
47
        protected LanguageValue $lang,
48
        ListOfStringsValue $keywords,
49
    ) {
50
        $this->setContent($keywords);
51
    }
52
53
54
    /**
55
     * Collect the value of the lang-property
56
     *
57
     * @return \SimpleSAML\XMLSchema\Type\LanguageValue
58
     */
59
    public function getLanguage(): LanguageValue
60
    {
61
        return $this->lang;
62
    }
63
64
65
    /**
66
     * Convert XML into a Keywords
67
     *
68
     * @param \DOMElement $xml The XML element we should load
69
     * @return static
70
     *
71
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
72
     *   if the qualified name of the supplied element is wrong
73
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
74
     *   if the supplied element is missing one of the mandatory attributes
75
     */
76
    public static function fromXML(DOMElement $xml): static
77
    {
78
        Assert::same($xml->localName, 'Keywords', InvalidDOMElementException::class);
79
        Assert::same($xml->namespaceURI, Keywords::NS, InvalidDOMElementException::class);
80
        Assert::stringNotEmpty($xml->textContent, 'Missing value for Keywords.');
81
82
        $lang = self::getAttribute($xml, 'xml:lang', LanguageValue::class);
83
        $Keywords = ListOfStringsValue::fromString($xml->textContent);
84
85
        return new static($lang, $Keywords);
86
    }
87
88
89
    /**
90
     * Convert this Keywords to XML.
91
     *
92
     * @param \DOMElement|null $parent The element we should append this Keywords to.
93
     * @throws \Exception
94
     * @return \DOMElement
95
     */
96
    public function toXML(?DOMElement $parent = null): DOMElement
97
    {
98
        $e = $this->instantiateParentElement($parent);
99
        $e->setAttribute('xml:lang', $this->getLanguage()->getValue());
100
        $e->textContent = $this->getContent()->getValue();
101
102
        return $e;
103
    }
104
105
106
    /**
107
     * Create a class from an array
108
     *
109
     * @param array $data
110
     * @return static
111
     */
112
    public static function fromArray(array $data): static
113
    {
114
        Assert::notEmpty($data, ArrayValidationException::class);
115
        Assert::count($data, 1, ArrayValidationException::class);
116
117
        $lang = LanguageValue::fromString(array_key_first($data));
118
        $keywords = $data[$lang->getValue()];
119
120
        Assert::allNotContains($keywords, '+', ProtocolViolationException::class);
121
122
        return new static($lang, ListOfStringsValue::fromArray($keywords));
123
    }
124
125
126
    /**
127
     * Create an array from this class
128
     *
129
     * @return array
130
     */
131
    public function toArray(): array
132
    {
133
        return [$this->getLanguage()->getValue() => $this->getContent()->toArray()];
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface. It seems like you code against a sub-type of SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface such as SimpleSAML\XMLSchema\Typ...rface\ListTypeInterface or SimpleSAML\XMLSchema\Type\EntitiesValue or SimpleSAML\XMLSchema\Type\IDRefsValue or SimpleSAML\XMLSchema\Type\NMTokensValue or SimpleSAML\SAML2\Type\ListOfStringsValue or SimpleSAML\SAML2\Type\AnyURIListValue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        return [$this->getLanguage()->getValue() => $this->getContent()->/** @scrutinizer ignore-call */ toArray()];
Loading history...
134
    }
135
}
136