1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SOAP12\XML\env; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SOAP\Constants as C; |
10
|
|
|
use SimpleSAML\SOAP\Exception\ProtocolViolationException; |
11
|
|
|
use SimpleSAML\XML\Chunk; |
12
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
13
|
|
|
use SimpleSAML\XML\ExtendableAttributesTrait; |
14
|
|
|
use SimpleSAML\XML\ExtendableElementTrait; |
15
|
|
|
use SimpleSAML\XML\XsNamespace as 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 env:Body element. |
24
|
|
|
* |
25
|
|
|
* @package simplesaml/xml-soap |
26
|
|
|
*/ |
27
|
|
|
final class Body extends AbstractSoapElement |
28
|
|
|
{ |
29
|
|
|
use ExtendableAttributesTrait; |
|
|
|
|
30
|
|
|
use ExtendableElementTrait; |
31
|
|
|
|
32
|
|
|
/** The namespace-attribute for the xs:any element */ |
33
|
|
|
public const XS_ANY_ELT_NAMESPACE = NS::ANY; |
34
|
|
|
|
35
|
|
|
/** The namespace-attribute for the xs:anyAttribute element */ |
36
|
|
|
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initialize a soap:Body |
41
|
|
|
* |
42
|
|
|
* @param \SimpleSAML\SOAP12\XML\Fault|null $fault |
|
|
|
|
43
|
|
|
* @param \SimpleSAML\XML\ElementInterface[] $children |
44
|
|
|
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
protected ?Fault $fault = null, |
48
|
|
|
array $children = [], |
49
|
|
|
array $namespacedAttributes = [] |
50
|
|
|
) { |
51
|
|
|
if ($fault !== null) { |
52
|
|
|
/** |
53
|
|
|
* 5.4: When generating a fault, SOAP senders MUST NOT include additional element |
54
|
|
|
* information items in the SOAP Body . |
55
|
|
|
*/ |
56
|
|
|
Assert::isEmpty( |
57
|
|
|
$children, |
58
|
|
|
"When generating a fault, SOAP senders MUST NOT include additional elements in the SOAP Body.", |
59
|
|
|
ProtocolViolationException::class, |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
Assert::allNotInstanceOf($children, Fault::class, ProtocolViolationException::class); |
63
|
|
|
|
64
|
|
|
$this->setElements($children); |
65
|
|
|
$this->setAttributesNS($namespacedAttributes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \SimpleSAML\SOAP12\XML\env\Fault|null |
71
|
|
|
*/ |
72
|
|
|
public function getFault(): ?Fault |
73
|
|
|
{ |
74
|
|
|
return $this->fault; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test if an object, at the state it's in, would produce an empty XML-element |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function isEmptyElement(): bool |
84
|
|
|
{ |
85
|
|
|
return empty($this->fault) && empty($this->elements) && empty($this->namespacedAttributes); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/* |
90
|
|
|
* Convert XML into an Body element |
91
|
|
|
* |
92
|
|
|
* @param \DOMElement $xml The XML element we should load |
93
|
|
|
* @return static |
94
|
|
|
* |
95
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
96
|
|
|
* If the qualified name of the supplied element is wrong |
97
|
|
|
*/ |
98
|
|
|
public static function fromXML(DOMElement $xml): static |
99
|
|
|
{ |
100
|
|
|
Assert::same($xml->localName, 'Body', InvalidDOMElementException::class); |
101
|
|
|
Assert::same($xml->namespaceURI, Body::NS, InvalidDOMElementException::class); |
102
|
|
|
|
103
|
|
|
$children = $fault = []; |
104
|
|
|
foreach ($xml->childNodes as $child) { |
105
|
|
|
if (!($child instanceof DOMElement)) { |
106
|
|
|
continue; |
107
|
|
|
} elseif ($child->namespaceURI === C::NS_SOAP_ENV_12) { |
108
|
|
|
if ($child->localName === 'Fault') { |
109
|
|
|
$fault = Fault::fromXML($child); |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
$children[] = new Chunk($child); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 5.4: To be recognized as carrying SOAP error information, a SOAP message MUST contain a single SOAP Fault |
118
|
|
|
* element information item as the only child element information item of the SOAP Body . |
119
|
|
|
*/ |
120
|
|
|
Assert::maxCount($fault, 1, ProtocolViolationException::class); |
121
|
|
|
|
122
|
|
|
return new static( |
123
|
|
|
array_pop($fault), |
|
|
|
|
124
|
|
|
$children, |
125
|
|
|
self::getAttributesNSFromXML($xml) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Convert this Body to XML. |
132
|
|
|
* |
133
|
|
|
* @param \DOMElement|null $parent The element we should add this Body to. |
134
|
|
|
* @return \DOMElement This Body-element. |
135
|
|
|
*/ |
136
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
137
|
|
|
{ |
138
|
|
|
$e = $this->instantiateParentElement($parent); |
139
|
|
|
|
140
|
|
|
foreach ($this->getAttributesNS() as $attr) { |
141
|
|
|
$attr->toXML($e); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->getFault()?->toXML($e); |
145
|
|
|
|
146
|
|
|
/** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */ |
147
|
|
|
foreach ($this->getElements() as $child) { |
148
|
|
|
$child->toXML($e); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $e; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|