MetadataSection::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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