Passed
Push — master ( 54bedc...fe2750 )
by Tim
02:40
created

MetadataSection::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 18
rs 9.8333
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsx;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\SerializableElementInterface;
15
use SimpleSAML\XML\XsNamespace as NS;
16
17
use function array_merge;
18
use function array_pop;
19
20
/**
21
 * Class defining the MetadataSection element
22
 *
23
 * @package simplesamlphp/ws-security
24
 */
25
final class MetadataSection extends AbstractWsxElement
26
{
27
    use ExtendableAttributesTrait;
28
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\wsx\MetadataSection: $namespaceURI, $localName, $childNodes
Loading history...
29
30
    /** The namespace-attribute for the xs:anyAttribute element */
31
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
32
33
    /** The namespace-attribute for the xs:any element */
34
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
35
36
37
    /**
38
     * MetadataSection constructor
39
     *
40
     * @param (\SimpleSAML\XML\SerializableElementInterface|
41
     *         \SimpleSAML\WSSecurity\XML\wsx\MetadataReference|
42
     *         \SimpleSAML\WSSecurity\XML\wsx\Location) $child
43
     * @param string $Dialect
44
     * @param string|null $Identifier
45
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
46
     */
47
    final public function __construct(
48
        protected SerializableElementInterface|MetadataReference|Location $child,
49
        protected string $Dialect,
50
        protected ?string $Identifier = null,
51
        array $namespacedAttributes = [],
52
    ) {
53
        if (!($child instanceof MetadataReference) && !($child instanceof Location)) {
54
            Assert::notSame($child->toXML()->namespaceURI, static::NS);
55
        }
56
        Assert::validURI($Dialect);
57
        Assert::nullOrValidURI($Identifier);
58
59
        $this->setAttributesNS($namespacedAttributes);
60
    }
61
62
63
    /**
64
     * Get the child property.
65
     *
66
     * @return (\SimpleSAML\XML\SerializableElementInterface|
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\SimpleSAML\XML\SerializableElementInterface| at position 3 could not be parsed: the token is null at position 3.
Loading history...
67
     *         \SimpleSAML\WSSecurity\XML\wsx\MetadataReference|
68
     *         \SimpleSAML\WSSecurity\XML\wsx\Location)
69
     */
70
    public function getChild(): SerializableElementInterface|MetadataReference|Location
71
    {
72
        return $this->child;
73
    }
74
75
76
    /**
77
     * Get the Dialect property.
78
     *
79
     * @return string
80
     */
81
    public function getDialect(): string
82
    {
83
        return $this->Dialect;
84
    }
85
86
87
    /**
88
     * Get the Identifier property.
89
     *
90
     * @return string|null
91
     */
92
    public function getIdentifier(): ?string
93
    {
94
        return $this->Identifier;
95
    }
96
97
98
    /**
99
     * Create an instance of this object from its XML representation.
100
     *
101
     * @param \DOMElement $xml
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
105
     *   if the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
111
112
        $children = self::getChildElementsFromXML($xml);
113
        $metadataReference = MetadataReference::getChildrenOfClass($xml);
114
        $location = Location::getChildrenOfClass($xml);
115
116
        $children = array_merge($children, $metadataReference, $location);
117
        Assert::minCount($children, 1, MissingElementException::class);
118
        Assert::maxCount($children, 1, TooManyElementsException::class);
119
120
        return new static(
121
            array_pop($children),
122
            self::getAttribute($xml, 'Dialect'),
123
            self::getOptionalAttribute($xml, 'Identifier', null),
124
            self::getAttributesNSFromXML($xml),
125
        );
126
    }
127
128
129
    /**
130
     * Add this MetadataSection to an XML element.
131
     *
132
     * @param \DOMElement $parent The element we should append this MetadataSection to.
133
     * @return \DOMElement
134
     */
135
    public function toXML(DOMElement $parent = null): DOMElement
136
    {
137
        $e = parent::instantiateParentElement($parent);
138
        $e->setAttribute('Dialect', $this->getDialect());
139
140
        if ($this->getIdentifier() !== null) {
141
            $e->setAttribute('Identifier', $this->getIdentifier());
142
        }
143
144
        $this->getChild()->toXML($e);
145
146
        foreach ($this->getAttributesNS() as $attr) {
147
            $attr->toXML($e);
148
        }
149
150
        return $e;
151
    }
152
}
153