GetMetadata::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\TooManyElementsException;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_pop;
16
17
/**
18
 * Class defining the GetMetadata element
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
final class GetMetadata extends AbstractWsxElement implements SchemaValidatableElementInterface
23
{
24
    use ExtendableAttributesTrait;
25
    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\GetMetadata: $message, $line
Loading history...
26
27
    /** The namespace-attribute for the xs:anyAttribute element */
28
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
29
30
31
    /**
32
     * GetMetadata constructor
33
     *
34
     * @param \SimpleSAML\WSSecurity\XML\wsx\Dialect|null $dialect
35
     * @param \SimpleSAML\WSSecurity\XML\wsx\Identifier|null $identifier
36
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
37
     */
38
    final public function __construct(
39
        protected ?Dialect $dialect = null,
40
        protected ?Identifier $identifier = null,
41
        array $namespacedAttributes = [],
42
    ) {
43
        $this->setAttributesNS($namespacedAttributes);
44
    }
45
46
47
    /**
48
     * Get the dialect property.
49
     *
50
     * @return \SimpleSAML\WSSecurity\XML\wsx\Dialect|null
51
     */
52
    public function getDialect(): ?Dialect
53
    {
54
        return $this->dialect;
55
    }
56
57
58
    /**
59
     * Get the identifier property.
60
     *
61
     * @return \SimpleSAML\WSSecurity\XML\wsx\Identifier|null
62
     */
63
    public function getIdentifier(): ?Identifier
64
    {
65
        return $this->identifier;
66
    }
67
68
69
    /**
70
     * Test if an object, at the state it's in, would produce an empty XML-element
71
     *
72
     * @return bool
73
     */
74
    public function isEmptyElement(): bool
75
    {
76
        return empty($this->getDialect())
77
            && empty($this->getIdentifier())
78
            && empty($this->getAttributesNS());
79
    }
80
81
82
    /**
83
     * Create an instance of this object from its XML representation.
84
     *
85
     * @param \DOMElement $xml
86
     * @return static
87
     *
88
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
89
     *   if the qualified name of the supplied element is wrong
90
     */
91
    public static function fromXML(DOMElement $xml): static
92
    {
93
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
94
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
95
96
        $dialect = Dialect::getChildrenOfClass($xml);
97
        Assert::maxCount($dialect, 1, TooManyElementsException::class);
98
99
        $identifier = Identifier::getChildrenOfClass($xml);
100
        Assert::maxCount($identifier, 1, TooManyElementsException::class);
101
102
        return new static(
103
            array_pop($dialect),
104
            array_pop($identifier),
105
            self::getAttributesNSFromXML($xml),
106
        );
107
    }
108
109
110
    /**
111
     * Add this GetMetadata to an XML element.
112
     *
113
     * @param \DOMElement|null $parent The element we should append this GetMetadata to.
114
     * @return \DOMElement
115
     */
116
    public function toXML(?DOMElement $parent = null): DOMElement
117
    {
118
        $e = parent::instantiateParentElement($parent);
119
120
        $this->getDialect()?->toXML($e);
121
        $this->getIdentifier()?->toXML($e);
122
123
        foreach ($this->getAttributesNS() as $attr) {
124
            $attr->toXML($e);
125
        }
126
127
        return $e;
128
    }
129
}
130