Passed
Pull Request — master (#374)
by Tim
03:49 queued 01:20
created

Keywords::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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