MetadataSection::getIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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