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