1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use GuzzleHttp\Utils; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This class represents an OpenStack API operation. It encapsulates most aspects of the REST operation: its HTTP |
10
|
|
|
* method, the URL path, its top-level JSON key, and all of its {@see Parameter} objects. |
11
|
|
|
* |
12
|
|
|
* An operation not only represents a remote operation, but it also provides the mechanism for executing it |
13
|
|
|
* over HTTP. To do this, it uses a {@see ClientInterface} that allows a {@see GuzzleHttp\Message\Request} |
14
|
|
|
* to be created from the user values provided. Once this request is assembled, it is then sent to the |
15
|
|
|
* remote API and the response is returned to whoever first invoked the Operation class. |
16
|
|
|
* |
17
|
|
|
* @package OpenStack\Common\Api |
18
|
|
|
*/ |
19
|
|
|
class Operation |
20
|
|
|
{ |
21
|
|
|
/** @var string The HTTP method */ |
22
|
|
|
private $method; |
23
|
|
|
|
24
|
|
|
/** @var string The URL path */ |
25
|
|
|
private $path; |
26
|
|
|
|
27
|
|
|
/** @var string The top-level JSON key */ |
28
|
|
|
private $jsonKey; |
29
|
|
|
|
30
|
|
|
/** @var []Parameter The parameters of this operation */ |
31
|
|
|
private $params; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $definition The data definition (in array form) that will populate this |
35
|
|
|
* operation. Usually this is retrieved from an {@see ApiInterface} |
36
|
|
|
* object method. |
37
|
|
|
*/ |
38
|
207 |
|
public function __construct(array $definition) |
39
|
|
|
{ |
40
|
207 |
|
$this->method = $definition['method']; |
41
|
207 |
|
$this->path = $definition['path']; |
42
|
|
|
|
43
|
207 |
|
if (isset($definition['jsonKey'])) { |
44
|
46 |
|
$this->jsonKey = $definition['jsonKey']; |
45
|
46 |
|
} |
46
|
|
|
|
47
|
207 |
|
$this->params = self::toParamArray($definition['params']); |
48
|
207 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
199 |
|
public function getPath() |
54
|
|
|
{ |
55
|
199 |
|
return $this->path; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
199 |
|
public function getMethod() |
62
|
|
|
{ |
63
|
199 |
|
return $this->method; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Indicates whether this operation supports a parameter. |
68
|
|
|
* |
69
|
|
|
* @param $key The name of a parameter |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
1 |
|
public function hasParam($key) |
74
|
|
|
{ |
75
|
1 |
|
return isset($this->params[$key]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $name |
80
|
|
|
* |
81
|
|
|
* @return Parameter |
82
|
|
|
*/ |
83
|
182 |
|
public function getParam($name) |
84
|
|
|
{ |
85
|
182 |
|
return isset($this->params[$name]) ? $this->params[$name] : null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
68 |
|
public function getJsonKey() |
92
|
|
|
{ |
93
|
68 |
|
return $this->jsonKey; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* A convenience method that will take a generic array of data and convert it into an array of |
98
|
|
|
* {@see Parameter} objects. |
99
|
|
|
* |
100
|
|
|
* @param array $data A generic data array |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
207 |
|
public static function toParamArray(array $data) |
105
|
|
|
{ |
106
|
207 |
|
$params = []; |
107
|
|
|
|
108
|
207 |
|
foreach ($data as $name => $param) { |
109
|
198 |
|
$params[$name] = new Parameter($param + ['name' => $name]); |
110
|
207 |
|
} |
111
|
|
|
|
112
|
207 |
|
return $params; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* This method will validate all of the user-provided values and throw an exception if any |
117
|
|
|
* failures are detected. This is useful for basic sanity-checking before a request is |
118
|
|
|
* serialized and sent to the API. |
119
|
|
|
* |
120
|
|
|
* @param array $userValues The user-defined values |
121
|
|
|
* |
122
|
|
|
* @return bool TRUE if validation passes |
123
|
|
|
* @throws \Exception If validate fails |
124
|
|
|
*/ |
125
|
203 |
|
public function validate(array $userValues) |
126
|
|
|
{ |
127
|
203 |
|
foreach ($this->params as $paramName => $param) { |
128
|
|
|
if (array_key_exists($paramName, $userValues)) { |
129
|
202 |
|
$param->validate($userValues[$paramName]); |
130
|
194 |
|
} elseif ($param->isRequired()) { |
131
|
183 |
|
throw new \Exception(sprintf('"%s" is a required option, but it was not provided', $paramName)); |
132
|
194 |
|
} |
133
|
1 |
|
} |
134
|
|
|
|
135
|
202 |
|
return true; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|