1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SOAP11\XML\env; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SOAP\Constants as C; |
10
|
|
|
use SimpleSAML\XML\Chunk; |
11
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
12
|
|
|
use SimpleSAML\XML\Exception\MissingElementException; |
13
|
|
|
use SimpleSAML\XML\Exception\TooManyElementsException; |
14
|
|
|
use SimpleSAML\XML\ExtendableElementTrait; |
15
|
|
|
use SimpleSAML\XML\ExtendableAttributesTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class representing a env:Envelope element. |
19
|
|
|
* |
20
|
|
|
* @package simplesaml/xml-soap |
21
|
|
|
*/ |
22
|
|
|
final class Envelope extends AbstractSoapElement |
23
|
|
|
{ |
24
|
|
|
use ExtendableElementTrait; |
25
|
|
|
use ExtendableAttributesTrait; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** The namespace-attribute for the xs:any element */ |
28
|
|
|
public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_OTHER; |
29
|
|
|
|
30
|
|
|
/** The namespace-attribute for the xs:anyAttribute element */ |
31
|
|
|
public const XS_ANY_ATTR_NAMESPACE = C::XS_ANY_NS_OTHER; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize a env:Envelope |
36
|
|
|
* |
37
|
|
|
* @param \SimpleSAML\SOAP11\XML\env\Body $body |
38
|
|
|
* @param \SimpleSAML\SOAP11\XML\env\Header|null $header |
39
|
|
|
* @param \SimpleSAML\XML\ElementInterface[] $children |
40
|
|
|
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
protected Body $body, |
44
|
|
|
protected ?Header $header = null, |
45
|
|
|
array $children = [], |
46
|
|
|
array $namespacedAttributes = [], |
47
|
|
|
) { |
48
|
|
|
$this->setElements($children); |
49
|
|
|
$this->setAttributesNS($namespacedAttributes); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \SimpleSAML\SOAP11\XML\env\Body |
55
|
|
|
*/ |
56
|
|
|
public function getBody(): Body |
57
|
|
|
{ |
58
|
|
|
return $this->body; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \SimpleSAML\SOAP11\XML\env\Header|null |
64
|
|
|
*/ |
65
|
|
|
public function getHeader(): ?Header |
66
|
|
|
{ |
67
|
|
|
return $this->header; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Convert XML into an Envelope element |
73
|
|
|
* |
74
|
|
|
* @param \DOMElement $xml The XML element we should load |
75
|
|
|
* @return static |
76
|
|
|
* |
77
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
78
|
|
|
* If the qualified name of the supplied element is wrong |
79
|
|
|
*/ |
80
|
|
|
public static function fromXML(DOMElement $xml): static |
81
|
|
|
{ |
82
|
|
|
Assert::same($xml->localName, 'Envelope', InvalidDOMElementException::class); |
83
|
|
|
Assert::same($xml->namespaceURI, Envelope::NS, InvalidDOMElementException::class); |
84
|
|
|
|
85
|
|
|
$body = Body::getChildrenOfClass($xml); |
86
|
|
|
Assert::count($body, 1, 'Must contain exactly one Body', MissingElementException::class); |
87
|
|
|
|
88
|
|
|
$header = Header::getChildrenOfClass($xml); |
89
|
|
|
Assert::maxCount($header, 1, 'Cannot process more than one Header element.', TooManyElementsException::class); |
90
|
|
|
|
91
|
|
|
$children = []; |
92
|
|
|
foreach ($xml->childNodes as $child) { |
93
|
|
|
if (!($child instanceof DOMElement)) { |
94
|
|
|
continue; |
95
|
|
|
} elseif ($child->namespaceURI === C::NS_SOAP_ENV_11) { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
$children[] = new Chunk($child); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return new static( |
102
|
|
|
array_pop($body), |
103
|
|
|
empty($header) ? null : array_pop($header), |
104
|
|
|
$children, |
105
|
|
|
self::getAttributesNSFromXML($xml), |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Convert this Envelope to XML. |
112
|
|
|
* |
113
|
|
|
* @param \DOMElement|null $parent The element we should add this envelope to. |
114
|
|
|
* @return \DOMElement This Envelope-element. |
115
|
|
|
*/ |
116
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
117
|
|
|
{ |
118
|
|
|
$e = $this->instantiateParentElement($parent); |
119
|
|
|
|
120
|
|
|
foreach ($this->getAttributesNS() as $attr) { |
121
|
|
|
$attr->toXML($e); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($this->getHeader() !== null && !$this->getHeader()->isEmptyElement()) { |
125
|
|
|
$this->getHeader()->toXML($e); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->getBody()->toXML($e); |
129
|
|
|
|
130
|
|
|
/** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */ |
131
|
|
|
foreach ($this->getElements() as $child) { |
132
|
|
|
if (!$child->isEmptyElement()) { |
133
|
|
|
$child->toXML($e); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $e; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|