Passed
Push — master ( c726f3...c686fd )
by Tim
10:07
created

Header::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP11\XML\env;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\ExtendableAttributesTrait;
14
15
/**
16
 * Class representing a env:Header element.
17
 *
18
 * @package simplesaml/xml-soap
19
 */
20
final class Header extends AbstractSoapElement
21
{
22
    use ExtendableAttributesTrait;
23
    use ExtendableElementTrait;
24
25
    /** The namespace-attribute for the xs:any element */
26
    public const NAMESPACE = C::XS_ANY_NS_ANY;
27
28
29
    /**
30
     * Initialize a env:Header
31
     *
32
     * @param \SimpleSAML\XML\Chunk[] $children
33
     * @param \DOMAttr[] $namespacedAttributes
34
     */
35
    public function __construct(array $children = [], array $namespacedAttributes = [])
36
    {
37
        $this->setElements($children);
38
        $this->setAttributesNS($namespacedAttributes);
39
    }
40
41
42
    /**
43
     * Test if an object, at the state it's in, would produce an empty XML-element
44
     *
45
     * @return bool
46
     */
47
    public function isEmptyElement(): bool
48
    {
49
        return empty($this->elements) && empty($this->namespacedAttributes);
50
    }
51
52
53
    /*
54
     * Convert XML into an Header element
55
     *
56
     * @param \DOMElement $xml The XML element we should load
57
     * @return static
58
     *
59
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
60
     *   If the qualified name of the supplied element is wrong
61
     */
62
    public static function fromXML(DOMElement $xml): static
63
    {
64
        Assert::same($xml->localName, 'Header', InvalidDOMElementException::class);
65
        Assert::same($xml->namespaceURI, Header::NS, InvalidDOMElementException::class);
66
67
        $children = [];
68
        foreach ($xml->childNodes as $child) {
69
            if (!($child instanceof DOMElement)) {
70
                continue;
71
            }
72
73
            $children[] = new Chunk($child);
74
        }
75
76
        return new static(
77
            $children,
78
            self::getAttributesNSFromXML($xml)
79
        );
80
    }
81
82
83
    /**
84
     * Convert this Header to XML.
85
     *
86
     * @param \DOMElement|null $parent The element we should add this header to.
87
     * @return \DOMElement This Header-element.
88
     */
89
    public function toXML(DOMElement $parent = null): DOMElement
90
    {
91
        $e = $this->instantiateParentElement($parent);
92
93
        foreach ($this->getAttributesNS() as $attr) {
94
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
95
        }
96
97
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
98
        foreach ($this->getElements() as $child) {
99
            $child->toXML($e);
100
        }
101
102
        return $e;
103
    }
104
}
105