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