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

Envelope   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

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