Operation.php
8.2 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
311
312
<?php
namespace GuzzleHttp\Command\Guzzle;
use GuzzleHttp\Command\ToArrayInterface;
/**
* Guzzle operation
*/
class Operation implements ToArrayInterface
{
/** @var array Parameters */
private $parameters = [];
/** @var Parameter Additional parameters schema */
private $additionalParameters;
/** @var DescriptionInterface */
private $description;
/** @var array Config data */
private $config;
/**
* Builds an Operation object using an array of configuration data.
*
* - name: (string) Name of the command
* - httpMethod: (string) HTTP method of the operation
* - uri: (string) URI template that can create a relative or absolute URL
* - parameters: (array) Associative array of parameters for the command.
* Each value must be an array that is used to create {@see Parameter}
* objects.
* - summary: (string) This is a short summary of what the operation does
* - notes: (string) A longer description of the operation.
* - documentationUrl: (string) Reference URL providing more information
* about the operation.
* - responseModel: (string) The model name used for processing response.
* - deprecated: (bool) Set to true if this is a deprecated command
* - errorResponses: (array) Errors that could occur when executing the
* command. Array of hashes, each with a 'code' (the HTTP response code),
* 'phrase' (response reason phrase or description of the error), and
* 'class' (a custom exception class that would be thrown if the error is
* encountered).
* - data: (array) Any extra data that might be used to help build or
* serialize the operation
* - additionalParameters: (null|array) Parameter schema to use when an
* option is passed to the operation that is not in the schema
*
* @param array $config Array of configuration data
* @param DescriptionInterface $description Service description used to resolve models if $ref tags are found
*
* @throws \InvalidArgumentException
*/
public function __construct(array $config = [], DescriptionInterface $description = null)
{
static $defaults = [
'name' => '',
'httpMethod' => '',
'uri' => '',
'responseModel' => null,
'notes' => '',
'summary' => '',
'documentationUrl' => null,
'deprecated' => false,
'data' => [],
'parameters' => [],
'additionalParameters' => null,
'errorResponses' => [],
];
$this->description = $description === null ? new Description([]) : $description;
if (isset($config['extends'])) {
$config = $this->resolveExtends($config['extends'], $config);
}
$this->config = $config + $defaults;
// Account for the old style of using responseClass
if (isset($config['responseClass'])) {
$this->config['responseModel'] = $config['responseClass'];
}
$this->resolveParameters();
}
/**
* @return array
*/
public function toArray()
{
return $this->config;
}
/**
* Get the service description that the operation belongs to
*
* @return Description
*/
public function getServiceDescription()
{
return $this->description;
}
/**
* Get the params of the operation
*
* @return Parameter[]
*/
public function getParams()
{
return $this->parameters;
}
/**
* Get additionalParameters of the operation
*
* @return Parameter|null
*/
public function getAdditionalParameters()
{
return $this->additionalParameters;
}
/**
* Check if the operation has a specific parameter by name
*
* @param string $name Name of the param
*
* @return bool
*/
public function hasParam($name)
{
return isset($this->parameters[$name]);
}
/**
* Get a single parameter of the operation
*
* @param string $name Parameter to retrieve by name
*
* @return Parameter|null
*/
public function getParam($name)
{
return isset($this->parameters[$name])
? $this->parameters[$name]
: null;
}
/**
* Get the HTTP method of the operation
*
* @return string|null
*/
public function getHttpMethod()
{
return $this->config['httpMethod'];
}
/**
* Get the name of the operation
*
* @return string|null
*/
public function getName()
{
return $this->config['name'];
}
/**
* Get a short summary of what the operation does
*
* @return string|null
*/
public function getSummary()
{
return $this->config['summary'];
}
/**
* Get a longer text field to explain the behavior of the operation
*
* @return string|null
*/
public function getNotes()
{
return $this->config['notes'];
}
/**
* Get the documentation URL of the operation
*
* @return string|null
*/
public function getDocumentationUrl()
{
return $this->config['documentationUrl'];
}
/**
* Get the name of the model used for processing the response.
*
* @return string
*/
public function getResponseModel()
{
return $this->config['responseModel'];
}
/**
* Get whether or not the operation is deprecated
*
* @return bool
*/
public function getDeprecated()
{
return $this->config['deprecated'];
}
/**
* Get the URI that will be merged into the generated request
*
* @return string
*/
public function getUri()
{
return $this->config['uri'];
}
/**
* Get the errors that could be encountered when executing the operation
*
* @return array
*/
public function getErrorResponses()
{
return $this->config['errorResponses'];
}
/**
* Get extra data from the operation
*
* @param string $name Name of the data point to retrieve or null to
* retrieve all of the extra data.
*
* @return mixed|null
*/
public function getData($name = null)
{
if ($name === null) {
return $this->config['data'];
} elseif (isset($this->config['data'][$name])) {
return $this->config['data'][$name];
} else {
return null;
}
}
/**
* @return array
*/
private function resolveExtends($name, array $config)
{
if (!$this->description->hasOperation($name)) {
throw new \InvalidArgumentException('No operation named '.$name);
}
// Merge parameters together one level deep
$base = $this->description->getOperation($name)->toArray();
$result = $config + $base;
if (isset($base['parameters']) && isset($config['parameters'])) {
$result['parameters'] = $config['parameters'] + $base['parameters'];
}
return $result;
}
/**
* Process the description and extract the parameter config
*
* @return void
*/
private function resolveParameters()
{
// Parameters need special handling when adding
foreach ($this->config['parameters'] as $name => $param) {
if (!is_array($param)) {
throw new \InvalidArgumentException(
"Parameters must be arrays, {$this->config['name']}.$name is ".gettype($param)
);
}
$param['name'] = $name;
$this->parameters[$name] = new Parameter(
$param,
['description' => $this->description]
);
}
if ($this->config['additionalParameters']) {
if (is_array($this->config['additionalParameters'])) {
$this->additionalParameters = new Parameter(
$this->config['additionalParameters'],
['description' => $this->description]
);
} else {
$this->additionalParameters = $this->config['additionalParameters'];
}
}
}
}