|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ================================== |
|
4
|
|
|
* Responsible PHP API |
|
5
|
|
|
* ================================== |
|
6
|
|
|
* |
|
7
|
|
|
* @link Git https://github.com/vince-scarpa/responsibleAPI.git |
|
8
|
|
|
* |
|
9
|
|
|
* @api Responible API |
|
10
|
|
|
* @package responsible\ |
|
11
|
|
|
* @version 1.2 |
|
12
|
|
|
* |
|
13
|
|
|
* @author Vince scarpa <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
namespace responsible; |
|
17
|
|
|
|
|
18
|
|
|
use responsible\core as responsibleCore; |
|
19
|
|
|
use responsible\core\configuration; |
|
20
|
|
|
use responsible\core\user; |
|
21
|
|
|
use responsible\core\headers; |
|
22
|
|
|
use responsible\core\exception\responsibleException; |
|
23
|
|
|
|
|
24
|
|
|
class responsible |
|
25
|
|
|
{ |
|
26
|
|
|
use \responsible\core\traits\optionsTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* [$config Variable store for the Responsible API config set] |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $config; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* [$defaults Variable store for the Responsible API defaults set] |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $defaults; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* [$server Core server object] |
|
42
|
|
|
* @var object |
|
43
|
|
|
*/ |
|
44
|
|
|
private $server; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* [$response Response data] |
|
48
|
|
|
* @var array|object |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $response; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* [$requestType Header request response format] |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
private $requestType = 'json'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* [$requestType Request limit] |
|
60
|
|
|
* @var integer |
|
61
|
|
|
*/ |
|
62
|
|
|
private $requestLimit; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* [$requestType Request window rate] |
|
66
|
|
|
* @var string|integer |
|
67
|
|
|
*/ |
|
68
|
|
|
private $requestRateWindow; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* [__construc :: Construct the Responsible API] |
|
72
|
|
|
* @param array $DEFAULTS |
|
73
|
|
|
* environment settings |
|
74
|
|
|
* @param array $options |
|
75
|
|
|
* API options |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct(array $options = [], $initiate = true) |
|
78
|
|
|
{ |
|
79
|
|
|
/** |
|
80
|
|
|
* Initiate the Responsible API configuration and options |
|
81
|
|
|
*/ |
|
82
|
|
|
$this->setConfig($options); |
|
83
|
|
|
|
|
84
|
|
|
$this->setRequestType(($options['requestType']) ?? 'json'); |
|
85
|
|
|
|
|
86
|
|
|
$this->setRateLimit(($options['rateLimit']) ?? 100); |
|
87
|
|
|
|
|
88
|
|
|
$this->setRateWindow(($options['rateWindow']) ?? 'MINUTE'); |
|
89
|
|
|
|
|
90
|
|
|
if ($initiate) { |
|
91
|
|
|
$this->run(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Run the server |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
public function run() |
|
100
|
|
|
{ |
|
101
|
|
|
/** |
|
102
|
|
|
* Initiate the Responsible API server |
|
103
|
|
|
*/ |
|
104
|
|
|
$this->server(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* [setConfig Set the ResponsibleAPI configuration] |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
private function setConfig($options) |
|
112
|
|
|
{ |
|
113
|
|
|
$config = new configuration\config; |
|
114
|
|
|
$config->baseApiRoot(dirname(__DIR__)); |
|
115
|
|
|
$config->responsibleDefault($options); |
|
116
|
|
|
|
|
117
|
|
|
$this->setOptions($config->getOptions()); |
|
118
|
|
|
$this->config($config->getConfig()); |
|
119
|
|
|
$this->defaults($config->getDefaults()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* [server :: Initiate the Responsible core server] |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
private function server() |
|
127
|
|
|
{ |
|
128
|
|
|
$options = $this->getOptions(); |
|
129
|
|
|
$route = ($options['route']) ?? ''; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* [$this->server :: Set the a new API server object] |
|
133
|
|
|
* @var responsibleCore [Alias for responsible\core] |
|
134
|
|
|
*/ |
|
135
|
|
|
$this->server = new responsibleCore\server( |
|
136
|
|
|
$this->getConfig(), |
|
137
|
|
|
$options, |
|
138
|
|
|
true |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
// Set the header request format |
|
142
|
|
|
$this->server->requestType($this->getRequestType()); |
|
143
|
|
|
|
|
144
|
|
|
// Authenticate the API connections |
|
145
|
|
|
try { |
|
146
|
|
|
$this->server->authenticate(); |
|
147
|
|
|
}catch (responsibleException | \Exception $e) { |
|
148
|
|
|
self::$response = $e->getMessage(); |
|
|
|
|
|
|
149
|
|
|
return; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// Set the rate limit and timeframe for API connection limits |
|
153
|
|
|
try { |
|
154
|
|
|
$this->server->rateLimit( |
|
155
|
|
|
$this->getRateLimit(), |
|
156
|
|
|
$this->getRateWindow() |
|
157
|
|
|
); |
|
158
|
|
|
}catch (responsibleException | \Exception $e) { |
|
159
|
|
|
self::$response = $e->getMessage(); |
|
160
|
|
|
return; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// Build the APIs internal router |
|
164
|
|
|
try { |
|
165
|
|
|
$this->server->route($route); |
|
166
|
|
|
}catch (responsibleException | \Exception $e) { |
|
167
|
|
|
self::$response = $e->getMessage(); |
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
self::$response = $this->server->response(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* [getApiData Get the Responsible API router data] |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
public function Router() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->server->getRouter(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* [response :: Get the final API response as an output] |
|
185
|
|
|
* @return object|array |
|
186
|
|
|
*/ |
|
187
|
|
|
public function responseData($debug = 'coredata') |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->server->response($debug); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* [config Set the Responsible API configuration] |
|
194
|
|
|
* @return void |
|
195
|
|
|
*/ |
|
196
|
|
|
private function config($config) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->config = $config; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* [getConfig Get the stored Responsible API configuration] |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getConfig() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->config; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* [defaults Set the Responsible API defaults] |
|
212
|
|
|
* Configuration and Options merged |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
|
|
private function defaults($defaults) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->defaults = $defaults; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* [getDefaults Get the stored Responsible API defaults] |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getDefaults() |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->defaults; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* [setRequestType :: Header request response format] |
|
231
|
|
|
* @param string $type |
|
232
|
|
|
*/ |
|
233
|
|
|
private function setRequestType($type) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->requestType = $type; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* [getRequestType :: Get the header request format] |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
private function getRequestType() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->requestType; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* [setRateLimit :: Set the Responsible API ratelimit] |
|
249
|
|
|
* How many API requests a connection is allowed |
|
250
|
|
|
* in a certain timeframe |
|
251
|
|
|
* |
|
252
|
|
|
* EG: 100 requests per minute |
|
253
|
|
|
* |
|
254
|
|
|
* @param integer $limit |
|
255
|
|
|
*/ |
|
256
|
|
|
private function setRateLimit($limit) |
|
257
|
|
|
{ |
|
258
|
|
|
$this->requestLimit = $limit; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* [getRateLimit :: Get the Responsible API ratelimit] |
|
263
|
|
|
* @return integer |
|
264
|
|
|
*/ |
|
265
|
|
|
private function getRateLimit() |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->requestLimit; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* [setRateWindow Set the Responsible API window for rate limits] |
|
272
|
|
|
* The window is a range set for API connection requests |
|
273
|
|
|
* |
|
274
|
|
|
* @see setRateLimit() |
|
275
|
|
|
* @param string|integer $frame |
|
276
|
|
|
*/ |
|
277
|
|
|
private function setRateWindow($frame) |
|
278
|
|
|
{ |
|
279
|
|
|
if (is_numeric($frame)) { |
|
280
|
|
|
$this->requestRateWindow = $frame; |
|
281
|
|
|
return; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$this->requestRateWindow = $frame; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* [getRateWindow Get the timeframe set for rate limits] |
|
289
|
|
|
* @return integer|string |
|
290
|
|
|
*/ |
|
291
|
|
|
private function getRateWindow() |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->requestRateWindow; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* ************************************** |
|
298
|
|
|
* PUBLIC FACTORY METHODS |
|
299
|
|
|
* *************************************** |
|
300
|
|
|
*/ |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* [API Initiate the Responsible API] |
|
304
|
|
|
* @param array $options |
|
305
|
|
|
* @return self|object |
|
306
|
|
|
*/ |
|
307
|
|
|
public static function API(array $options = []) |
|
308
|
|
|
{ |
|
309
|
|
|
return new self($options); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* [unauthorised Set a custom unauthorized header] |
|
314
|
|
|
* @return void |
|
315
|
|
|
*/ |
|
316
|
|
|
public static function unauthorised() |
|
317
|
|
|
{ |
|
318
|
|
|
(new headers\header)->unauthorised(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* [response Get the Responsible API response] |
|
323
|
|
|
* @return array|object |
|
324
|
|
|
*/ |
|
325
|
|
|
public static function response($echo = false) |
|
326
|
|
|
{ |
|
327
|
|
|
if ($echo) { |
|
328
|
|
|
print_r(self::$response); |
|
329
|
|
|
return; |
|
330
|
|
|
} |
|
331
|
|
|
return self::$response; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* [createUser Create a new user access] |
|
336
|
|
|
* @param string $name |
|
337
|
|
|
* @param string $mail |
|
338
|
|
|
* @return array |
|
339
|
|
|
*/ |
|
340
|
|
|
public static function createUser($name, $mail, array $options = []) |
|
341
|
|
|
{ |
|
342
|
|
|
return (new user\user) |
|
343
|
|
|
->setOptions($options) |
|
344
|
|
|
->credentials($name, $mail) |
|
345
|
|
|
->create() |
|
346
|
|
|
; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* [updateUser Update a user account] |
|
351
|
|
|
* @param array $properties |
|
352
|
|
|
* @return array |
|
353
|
|
|
*/ |
|
354
|
|
|
public static function updateUser($properties) |
|
355
|
|
|
{ |
|
356
|
|
|
return (new user\user) |
|
357
|
|
|
->update($properties) |
|
358
|
|
|
; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* [loadUser Load a stored account] |
|
363
|
|
|
* @param integer|string $property |
|
364
|
|
|
* @param string $type |
|
365
|
|
|
* @return array |
|
366
|
|
|
*/ |
|
367
|
|
|
public static function loadUser($property, array $options = []) |
|
368
|
|
|
{ |
|
369
|
|
|
$loadBy = (isset($options['loadBy']) && !empty($options['loadBy'])) |
|
370
|
|
|
? $options['loadBy'] : 'account_id'; |
|
371
|
|
|
|
|
372
|
|
|
$getJWT = (isset($options['getJWT']) && is_bool($options['getJWT'])) |
|
373
|
|
|
? $options['getJWT'] : true; |
|
374
|
|
|
|
|
375
|
|
|
$getSecretAppend = (isset($options['secret']) && ($options['secret'] == 'append') ) |
|
376
|
|
|
? $options['secret'] : false; |
|
377
|
|
|
|
|
378
|
|
|
return (new user\user) |
|
379
|
|
|
->setOptions($options) |
|
380
|
|
|
->load( |
|
381
|
|
|
$property, |
|
382
|
|
|
array( |
|
383
|
|
|
'loadBy' => $loadBy, |
|
384
|
|
|
'getJWT' => $getJWT, |
|
385
|
|
|
'secret' => $getSecretAppend |
|
386
|
|
|
) |
|
387
|
|
|
); |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..