JsonLocation.php
5.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
<?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 a JSON document.
*/
class JsonLocation extends AbstractLocation
{
/** @var array The JSON document being visited */
private $json = [];
/**
* Set the name of the location
*
* @param string $locationName
*/
public function __construct($locationName = 'json')
{
parent::__construct($locationName);
}
/**
* @return \GuzzleHttp\Command\ResultInterface
*/
public function before(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
$body = (string) $response->getBody();
$body = $body ?: '{}';
$this->json = \GuzzleHttp\json_decode($body, true);
// relocate named arrays, so that they have the same structure as
// arrays nested in objects and visit can work on them in the same way
if ($model->getType() === 'array' && ($name = $model->getName())) {
$this->json = [$name => $this->json];
}
return $result;
}
/**
* @return ResultInterface
*/
public function after(
ResultInterface $result,
ResponseInterface $response,
Parameter $model
) {
// Handle additional, undefined properties
$additional = $model->getAdditionalProperties();
if (!($additional instanceof Parameter)) {
return $result;
}
// Use the model location as the default if one is not set on additional
$addLocation = $additional->getLocation() ?: $model->getLocation();
if ($addLocation == $this->locationName) {
foreach ($this->json as $prop => $val) {
if (!isset($result[$prop])) {
// Only recurse if there is a type specified
$result[$prop] = $additional->getType()
? $this->recurse($additional, $val)
: $val;
}
}
}
$this->json = [];
return $result;
}
/**
* @return Result|ResultInterface
*/
public function visit(
ResultInterface $result,
ResponseInterface $response,
Parameter $param
) {
$name = $param->getName();
$key = $param->getWireName();
// Check if the result should be treated as a list
if ($param->getType() == 'array') {
// Treat as javascript array
if ($name) {
// name provided, store it under a key in the array
$subArray = isset($this->json[$key]) ? $this->json[$key] : null;
$result[$name] = $this->recurse($param, $subArray);
} else {
// top-level `array` or an empty name
$result = new Result(array_merge(
$result->toArray(),
$this->recurse($param, $this->json)
));
}
} elseif (isset($this->json[$key])) {
$result[$name] = $this->recurse($param, $this->json[$key]);
}
return $result;
}
/**
* Recursively process a parameter while applying filters
*
* @param Parameter $param API parameter being validated
* @param mixed $value Value to process.
*
* @return mixed|null
*/
private function recurse(Parameter $param, $value)
{
if (!is_array($value)) {
return $param->filter($value);
}
$result = [];
$type = $param->getType();
if ($type == 'array') {
$items = $param->getItems();
foreach ($value as $val) {
$result[] = $this->recurse($items, $val);
}
} elseif ($type == 'object' && !isset($value[0])) {
// On the above line, we ensure that the array is associative and
// not numerically indexed
if ($properties = $param->getProperties()) {
foreach ($properties as $property) {
$key = $property->getWireName();
if (array_key_exists($key, $value)) {
$result[$property->getName()] = $this->recurse(
$property,
$value[$key]
);
// Remove from the value so that AP can later be handled
unset($value[$key]);
}
}
}
// Only check additional properties if everything wasn't already
// handled
if ($value) {
$additional = $param->getAdditionalProperties();
if ($additional === null || $additional === true) {
// Merge the JSON under the resulting array
$result += $value;
} elseif ($additional instanceof Parameter) {
// Process all child elements according to the given schema
foreach ($value as $prop => $val) {
$result[$prop] = $this->recurse($additional, $val);
}
}
}
}
return $param->filter($result);
}
}