XmlLocation.php
9.3 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
<?php
namespace GuzzleHttp\Command\Guzzle\ResponseLocation;
use GuzzleHttp\Command\Guzzle\Parameter;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Extracts elements from an XML document
*/
class XmlLocation extends AbstractLocation
{
/** @var \SimpleXMLElement XML document being visited */
private $xml;
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'xml')
{
parent::__construct($locationName);
}
/**
* @return ResultInterface
*/
public function before(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
$this->xml = simplexml_load_string((string) $response->getBody());
return $result;
}
/**
* @return Result|ResultInterface
*/
public function after(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
// Handle additional, undefined properties
$additional = $model->getAdditionalProperties();
if ($additional instanceof Parameter
&& $additional->getLocation() == $this->locationName
) {
$result = new Result(array_merge(
$result->toArray(),
self::xmlToArray($this->xml)
));
}
$this->xml = null;
return $result;
}
/**
* @return ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
$sentAs = $param->getWireName();
$ns = null;
if (null !== $sentAs && strstr($sentAs, ':')) {
list($ns, $sentAs) = explode(':', $sentAs);
}
// Process the primary property
if (count($this->xml->children($ns, true)->{$sentAs})) {
$result[$param->getName()] = $this->recursiveProcess(
$param,
$this->xml->children($ns, true)->{$sentAs}
);
}
return $result;
}
/**
* Recursively process a parameter while applying filters
*
* @param Parameter $param API parameter being processed
* @param \SimpleXMLElement $node Node being processed
*
* @return array
*/
private function recursiveProcess(
Parameter $param,
\SimpleXMLElement $node
) {
$result = [];
$type = $param->getType();
if ($type == 'object') {
$result = $this->processObject($param, $node);
} elseif ($type == 'array') {
$result = $this->processArray($param, $node);
} else {
// We are probably handling a flat data node (i.e. string or
// integer), so let's check if it's childless, which indicates a
// node containing plain text.
if ($node->children()->count() == 0) {
// Retrieve text from node
$result = (string) $node;
}
}
// Filter out the value
if (isset($result)) {
$result = $param->filter($result);
}
return $result;
}
/**
* @return array
*/
private function processArray(Parameter $param, \SimpleXMLElement $node)
{
// Cast to an array if the value was a string, but should be an array
$items = $param->getItems();
$sentAs = $items->getWireName();
$result = [];
$ns = null;
if (null !== $sentAs && strstr($sentAs, ':')) {
// Get namespace from the wire name
list($ns, $sentAs) = explode(':', $sentAs);
} else {
// Get namespace from data
$ns = $items->getData('xmlNs');
}
if ($sentAs === null) {
// A general collection of nodes
foreach ($node as $child) {
$result[] = $this->recursiveProcess($items, $child);
}
} else {
// A collection of named, repeating nodes
// (i.e. <collection><foo></foo><foo></foo></collection>)
$children = $node->children($ns, true)->{$sentAs};
foreach ($children as $child) {
$result[] = $this->recursiveProcess($items, $child);
}
}
return $result;
}
/**
* Process an object
*
* @param Parameter $param API parameter being parsed
* @param \SimpleXMLElement $node Value to process
*
* @return array
*/
private function processObject(Parameter $param, \SimpleXMLElement $node)
{
$result = $knownProps = $knownAttributes = [];
// Handle known properties
if ($properties = $param->getProperties()) {
foreach ($properties as $property) {
$name = $property->getName();
$sentAs = $property->getWireName();
$knownProps[$sentAs] = 1;
if (strpos($sentAs, ':')) {
list($ns, $sentAs) = explode(':', $sentAs);
} else {
$ns = $property->getData('xmlNs');
}
if ($property->getData('xmlAttribute')) {
// Handle XML attributes
$result[$name] = (string) $node->attributes($ns, true)->{$sentAs};
$knownAttributes[$sentAs] = 1;
} elseif (count($node->children($ns, true)->{$sentAs})) {
// Found a child node matching wire name
$childNode = $node->children($ns, true)->{$sentAs};
$result[$name] = $this->recursiveProcess(
$property,
$childNode
);
}
}
}
// Handle additional, undefined properties
$additional = $param->getAdditionalProperties();
if ($additional instanceof Parameter) {
// Process all child elements according to the given schema
foreach ($node->children($additional->getData('xmlNs'), true) as $childNode) {
$sentAs = $childNode->getName();
if (!isset($knownProps[$sentAs])) {
$result[$sentAs] = $this->recursiveProcess(
$additional,
$childNode
);
}
}
} elseif ($additional === null || $additional === true) {
// Blindly transform the XML into an array preserving as much data
// as possible. Remove processed, aliased properties.
$array = array_diff_key(self::xmlToArray($node), $knownProps);
// Remove @attributes that were explicitly plucked from the
// attributes list.
if (isset($array['@attributes']) && $knownAttributes) {
$array['@attributes'] = array_diff_key($array['@attributes'], $knownProps);
if (!$array['@attributes']) {
unset($array['@attributes']);
}
}
// Merge it together with the original result
$result = array_merge($array, $result);
}
return $result;
}
/**
* Convert an XML document to an array.
*
* @param int $nesting
* @param null $ns
*
* @return array
*/
private static function xmlToArray(
\SimpleXMLElement $xml,
$ns = null,
$nesting = 0
) {
$result = [];
$children = $xml->children($ns, true);
foreach ($children as $name => $child) {
$attributes = (array) $child->attributes($ns, true);
if (!isset($result[$name])) {
$childArray = self::xmlToArray($child, $ns, $nesting + 1);
$result[$name] = $attributes
? array_merge($attributes, $childArray)
: $childArray;
continue;
}
// A child element with this name exists so we're assuming
// that the node contains a list of elements
if (!is_array($result[$name])) {
$result[$name] = [$result[$name]];
} elseif (!isset($result[$name][0])) {
// Convert the first child into the first element of a numerically indexed array
$firstResult = $result[$name];
$result[$name] = [];
$result[$name][] = $firstResult;
}
$childArray = self::xmlToArray($child, $ns, $nesting + 1);
if ($attributes) {
$result[$name][] = array_merge($attributes, $childArray);
} else {
$result[$name][] = $childArray;
}
}
// Extract text from node
$text = trim((string) $xml);
if ($text === '') {
$text = null;
}
// Process attributes
$attributes = (array) $xml->attributes($ns, true);
if ($attributes) {
if ($text !== null) {
$result['value'] = $text;
}
$result = array_merge($attributes, $result);
} elseif ($text !== null) {
$result = $text;
}
// Make sure we're always returning an array
if ($nesting == 0 && !is_array($result)) {
$result = [$result];
}
return $result;
}
}