Issues (16)

src/SOAP11/XML/Body.php (1 issue)

Labels
Severity
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\SOAP11\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_diff;
18
use function array_filter;
19
use function array_pop;
20
use function array_values;
21
22
/**
23
 * Class representing a SOAP-ENV:Body element.
24
 *
25
 * @package simplesaml/xml-soap
26
 */
27
final class Body extends AbstractSoapElement implements SchemaValidatableElementInterface
28
{
29
    use ExtendableAttributesTrait;
30
    use ExtendableElementTrait;
31
    use SchemaValidatableElementTrait;
32
33
34
    /** The namespace-attribute for the xs:any element */
35
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
36
37
    /** The namespace-attribute for the xs:anyAttribute element */
38
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
39
40
41
    /**
42
     * @var \SimpleSAML\SOAP11\XML\Fault|null
43
     */
44
    protected ?Fault $fault;
45
46
47
    /**
48
     * Initialize a soap:Body
49
     *
50
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $children
51
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
The type SimpleSAML\SOAP11\XML\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
     */
53
    public function __construct(array $children = [], array $namespacedAttributes = [])
54
    {
55
        /**
56
         * 4.4: If present, the SOAP Fault element MUST appear as a body entry and MUST NOT
57
         * appear more than once within a Body element.
58
         */
59
        $fault =  array_values(array_filter($children, function ($elt) {
60
            return $elt instanceof Fault;
61
        }));
62
        Assert::maxCount($fault, 1, ProtocolViolationException::class);
63
64
        $this->setFault(array_pop($fault));
65
        $this->setElements(array_diff($children, $fault));
66
        $this->setAttributesNS($namespacedAttributes);
67
    }
68
69
70
    /**
71
     * @param \SimpleSAML\SOAP11\XML\Fault|null $fault
72
     */
73
    public function setFault(?Fault $fault): void
74
    {
75
        $this->fault = $fault;
76
    }
77
78
79
    /**
80
     * @return \SimpleSAML\SOAP11\XML\Fault|null
81
     */
82
    public function getFault(): ?Fault
83
    {
84
        return $this->fault;
85
    }
86
87
88
    /**
89
     * Test if an object, at the state it's in, would produce an empty XML-element
90
     *
91
     * @return bool
92
     */
93
    public function isEmptyElement(): bool
94
    {
95
        return empty($this->fault) && empty($this->elements) && empty($this->namespacedAttributes);
96
    }
97
98
99
    /*
100
     * Convert XML into an Body element
101
     *
102
     * @param \DOMElement $xml The XML element we should load
103
     * @return static
104
     *
105
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
106
     *   If the qualified name of the supplied element is wrong
107
     */
108
    public static function fromXML(DOMElement $xml): static
109
    {
110
        Assert::same($xml->localName, 'Body', InvalidDOMElementException::class);
111
        Assert::same($xml->namespaceURI, Body::NS, InvalidDOMElementException::class);
112
113
        return new static(
114
            self::getChildElementsFromXML($xml),
115
            self::getAttributesNSFromXML($xml),
116
        );
117
    }
118
119
120
    /**
121
     * Convert this Body to XML.
122
     *
123
     * @param \DOMElement|null $parent The element we should add this Body to.
124
     * @return \DOMElement This Body-element.
125
     */
126
    public function toXML(?DOMElement $parent = null): DOMElement
127
    {
128
        $e = $this->instantiateParentElement($parent);
129
130
        foreach ($this->getAttributesNS() as $attr) {
131
            $attr->toXML($e);
132
        }
133
134
        $this->getFault()?->toXML($e);
135
136
        foreach ($this->getElements() as $child) {
137
            if (!$child->isEmptyElement()) {
138
                $child->toXML($e);
139
            }
140
        }
141
142
        return $e;
143
    }
144
}
145