1 | <?php |
||
18 | class RandomOrgAPI { |
||
|
|||
19 | |||
20 | /** |
||
21 | * The default Random.org endpoint template. |
||
22 | * |
||
23 | * @var string; |
||
24 | */ |
||
25 | protected $endpoint = 'https://api.random.org/json-rpc/%s/invoke'; |
||
26 | |||
27 | /** |
||
28 | * The Random.org api key. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $apiKey = ''; |
||
33 | |||
34 | /** |
||
35 | * The HTTP client. |
||
36 | * |
||
37 | * @var Client |
||
38 | */ |
||
39 | protected $httpClient; |
||
40 | |||
41 | /** |
||
42 | * The Method plugin manager. |
||
43 | * |
||
44 | * @var MethodPluginManager |
||
45 | */ |
||
46 | protected $methodPluginManager; |
||
47 | |||
48 | /** |
||
49 | * The Random.org API version. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $apiVersion = 1; |
||
54 | |||
55 | /** |
||
56 | * The response if any. |
||
57 | * |
||
58 | * @var bool|ResponseInterface |
||
59 | */ |
||
60 | protected $response = FALSE; |
||
61 | |||
62 | /** |
||
63 | * @var MethodPluginInterface |
||
64 | */ |
||
65 | protected $methodPlugin = FALSE; |
||
66 | |||
67 | /** |
||
68 | * RandomOrgAPI constructor. |
||
69 | * |
||
70 | * @param null|\Http\Client\HttpClient $httpClient |
||
71 | * The HTTP client. |
||
72 | */ |
||
73 | 17 | public function __construct(HttpClient $httpClient = NULL) { |
|
78 | |||
79 | /** |
||
80 | * Set the Random.org API Key. |
||
81 | * |
||
82 | * @param string $key |
||
83 | * The API Key. |
||
84 | * |
||
85 | * @return self |
||
86 | */ |
||
87 | 17 | public function setApiKey($key) { |
|
92 | |||
93 | /** |
||
94 | * Get the Random.org API Key. |
||
95 | * |
||
96 | * @return string |
||
97 | * The API Key. |
||
98 | */ |
||
99 | 14 | public function getApiKey() { |
|
102 | |||
103 | /** |
||
104 | * Set the API version. |
||
105 | * |
||
106 | * @param int |
||
107 | * The API version. |
||
108 | * |
||
109 | * @return self |
||
110 | */ |
||
111 | 17 | public function setApiVersion($version) { |
|
117 | |||
118 | /** |
||
119 | * Get the API version. |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | 17 | public function getApiVersion() { |
|
126 | |||
127 | /** |
||
128 | * Set the Random.org endpoint template. |
||
129 | * |
||
130 | * @param string $uri |
||
131 | * The URI. |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | 2 | public function setEndpoint($uri) { |
|
141 | |||
142 | /** |
||
143 | * Get the Random.org endpoint. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 18 | public function getEndpoint() { |
|
150 | |||
151 | /** |
||
152 | * Set the client request. |
||
153 | * |
||
154 | * @param null|HttpClient $httpClient |
||
155 | * The client request. |
||
156 | * @param null|UriFactory $uriFactory |
||
157 | * The URI Factory. |
||
158 | * @param Plugin[] $plugins |
||
159 | * The HTTP plugins. |
||
160 | * |
||
161 | * @return self |
||
162 | */ |
||
163 | 17 | public function setHttpClient(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, array $plugins = array()) { |
|
177 | |||
178 | /** |
||
179 | * Get the Http client. |
||
180 | * |
||
181 | * @return Client |
||
182 | */ |
||
183 | 17 | public function getHttpClient() { |
|
186 | |||
187 | /** |
||
188 | * Set the method plugin. |
||
189 | * |
||
190 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin |
||
191 | * |
||
192 | * @return False|self |
||
193 | * Return itself, or FALSE otherwise. |
||
194 | */ |
||
195 | 13 | public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) { |
|
200 | |||
201 | /** |
||
202 | * Get the method plugin. |
||
203 | * |
||
204 | * @return \drupol\Yaroc\Plugin\MethodPluginInterface |
||
205 | */ |
||
206 | 13 | private function getMethodPlugin() { |
|
209 | |||
210 | /** |
||
211 | * Set the Method plugin manager. |
||
212 | * |
||
213 | * @param MethodPluginManager $methodPluginManager |
||
214 | * The method plugin manager. |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | 17 | public function setMethodPluginManager(MethodPluginManager $methodPluginManager) { |
|
223 | |||
224 | /** |
||
225 | * Return the Method plugin manager. |
||
226 | * |
||
227 | * @return \drupol\Yaroc\Plugin\MethodPluginManager |
||
228 | */ |
||
229 | 14 | public function getMethodPluginManager() { |
|
232 | |||
233 | /** |
||
234 | * Set the response. |
||
235 | * |
||
236 | * @param \Psr\Http\Message\ResponseInterface|NULL $response |
||
237 | * |
||
238 | * @return self |
||
239 | */ |
||
240 | 13 | private function setResponse(ResponseInterface $response = NULL) { |
|
245 | |||
246 | /** |
||
247 | * Get the response. |
||
248 | * |
||
249 | * @return bool|\Psr\Http\Message\ResponseInterface |
||
250 | */ |
||
251 | 14 | public function getResponse() { |
|
254 | |||
255 | /** |
||
256 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin |
||
257 | * |
||
258 | * @return ResponseInterface|\Exception |
||
259 | */ |
||
260 | 14 | private function request(MethodPluginInterface $methodPlugin) { |
|
263 | |||
264 | /** |
||
265 | * Call Random.org API. |
||
266 | * |
||
267 | * @param string $method |
||
268 | * The method to call. |
||
269 | * @param array $params |
||
270 | * The associative array of params as defined in the Random.org API. |
||
271 | * |
||
272 | * @return self |
||
273 | * Returns itself. |
||
274 | */ |
||
275 | 17 | public function call($method, array $params = array()) { |
|
292 | |||
293 | /** |
||
294 | * Get the result array from the response. |
||
295 | * |
||
296 | * @param string $key |
||
297 | * The key you want to get. |
||
298 | * |
||
299 | * @return array|bool |
||
300 | * The result array, FALSE otherwise. |
||
301 | */ |
||
302 | 13 | public function get($key = NULL) { |
|
311 | |||
312 | /** |
||
313 | * Get the result array from the response. |
||
314 | * |
||
315 | * @param string $key |
||
316 | * The key you want to get. |
||
317 | * |
||
318 | * @return array|bool |
||
319 | * The result array, FALSE otherwise. |
||
320 | */ |
||
321 | 14 | public function getFromResult($key = NULL) { |
|
333 | |||
334 | /** |
||
335 | * Get the data from the result array. |
||
336 | * |
||
337 | * @return array|bool |
||
338 | * The data array, FALSE otherwise. |
||
339 | */ |
||
340 | 12 | public function getData() { |
|
349 | |||
350 | } |
||
351 |