Metadata::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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