XmlLocation.php
9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Guzzle\Operation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* Creates an XML document
*/
class XmlLocation extends AbstractLocation
{
/** @var \XMLWriter XML writer resource */
private $writer;
/** @var string Content-Type header added when XML is found */
private $contentType;
/** @var Parameter[] Buffered elements to write */
private $buffered = [];
/**
* @param string $locationName Name of the location
* @param string $contentType Set to a non-empty string to add a
* Content-Type header to a request if any XML content is added to the
* body. Pass an empty string to disable the addition of the header.
*/
public function __construct($locationName = 'xml', $contentType = 'application/xml')
{
parent::__construct($locationName);
$this->contentType = $contentType;
}
/**
* @return RequestInterface
*/
public function visit(
CommandInterface $command,
RequestInterface $request,
Parameter $param
) {
// Buffer and order the parameters to visit based on if they are
// top-level attributes or child nodes.
// @link https://github.com/guzzle/guzzle/pull/494
if ($param->getData('xmlAttribute')) {
array_unshift($this->buffered, $param);
} else {
$this->buffered[] = $param;
}
return $request;
}
/**
* @return RequestInterface
*/
public function after(
CommandInterface $command,
RequestInterface $request,
Operation $operation
) {
foreach ($this->buffered as $param) {
$this->visitWithValue(
$command[$param->getName()],
$param,
$operation
);
}
$this->buffered = [];
$additional = $operation->getAdditionalParameters();
if ($additional && $additional->getLocation() == $this->locationName) {
foreach ($command->toArray() as $key => $value) {
if (!$operation->hasParam($key)) {
$additional->setName($key);
$this->visitWithValue($value, $additional, $operation);
}
}
$additional->setName(null);
}
// If data was found that needs to be serialized, then do so
$xml = '';
if ($this->writer) {
$xml = $this->finishDocument($this->writer);
} elseif ($operation->getData('xmlAllowEmpty')) {
// Check if XML should always be sent for the command
$writer = $this->createRootElement($operation);
$xml = $this->finishDocument($writer);
}
if ($xml !== '') {
$request = $request->withBody(Psr7\Utils::streamFor($xml));
// Don't overwrite the Content-Type if one is set
if ($this->contentType && !$request->hasHeader('Content-Type')) {
$request = $request->withHeader('Content-Type', $this->contentType);
}
}
$this->writer = null;
return $request;
}
/**
* Create the root XML element to use with a request
*
* @param Operation $operation Operation object
*
* @return \XMLWriter
*/
protected function createRootElement(Operation $operation)
{
static $defaultRoot = ['name' => 'Request'];
// If no root element was specified, then just wrap the XML in 'Request'
$root = $operation->getData('xmlRoot') ?: $defaultRoot;
// Allow the XML declaration to be customized with xmlEncoding
$encoding = $operation->getData('xmlEncoding');
$writer = $this->startDocument($encoding);
$writer->startElement($root['name']);
// Create the wrapping element with no namespaces if no namespaces were present
if (!empty($root['namespaces'])) {
// Create the wrapping element with an array of one or more namespaces
foreach ((array) $root['namespaces'] as $prefix => $uri) {
$nsLabel = 'xmlns';
if (!is_numeric($prefix)) {
$nsLabel .= ':'.$prefix;
}
$writer->writeAttribute($nsLabel, $uri);
}
}
return $writer;
}
/**
* Recursively build the XML body
*
* @param \XMLWriter $writer XML to modify
* @param Parameter $param API Parameter
* @param mixed $value Value to add
*/
protected function addXml(\XMLWriter $writer, Parameter $param, $value)
{
$value = $param->filter($value);
$type = $param->getType();
$name = $param->getWireName();
$prefix = null;
$namespace = $param->getData('xmlNamespace');
if (false !== strpos($name, ':')) {
list($prefix, $name) = explode(':', $name, 2);
}
if ($type == 'object' || $type == 'array') {
if (!$param->getData('xmlFlattened')) {
if ($namespace) {
$writer->startElementNS(null, $name, $namespace);
} else {
$writer->startElement($name);
}
}
if ($param->getType() == 'array') {
$this->addXmlArray($writer, $param, $value);
} elseif ($param->getType() == 'object') {
$this->addXmlObject($writer, $param, $value);
}
if (!$param->getData('xmlFlattened')) {
$writer->endElement();
}
return;
}
if ($param->getData('xmlAttribute')) {
$this->writeAttribute($writer, $prefix, $name, $namespace, $value);
} else {
$this->writeElement($writer, $prefix, $name, $namespace, $value);
}
}
/**
* Write an attribute with namespace if used
*
* @param \XMLWriter $writer XMLWriter instance
* @param string $prefix Namespace prefix if any
* @param string $name Attribute name
* @param string $namespace The uri of the namespace
* @param string $value The attribute content
*/
protected function writeAttribute($writer, $prefix, $name, $namespace, $value)
{
if ($namespace) {
$writer->writeAttributeNS($prefix, $name, $namespace, $value);
} else {
$writer->writeAttribute($name, $value);
}
}
/**
* Write an element with namespace if used
*
* @param \XMLWriter $writer XML writer resource
* @param string $prefix Namespace prefix if any
* @param string $name Element name
* @param string $namespace The uri of the namespace
* @param string $value The element content
*/
protected function writeElement(\XMLWriter $writer, $prefix, $name, $namespace, $value)
{
if ($namespace) {
$writer->startElementNS($prefix, $name, $namespace);
} else {
$writer->startElement($name);
}
if (strpbrk($value, '<>&')) {
$writer->writeCData($value);
} else {
$writer->writeRaw($value);
}
$writer->endElement();
}
/**
* Create a new xml writer and start a document
*
* @param string $encoding document encoding
*
* @return \XMLWriter the writer resource
*
* @throws \RuntimeException if the document cannot be started
*/
protected function startDocument($encoding)
{
$this->writer = new \XMLWriter();
if (!$this->writer->openMemory()) {
throw new \RuntimeException('Unable to open XML document in memory');
}
if (!$this->writer->startDocument('1.0', $encoding)) {
throw new \RuntimeException('Unable to start XML document');
}
return $this->writer;
}
/**
* End the document and return the output
*
* @param \XMLWriter $writer
*
* @return string the writer resource
*/
protected function finishDocument($writer)
{
$writer->endDocument();
return $writer->outputMemory();
}
/**
* Add an array to the XML
*/
protected function addXmlArray(\XMLWriter $writer, Parameter $param, &$value)
{
if ($items = $param->getItems()) {
foreach ($value as $v) {
$this->addXml($writer, $items, $v);
}
}
}
/**
* Add an object to the XML
*/
protected function addXmlObject(\XMLWriter $writer, Parameter $param, &$value)
{
$noAttributes = [];
// add values which have attributes
foreach ($value as $name => $v) {
if ($property = $param->getProperty($name)) {
if ($property->getData('xmlAttribute')) {
$this->addXml($writer, $property, $v);
} else {
$noAttributes[] = ['value' => $v, 'property' => $property];
}
}
}
// now add values with no attributes
foreach ($noAttributes as $element) {
$this->addXml($writer, $element['property'], $element['value']);
}
}
private function visitWithValue(
$value,
Parameter $param,
Operation $operation
) {
if (!$this->writer) {
$this->createRootElement($operation);
}
$this->addXml($this->writer, $param, $value);
}
}