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 | 16 | 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 | 16 | public function setApiKey($key) { |
|
92 | |||
93 | /** |
||
94 | * Get the Random.org API Key. |
||
95 | * |
||
96 | * @return string |
||
97 | * The API Key. |
||
98 | */ |
||
99 | 13 | public function getApiKey() { |
|
102 | |||
103 | /** |
||
104 | * Set the API version. |
||
105 | * |
||
106 | * @param int |
||
107 | * The API version. |
||
108 | * |
||
109 | * @return self |
||
110 | */ |
||
111 | 16 | public function setApiVersion($version) { |
|
117 | |||
118 | /** |
||
119 | * Get the API version. |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | 16 | 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 | 17 | 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 | 16 | public function setHttpClient(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, array $plugins = array()) { |
|
180 | |||
181 | /** |
||
182 | * Get the Http client. |
||
183 | * |
||
184 | * @return Client |
||
185 | */ |
||
186 | 16 | public function getHttpClient() { |
|
189 | |||
190 | /** |
||
191 | * Set the method plugin. |
||
192 | * |
||
193 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin |
||
194 | * |
||
195 | * @return False|self |
||
196 | * Return itself, or FALSE otherwise. |
||
197 | */ |
||
198 | 13 | public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) { |
|
203 | |||
204 | /** |
||
205 | * Get the method plugin. |
||
206 | * |
||
207 | * @return \drupol\Yaroc\Plugin\MethodPluginInterface |
||
208 | */ |
||
209 | 12 | private function getMethodPlugin() { |
|
212 | |||
213 | /** |
||
214 | * Set the Method plugin manager. |
||
215 | * |
||
216 | * @param MethodPluginManager $methodPluginManager |
||
217 | * The method plugin manager. |
||
218 | * |
||
219 | * @return self |
||
220 | */ |
||
221 | 16 | public function setMethodPluginManager(MethodPluginManager $methodPluginManager) { |
|
226 | |||
227 | /** |
||
228 | * Return the Method plugin manager. |
||
229 | * |
||
230 | * @return \drupol\Yaroc\Plugin\MethodPluginManager |
||
231 | */ |
||
232 | 13 | public function getMethodPluginManager() { |
|
235 | |||
236 | /** |
||
237 | * Set the response. |
||
238 | * |
||
239 | * @param \Psr\Http\Message\ResponseInterface|NULL $response |
||
240 | * |
||
241 | * @return self |
||
242 | */ |
||
243 | 13 | private function setResponse(ResponseInterface $response = NULL) { |
|
248 | |||
249 | /** |
||
250 | * Get the response. |
||
251 | * |
||
252 | * @return bool|\Psr\Http\Message\ResponseInterface |
||
253 | */ |
||
254 | 14 | public function getResponse() { |
|
257 | |||
258 | /** |
||
259 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin |
||
260 | * |
||
261 | * @return ResponseInterface|\Exception |
||
262 | */ |
||
263 | 13 | private function request(MethodPluginInterface $methodPlugin) { |
|
266 | |||
267 | /** |
||
268 | * Call Random.org API. |
||
269 | * |
||
270 | * @param string $method |
||
271 | * The method to call. |
||
272 | * @param array $params |
||
273 | * The associative array of params as defined in the Random.org API. |
||
274 | * |
||
275 | * @return self |
||
276 | * Returns itself. |
||
277 | */ |
||
278 | 16 | public function call($method, array $params = array()) { |
|
295 | |||
296 | /** |
||
297 | * Get the result array from the response. |
||
298 | * |
||
299 | * @param null|string $key |
||
300 | * The key you want to get. |
||
301 | * |
||
302 | * @return array|bool |
||
303 | * The result array, FALSE otherwise. |
||
304 | */ |
||
305 | 12 | public function get($key = NULL) { |
|
314 | |||
315 | /** |
||
316 | * Get the result array from the response. |
||
317 | * |
||
318 | * @param null|string $key |
||
319 | * The key you want to get. |
||
320 | * |
||
321 | * @return array|bool |
||
322 | * The result array, FALSE otherwise. |
||
323 | */ |
||
324 | 14 | public function getFromResult($key = NULL) { |
|
336 | |||
337 | /** |
||
338 | * Get the data from the result array. |
||
339 | * |
||
340 | * @return array|bool |
||
341 | * The data array, FALSE otherwise. |
||
342 | */ |
||
343 | 12 | public function getData() { |
|
352 | |||
353 | } |
||
354 |