GetMetadata::isEmptyElement()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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