Passed
Push — master ( 9d958a...532318 )
by Tim
02:50
created

Envelope::fromXML()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 16
rs 9.9
cc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP11\XML;
6
7
use DOMElement;
8
use SimpleSAML\SOAP11\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\Exception\MissingElementException;
15
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
16
use SimpleSAML\XMLSchema\XML\Constants\NS;
17
18
/**
19
 * Class representing a SOAP-ENV:Envelope element.
20
 *
21
 * @package simplesaml/xml-soap
22
 */
23
final class Envelope extends AbstractSoapElement implements SchemaValidatableElementInterface
24
{
25
    use ExtendableElementTrait;
26
    use ExtendableAttributesTrait;
27
    use SchemaValidatableElementTrait;
28
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const string XS_ANY_ELT_NAMESPACE = NS::OTHER;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 31 at column 24
Loading history...
32
33
    /** The namespace-attribute for the xs:anyAttribute element */
34
    public const string XS_ANY_ATTR_NAMESPACE = NS::OTHER;
35
36
37
    /**
38
     * Initialize a SOAP-ENV:Envelope
39
     *
40
     * @param \SimpleSAML\SOAP11\XML\Body $body
41
     * @param \SimpleSAML\SOAP11\XML\Header|null $header
42
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $children
43
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
44
     */
45
    public function __construct(
46
        protected Body $body,
47
        protected ?Header $header = null,
48
        array $children = [],
49
        array $namespacedAttributes = [],
50
    ) {
51
        $this->setElements($children);
52
        $this->setAttributesNS($namespacedAttributes);
53
    }
54
55
56
    /**
57
     * @return \SimpleSAML\SOAP11\XML\Body
58
     */
59
    public function getBody(): Body
60
    {
61
        return $this->body;
62
    }
63
64
65
    /**
66
     * @return \SimpleSAML\SOAP11\XML\Header|null
67
     */
68
    public function getHeader(): ?Header
69
    {
70
        return $this->header;
71
    }
72
73
74
    /**
75
     * Convert XML into an Envelope element
76
     *
77
     * @param \DOMElement $xml The XML element we should load
78
     *
79
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
80
     *   If the qualified name of the supplied element is wrong
81
     */
82
    public static function fromXML(DOMElement $xml): static
83
    {
84
        Assert::same($xml->localName, 'Envelope', InvalidDOMElementException::class);
85
        Assert::same($xml->namespaceURI, Envelope::NS, InvalidDOMElementException::class);
86
87
        $body = Body::getChildrenOfClass($xml);
88
        Assert::count($body, 1, 'Must contain exactly one Body', MissingElementException::class);
89
90
        $header = Header::getChildrenOfClass($xml);
91
        Assert::maxCount($header, 1, 'Cannot process more than one Header element.', TooManyElementsException::class);
92
93
        return new static(
94
            array_pop($body),
95
            empty($header) ? null : array_pop($header),
96
            self::getChildElementsFromXML($xml),
97
            self::getAttributesNSFromXML($xml),
98
        );
99
    }
100
101
102
    /**
103
     * Convert this Envelope to XML.
104
     *
105
     * @param \DOMElement|null $parent The element we should add this envelope to.
106
     */
107
    public function toXML(?DOMElement $parent = null): DOMElement
108
    {
109
        $e = $this->instantiateParentElement($parent);
110
111
        foreach ($this->getAttributesNS() as $attr) {
112
            $attr->toXML($e);
113
        }
114
115
        if ($this->getHeader() !== null && !$this->getHeader()->isEmptyElement()) {
116
            $this->getHeader()->toXML($e);
117
        }
118
119
        $this->getBody()->toXML($e);
120
121
        foreach ($this->getElements() as $child) {
122
            if (!$child->isEmptyElement()) {
123
                $child->toXML($e);
124
            }
125
        }
126
127
        return $e;
128
    }
129
}
130