1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/PhpPulse/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\DaPulse\Objects; |
9
|
|
|
|
10
|
|
|
use allejo\DaPulse\Exceptions\InvalidObjectException; |
11
|
|
|
use allejo\DaPulse\Utilities\UrlQuery; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The base class for all DaPulse API objects |
15
|
|
|
* |
16
|
|
|
* @internal |
17
|
|
|
* @package allejo\DaPulse\Objects |
18
|
|
|
* @since 0.1.0 |
19
|
|
|
*/ |
20
|
|
|
abstract class ApiObject implements \JsonSerializable |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The namespace used for all main PhpPulse objects. This is value is prepended before PhpPulse objects when being |
24
|
|
|
* checked with `instanceof`. |
25
|
|
|
* |
26
|
|
|
* @internal |
27
|
|
|
*/ |
28
|
|
|
const OBJ_NAMESPACE = "\\allejo\\DaPulse\\"; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The default API protocol used for URL calls |
32
|
|
|
* |
33
|
|
|
* @internal |
34
|
|
|
*/ |
35
|
|
|
const API_PROTOCOL = "https"; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The API end point for URL calls |
39
|
|
|
* |
40
|
|
|
* @internal |
41
|
|
|
*/ |
42
|
|
|
const API_ENDPOINT = "api.dapulse.com"; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The API version used for URL calls |
46
|
|
|
* |
47
|
|
|
* @internal |
48
|
|
|
*/ |
49
|
|
|
const API_VERSION = "v1"; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The suffix that is appended to the URL to access functionality for certain objects |
53
|
|
|
* |
54
|
|
|
* @internal |
55
|
|
|
*/ |
56
|
|
|
const API_PREFIX = ""; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The API key used to make the URL calls |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected static $apiKey; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* When set to true, the object can only be constructed from an associative array of data. It will not attempt |
67
|
|
|
* to fetch the data with an API call; this is intended for objects are not directly accessible via the API. |
68
|
|
|
* |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
protected $arrayConstructionOnly = false; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set to true if the object has been deleted via an API call but the instance still exists. This variable will |
75
|
|
|
* prevent further API calls to a nonexistent object. |
76
|
|
|
* |
77
|
|
|
* @var bool |
78
|
|
|
*/ |
79
|
|
|
protected $deletedObject = false; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* An associative array representing the original JSON response from DaPulse |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $jsonResponse; |
87
|
|
|
|
88
|
|
|
protected $urlEndPoint; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The ID for the object we're handling |
92
|
|
|
* |
93
|
|
|
* @var int |
94
|
|
|
*/ |
95
|
|
|
protected $id; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create an object from an API call |
99
|
|
|
* |
100
|
|
|
* @param int|array $idOrArray Either the numerical ID of an object or an associative array representing a JSON |
101
|
|
|
* response from an API call |
102
|
|
|
* @param bool $lazyLoad When set to true, an initial API call will not be made. An API call will be made when |
103
|
|
|
* the information is requested |
104
|
|
|
* |
105
|
|
|
* @throws \InvalidArgumentException The specified object cannot be created directly from an API call but instead |
106
|
|
|
* requires an associative array of information gathered from other API calls. |
107
|
|
|
* |
108
|
|
|
* @since 0.1.0 |
109
|
|
|
*/ |
110
|
99 |
|
public function __construct ($idOrArray, $lazyLoad = false) |
111
|
|
|
{ |
112
|
99 |
|
$staticClass = explode("\\", get_called_class()); |
113
|
99 |
|
$staticClass = end($staticClass); |
114
|
|
|
|
115
|
99 |
|
if (is_null($idOrArray)) |
116
|
|
|
{ |
117
|
|
|
throw new \InvalidArgumentException("You may not initialize $staticClass with null."); |
118
|
|
|
} |
119
|
|
|
|
120
|
99 |
|
if (!is_array($idOrArray)) |
121
|
|
|
{ |
122
|
99 |
|
$this->urlEndPoint = sprintf("%s/%d.json", self::apiEndpoint(), $idOrArray); |
123
|
|
|
} |
124
|
|
|
|
125
|
99 |
|
if ($this->arrayConstructionOnly && !is_array($idOrArray)) |
126
|
|
|
{ |
127
|
|
|
throw new \InvalidArgumentException("A $staticClass cannot be fetched from an ID."); |
128
|
|
|
} |
129
|
|
|
|
130
|
99 |
|
$this->initializeValues(); |
131
|
|
|
|
132
|
99 |
|
if (is_array($idOrArray)) |
133
|
|
|
{ |
134
|
89 |
|
$this->jsonResponse = $idOrArray; |
135
|
89 |
|
$this->assignResults(); |
136
|
|
|
} |
137
|
|
|
else |
138
|
|
|
{ |
139
|
99 |
|
if ($lazyLoad) |
140
|
|
|
{ |
141
|
8 |
|
$this->id = $idOrArray; |
142
|
8 |
|
$this->jsonResponse = []; |
143
|
|
|
} |
144
|
|
|
else |
145
|
|
|
{ |
146
|
99 |
|
$this->lazyLoad(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
99 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function jsonSerialize () |
155
|
|
|
{ |
156
|
|
|
return $this->jsonResponse; |
157
|
|
|
} |
158
|
|
|
|
159
|
99 |
|
protected function lazyLoad () |
160
|
|
|
{ |
161
|
99 |
|
if (empty($this->jsonResponse)) |
162
|
|
|
{ |
163
|
99 |
|
$this->jsonResponse = $this::sendGet($this->urlEndPoint); |
|
|
|
|
164
|
99 |
|
$this->assignResults(); |
165
|
|
|
} |
166
|
99 |
|
} |
167
|
|
|
|
168
|
|
|
// ================================================================================================================ |
169
|
|
|
// Getter functions |
170
|
|
|
// ================================================================================================================ |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Access the JSON response from DaPulse directly |
174
|
|
|
* |
175
|
|
|
* @api |
176
|
|
|
* @deprecated 0.3.0 Feed this object to json_encode() to get the JSON representation of this object instead or call |
177
|
|
|
* `jsonSerialize()` |
178
|
|
|
* @since 0.1.0 |
179
|
|
|
* @todo Remove at 0.4.0 or next breaking release |
|
|
|
|
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
final public function getJson () |
183
|
|
|
{ |
184
|
|
|
return $this->jsonSerialize(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// ================================================================================================================ |
188
|
|
|
// Helper functions |
189
|
|
|
// ================================================================================================================ |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Assign an associative array from a JSON response and map them to instance variables |
193
|
|
|
* |
194
|
|
|
* @since 0.1.0 |
195
|
|
|
*/ |
196
|
99 |
|
final protected function assignResults () |
197
|
|
|
{ |
198
|
99 |
|
foreach ($this->jsonResponse as $key => $val) |
199
|
|
|
{ |
200
|
99 |
|
if (property_exists(get_called_class(), $key)) |
201
|
|
|
{ |
202
|
99 |
|
$this->$key = $val; |
203
|
|
|
} |
204
|
|
|
} |
205
|
99 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Check if the current object has been marked as deleted from DaPulse. If so, throw an exception. |
209
|
|
|
* |
210
|
|
|
* @throws InvalidObjectException |
211
|
|
|
*/ |
212
|
2 |
|
final protected function checkInvalid () |
213
|
|
|
{ |
214
|
2 |
|
if ($this->deletedObject) |
215
|
|
|
{ |
216
|
1 |
|
throw new InvalidObjectException("This object no longer exists on DaPulse", 2); |
217
|
|
|
} |
218
|
2 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Mark an object as deleted |
222
|
|
|
* |
223
|
|
|
* @internal |
224
|
|
|
*/ |
225
|
|
|
final public function _markInvalid () |
226
|
|
|
{ |
227
|
|
|
$this->deletedObject = true; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Store the value in an array if the value is not null. This function is a shortcut of setting values in an array |
232
|
|
|
* only if they are not null, if not leave them unset; used ideally for PUT requests. |
233
|
|
|
* |
234
|
|
|
* @param array $array The array that will store all of the POST parameters |
235
|
|
|
* @param string $name The name of the field |
236
|
|
|
* @param mixed $value The value to be stored in a given field |
237
|
|
|
*/ |
238
|
4 |
|
final protected static function setIfNotNullOrEmpty (&$array, $name, $value) |
239
|
|
|
{ |
240
|
4 |
|
if (!is_null($value) && !empty($value)) |
241
|
|
|
{ |
242
|
2 |
|
$array[$name] = $value; |
243
|
|
|
} |
244
|
4 |
|
} |
245
|
|
|
|
246
|
|
|
// ================================================================================================================ |
247
|
|
|
// Empty functions |
248
|
|
|
// ================================================================================================================ |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Overload this function if any class variables need to be initialized to a default value |
252
|
|
|
*/ |
253
|
89 |
|
protected function initializeValues () |
254
|
|
|
{ |
255
|
89 |
|
} |
256
|
|
|
|
257
|
|
|
// ================================================================================================================ |
258
|
|
|
// Lazy loading functions |
259
|
|
|
// ================================================================================================================ |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Inject data into the array that will be mapped into individual instance variables. This function must be called |
263
|
|
|
* **before** lazyCastAll() is called and maps the associative array to objects. |
264
|
|
|
* |
265
|
|
|
* @param array $target An array of associative arrays with data to be converted into objects |
266
|
|
|
* @param array $array An associative array containing data to be merged with the key being the name of the |
267
|
|
|
* instance variable. |
268
|
|
|
* |
269
|
|
|
* @throws \InvalidArgumentException If either parameters are not arrays |
270
|
|
|
* |
271
|
|
|
* @since 0.1.0 |
272
|
|
|
*/ |
273
|
38 |
|
final protected static function lazyInject (&$target, $array) |
274
|
|
|
{ |
275
|
38 |
|
if (!is_array($target) || !is_array($array)) |
276
|
|
|
{ |
277
|
|
|
throw new \InvalidArgumentException("Both the target and array must be arrays"); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// If the first element is an array, let's assume $target hasn't been lazily casted into objects |
281
|
38 |
|
if (is_array($target[0])) |
282
|
|
|
{ |
283
|
38 |
|
foreach ($target as &$element) |
284
|
|
|
{ |
285
|
38 |
|
$element = array_merge($element, $array); |
286
|
|
|
} |
287
|
|
|
} |
288
|
38 |
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Convert the specified array into an array of object types if needed |
292
|
|
|
* |
293
|
|
|
* @param string $objectType The class name of the Objects the items should be |
294
|
|
|
* @param array $array The array to check |
295
|
|
|
* |
296
|
|
|
* @since 0.2.0 |
297
|
|
|
*/ |
298
|
44 |
|
final protected static function lazyCastAll (&$array, $objectType) |
299
|
|
|
{ |
300
|
44 |
|
if (self::lazyCastNeededOnArray($objectType, $array)) |
301
|
|
|
{ |
302
|
40 |
|
$array = self::castArrayToObjectArray($objectType, $array); |
303
|
|
|
} |
304
|
44 |
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Convert the specified item into the specified object if needed |
308
|
|
|
* |
309
|
|
|
* @param mixed $target The item to check |
310
|
|
|
* @param string $objectType The class name of the Objects the items should be |
311
|
|
|
* |
312
|
|
|
* @since 0.2.0 |
313
|
|
|
*/ |
314
|
8 |
|
final protected static function lazyCast (&$target, $objectType) |
315
|
|
|
{ |
316
|
8 |
|
if (self::lazyCastNeeded($target, $objectType)) |
317
|
|
|
{ |
318
|
8 |
|
$object = ($objectType[0] == "\\") ? $objectType : self::OBJ_NAMESPACE . $objectType; |
319
|
8 |
|
$target = new $object($target); |
320
|
|
|
} |
321
|
8 |
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Check whether it is required for an array of JSON data to be converted into an array of the specified objects |
325
|
|
|
* |
326
|
|
|
* @param string $objectType The class name of the Objects the items should be |
327
|
|
|
* @param array $array The array to check |
328
|
|
|
* |
329
|
|
|
* @since 0.2.0 |
330
|
|
|
* |
331
|
|
|
* @return bool True if the array needs to converted into an array of objects |
332
|
|
|
*/ |
333
|
44 |
|
final protected static function lazyCastNeededOnArray ($objectType, $array) |
334
|
|
|
{ |
335
|
44 |
|
if (is_array($array) && count($array) == 0) |
336
|
|
|
{ |
337
|
1 |
|
return false; |
338
|
|
|
} |
339
|
|
|
|
340
|
43 |
|
$firstItem = $array[0]; |
341
|
|
|
|
342
|
43 |
|
return self::lazyCastNeeded($firstItem, $objectType); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Check if an individual item needs to be lazily converted into an object |
347
|
|
|
* |
348
|
|
|
* @param mixed $target The item to check |
349
|
|
|
* @param string $objectType The class name of the Objects the items should be |
350
|
|
|
* |
351
|
|
|
* @since 0.2.0 |
352
|
|
|
* |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
49 |
|
final protected static function lazyCastNeeded ($target, $objectType) |
356
|
|
|
{ |
357
|
49 |
|
$objectDefinition = ($objectType[0] === "\\") ? $objectType : self::OBJ_NAMESPACE . $objectType; |
358
|
|
|
|
359
|
49 |
|
return !($target instanceof $objectDefinition); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Sends a GET request for a JSON array and casts the response into an array of objects |
364
|
|
|
* |
365
|
|
|
* @param string $url The API endpoint to call to get the JSON response from |
366
|
|
|
* @param string $className The class name of the Object type to cast to |
367
|
|
|
* @param array $params An associative array of URL parameters that will be passed to the specific call. For |
368
|
|
|
* example, limiting the number of results or the pagination of results. **Warning** The |
369
|
|
|
* API key does NOT need to be passed here |
370
|
|
|
* |
371
|
|
|
* @since 0.2.0 |
372
|
|
|
* |
373
|
|
|
* @return array |
374
|
|
|
*/ |
375
|
26 |
|
final protected static function fetchAndCastToObjectArray ($url, $className, $params = []) |
376
|
|
|
{ |
377
|
26 |
|
$objects = self::sendGet($url, $params); |
378
|
|
|
|
379
|
26 |
|
return self::castArrayToObjectArray($className, $objects); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Convert an array of associative arrays into a specific object |
384
|
|
|
* |
385
|
|
|
* @param string $className The class name of the Object type |
386
|
|
|
* @param array $objects An associative array to be converted into an object |
387
|
|
|
* |
388
|
|
|
* @since 0.2.0 |
389
|
|
|
* |
390
|
|
|
* @return array An array of the specified objects |
391
|
|
|
*/ |
392
|
61 |
|
final protected static function castArrayToObjectArray ($className, $objects) |
393
|
|
|
{ |
394
|
61 |
|
$class = self::OBJ_NAMESPACE . $className; |
395
|
61 |
|
$array = []; |
396
|
|
|
|
397
|
61 |
|
foreach ($objects as $post) |
398
|
|
|
{ |
399
|
61 |
|
$array[] = new $class($post); |
400
|
|
|
} |
401
|
|
|
|
402
|
61 |
|
return $array; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
// ================================================================================================================ |
406
|
|
|
// URL jobs functions |
407
|
|
|
// ================================================================================================================ |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Send a GET request to fetch the data from the specified URL |
411
|
|
|
* |
412
|
|
|
* @param string $url The API endpoint to call |
413
|
|
|
* @param array $params An associative array of URL parameters that will be passed to the specific call. For |
414
|
|
|
* example, limiting the number of results or the pagination of results. **Warning** The API |
415
|
|
|
* key does NOT need to be passed here |
416
|
|
|
* |
417
|
|
|
* @since 0.1.0 |
418
|
|
|
* |
419
|
|
|
* @return mixed An associative array of the JSON response from DaPulse |
420
|
|
|
*/ |
421
|
99 |
|
final protected static function sendGet ($url, $params = []) |
422
|
|
|
{ |
423
|
99 |
|
$params["api_key"] = self::$apiKey; |
424
|
|
|
|
425
|
99 |
|
$urlQuery = new UrlQuery($url, $params); |
426
|
|
|
|
427
|
99 |
|
return $urlQuery->sendGet(); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Send a POST request to a specified URL |
432
|
|
|
* |
433
|
|
|
* @param string $url |
434
|
|
|
* @param array $postParams |
435
|
|
|
* @param array $getParams |
436
|
|
|
* |
437
|
|
|
* @since 0.1.0 |
438
|
|
|
* |
439
|
|
|
* @return mixed |
440
|
|
|
*/ |
441
|
7 |
|
final protected static function sendPost ($url, $postParams, $getParams = []) |
442
|
|
|
{ |
443
|
7 |
|
return self::sendRequest("POST", $url, $postParams, $getParams); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Send a PUT request to a specified URL |
448
|
|
|
* |
449
|
|
|
* @param string $url |
450
|
|
|
* @param array $postParams |
451
|
|
|
* @param array $getParams |
452
|
|
|
* |
453
|
|
|
* @since 0.1.0 |
454
|
|
|
* |
455
|
|
|
* @return mixed |
456
|
|
|
*/ |
457
|
9 |
|
final protected static function sendPut ($url, $postParams, $getParams = []) |
458
|
|
|
{ |
459
|
9 |
|
return self::sendRequest("PUT", $url, $postParams, $getParams); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Send a DELETE request to a specified URL |
464
|
|
|
* |
465
|
|
|
* @param string $url |
466
|
|
|
* @param array $getParams |
467
|
|
|
* |
468
|
|
|
* @since 0.1.0 |
469
|
|
|
* |
470
|
|
|
* @return mixed |
471
|
|
|
*/ |
472
|
5 |
|
final protected static function sendDelete ($url, $getParams = []) |
473
|
|
|
{ |
474
|
5 |
|
return self::sendRequest("DELETE", $url, null, $getParams); |
|
|
|
|
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Send the appropriate URL request |
479
|
|
|
* |
480
|
|
|
* @param string $type |
481
|
|
|
* @param string $url |
482
|
|
|
* @param array $postParams |
483
|
|
|
* @param array $getParams |
484
|
|
|
* |
485
|
|
|
* @throws \InvalidArgumentException if $type not 'POST', 'PUT', or 'DELETE' |
486
|
|
|
* |
487
|
|
|
* @since 0.1.0 |
488
|
|
|
* |
489
|
|
|
* @return mixed |
490
|
|
|
*/ |
491
|
21 |
|
private static function sendRequest ($type, $url, $postParams, $getParams) |
492
|
|
|
{ |
493
|
21 |
|
$getParams["api_key"] = self::$apiKey; |
494
|
|
|
|
495
|
21 |
|
$urlQuery = new UrlQuery($url, $getParams); |
496
|
|
|
|
497
|
|
|
switch ($type) |
498
|
|
|
{ |
499
|
21 |
|
case "POST": |
500
|
7 |
|
return $urlQuery->sendPost($postParams); |
501
|
|
|
|
502
|
14 |
|
case "PUT": |
503
|
9 |
|
return $urlQuery->sendPut($postParams); |
504
|
|
|
|
505
|
5 |
|
case "DELETE": |
506
|
5 |
|
return $urlQuery->sendDelete(); |
507
|
|
|
|
508
|
|
|
default: |
509
|
|
|
throw new \InvalidArgumentException(); |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
// ================================================================================================================ |
514
|
|
|
// API key functions |
515
|
|
|
// ================================================================================================================ |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Get the base URL to use in all of the API calls |
519
|
|
|
* |
520
|
|
|
* @param string|null $apiPrefix If the API end point is different from the class's constant, this value will be |
521
|
|
|
* used as the suffix for the API endpoint |
522
|
|
|
* |
523
|
|
|
* @since 0.1.0 |
524
|
|
|
* |
525
|
|
|
* @return string The base URL to call |
526
|
|
|
*/ |
527
|
99 |
|
final protected static function apiEndpoint ($apiPrefix = null) |
528
|
|
|
{ |
529
|
99 |
|
$apiSection = isset($apiPrefix) ? $apiPrefix : static::API_PREFIX; |
530
|
|
|
|
531
|
99 |
|
return sprintf("%s://%s/%s/%s", self::API_PROTOCOL, self::API_ENDPOINT, self::API_VERSION, $apiSection); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* Set the API for all calls to the API |
536
|
|
|
* |
537
|
|
|
* @param string $apiKey The API key used to access the DaPulse API |
538
|
|
|
* |
539
|
|
|
* @since 0.1.0 |
540
|
|
|
*/ |
541
|
99 |
|
final public static function setApiKey ($apiKey) |
542
|
|
|
{ |
543
|
99 |
|
self::$apiKey = $apiKey; |
544
|
99 |
|
} |
545
|
|
|
} |
546
|
|
|
|
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..