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

Detail::isEmptyElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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