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

Body   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 30
c 0
b 0
f 0
dl 0
loc 114
rs 10
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\SOAP12\Exception\ProtocolViolationException;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15
use SimpleSAML\XMLSchema\XML\Constants\NS;
16
17
use function array_pop;
18
19
/**
20
 * Class representing a env:Body element.
21
 *
22
 * @package simplesaml/xml-soap
23
 */
24
final class Body extends AbstractSoapElement implements SchemaValidatableElementInterface
25
{
26
    use ExtendableAttributesTrait;
27
    use ExtendableElementTrait;
28
    use SchemaValidatableElementTrait;
29
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const string XS_ANY_ELT_NAMESPACE = NS::ANY;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 32 at column 24
Loading history...
33
34
    /** The namespace-attribute for the xs:anyAttribute element */
35
    public const string XS_ANY_ATTR_NAMESPACE = NS::OTHER;
36
37
38
    /**
39
     * Initialize a soap:Body
40
     *
41
     * @param \SimpleSAML\SOAP12\XML\Fault|null $fault
42
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $children
43
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
44
     */
45
    public function __construct(
46
        protected ?Fault $fault = null,
47
        array $children = [],
48
        array $namespacedAttributes = [],
49
    ) {
50
        if ($fault !== null) {
51
            /**
52
             * 5.4: When generating a fault, SOAP senders MUST NOT include additional element
53
             *      information items in the SOAP Body .
54
             */
55
            Assert::isEmpty(
56
                $children,
57
                "When generating a fault, SOAP senders MUST NOT include additional elements in the SOAP Body.",
58
                ProtocolViolationException::class,
59
            );
60
        }
61
        Assert::allNotInstanceOf($children, Fault::class, ProtocolViolationException::class);
62
63
        $this->setElements($children);
64
        $this->setAttributesNS($namespacedAttributes);
65
    }
66
67
68
    /**
69
     * @return \SimpleSAML\SOAP12\XML\Fault|null
70
     */
71
    public function getFault(): ?Fault
72
    {
73
        return $this->fault;
74
    }
75
76
77
    /**
78
     * Test if an object, at the state it's in, would produce an empty XML-element
79
     */
80
    public function isEmptyElement(): bool
81
    {
82
        return empty($this->fault) && empty($this->elements) && empty($this->namespacedAttributes);
83
    }
84
85
86
    /*
87
     * Convert XML into an Body element
88
     *
89
     * @param \DOMElement $xml The XML element we should load
90
     *
91
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
92
     *   If the qualified name of the supplied element is wrong
93
     */
94
    public static function fromXML(DOMElement $xml): static
95
    {
96
        Assert::same($xml->localName, 'Body', InvalidDOMElementException::class);
97
        Assert::same($xml->namespaceURI, Body::NS, InvalidDOMElementException::class);
98
99
        /**
100
         * 5.4: To be recognized as carrying SOAP error information, a SOAP message MUST contain a single SOAP Fault
101
         *      element information item as the only child element information item of the SOAP Body .
102
         */
103
        $fault = Fault::getChildrenOfClass($xml);
104
        Assert::maxCount($fault, 1, ProtocolViolationException::class);
105
106
        return new static(
107
            array_pop($fault),
108
            self::getChildElementsFromXML($xml),
109
            self::getAttributesNSFromXML($xml),
110
        );
111
    }
112
113
114
    /**
115
     * Convert this Body to XML.
116
     *
117
     * @param \DOMElement|null $parent The element we should add this Body to.
118
     */
119
    public function toXML(?DOMElement $parent = null): DOMElement
120
    {
121
        $e = $this->instantiateParentElement($parent);
122
123
        foreach ($this->getAttributesNS() as $attr) {
124
            $attr->toXML($e);
125
        }
126
127
        $this->getFault()?->toXML($e);
128
129
        foreach ($this->getElements() as $child) {
130
            $child->toXML($e);
131
        }
132
133
        return $e;
134
    }
135
}
136