1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Error.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:JsonAPIClient! |
9
|
|
|
* @subpackage Objects |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 05.05.18 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\JsonAPIClient\Objects; |
18
|
|
|
|
19
|
|
|
use Neomerx\JsonApi\Contracts\Document\ErrorInterface; |
20
|
|
|
use Neomerx\JsonApi\Contracts\Document\LinkInterface; |
21
|
|
|
use Neomerx\JsonApi\Document\Link; |
22
|
|
|
use Neomerx\JsonApi\Exceptions\ErrorCollection; |
23
|
|
|
|
24
|
|
|
use IPub\JsonAPIClient\Exceptions; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Error |
28
|
|
|
* |
29
|
|
|
* @package CloudCreativity\LaravelJsonApi |
30
|
|
|
*/ |
31
|
|
|
class Error implements IMutableError |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var int|string|NULL |
35
|
|
|
*/ |
36
|
|
|
private $id = NULL; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $links = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string|NULL |
45
|
|
|
*/ |
46
|
|
|
private $status = NULL; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string|NULL |
50
|
|
|
*/ |
51
|
|
|
private $code = NULL; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string|NULL |
55
|
|
|
*/ |
56
|
|
|
private $title = NULL; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string|NULL |
60
|
|
|
*/ |
61
|
|
|
private $detail = NULL; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private $source = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array|NULL |
70
|
|
|
*/ |
71
|
|
|
private $meta = NULL; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $error |
75
|
|
|
* |
76
|
|
|
* @return Error |
77
|
|
|
*/ |
78
|
|
|
public static function cast($error) : Error |
79
|
|
|
{ |
80
|
|
|
if ($error instanceof self) { |
81
|
|
|
return $error; |
82
|
|
|
|
83
|
|
|
} elseif (!$error instanceof ErrorInterface) { |
|
|
|
|
84
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting an error object.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new self( |
88
|
|
|
$error->getId(), |
89
|
|
|
$error->getLinks(), |
90
|
|
|
$error->getStatus(), |
91
|
|
|
$error->getCode(), |
92
|
|
|
$error->getTitle(), |
93
|
|
|
$error->getDetail(), |
94
|
|
|
$error->getSource(), |
95
|
|
|
$error->getMeta() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create an error object from an array |
101
|
|
|
* |
102
|
|
|
* @param array $input |
103
|
|
|
* |
104
|
|
|
* @return Error |
105
|
|
|
*/ |
106
|
|
|
public static function create(array $input = []) : Error |
107
|
|
|
{ |
108
|
|
|
$error = new self(); |
109
|
|
|
$error->exchangeArray($input); |
110
|
|
|
|
111
|
|
|
return $error; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create an error collection from an array of error arrays. |
116
|
|
|
* |
117
|
|
|
* @param array $input |
118
|
|
|
* |
119
|
|
|
* @return ErrorCollection |
120
|
|
|
*/ |
121
|
|
|
public static function createMany(array $input) : ErrorCollection |
122
|
|
|
{ |
123
|
|
|
$errors = new ErrorCollection; |
124
|
|
|
|
125
|
|
|
foreach ($input as $item) { |
126
|
|
|
$errors->add(self::create((array) $item)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $errors; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param int|string|NULL $id |
134
|
|
|
* @param array|NULL $links |
135
|
|
|
* @param int|string|NULL $status |
136
|
|
|
* @param int|string|NULL $code |
137
|
|
|
* @param string|NULL $title |
138
|
|
|
* @param string|NULL $detail |
139
|
|
|
* @param array|NULL $source |
140
|
|
|
* @param array|NULL $meta |
141
|
|
|
*/ |
142
|
|
|
public function __construct( |
143
|
|
|
$id = NULL, |
144
|
|
|
array $links = NULL, |
145
|
|
|
$status = NULL, |
146
|
|
|
$code = NULL, |
147
|
|
|
?string $title = NULL, |
148
|
|
|
?string $detail = NULL, |
149
|
|
|
array $source = NULL, |
150
|
|
|
array $meta = NULL |
151
|
|
|
) { |
152
|
|
|
$this->setId($id); |
153
|
|
|
$this->setLinks($links); |
154
|
|
|
$this->setStatus($status); |
155
|
|
|
$this->setCode($code); |
156
|
|
|
$this->setTitle($title); |
157
|
|
|
$this->setDetail($detail); |
158
|
|
|
$this->setSource($source); |
159
|
|
|
$this->setMeta($meta); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function setId($id) : void |
166
|
|
|
{ |
167
|
|
|
if (!is_int($id) && !is_string($id) && !is_null($id)) { |
168
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting error id to be a string, integer or null.'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->id = $id; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
|
|
public function getId() |
178
|
|
|
{ |
179
|
|
|
return $this->id; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
public function hasId() : bool |
186
|
|
|
{ |
187
|
|
|
return $this->id !== NULL; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function setLinks(array $links = NULL) : void |
194
|
|
|
{ |
195
|
|
|
$this->links = []; |
196
|
|
|
|
197
|
|
|
if ($links) { |
198
|
|
|
$this->addLinks($links); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritdoc} |
204
|
|
|
*/ |
205
|
|
|
public function addLinks(array $links = NULL) : void |
206
|
|
|
{ |
207
|
|
|
foreach ((array) $links as $key => $link) { |
208
|
|
|
if (is_string($link)) { |
209
|
|
|
$link = new Link($link, NULL, TRUE); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (!$link instanceof LinkInterface) { |
|
|
|
|
213
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting links to contain link objects.'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->addLink($key, $link); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function addLink(string $key, LinkInterface $link) : void |
224
|
|
|
{ |
225
|
|
|
$this->links[$key] = $link; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritdoc} |
230
|
|
|
*/ |
231
|
|
|
public function setAboutLink(LinkInterface $link) : void |
232
|
|
|
{ |
233
|
|
|
$this->addLink(self::LINKS_ABOUT, $link); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@inheritdoc} |
238
|
|
|
*/ |
239
|
|
|
public function getLinks() : ?array |
240
|
|
|
{ |
241
|
|
|
return $this->links !== [] ? $this->links : NULL; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* {@inheritdoc} |
246
|
|
|
*/ |
247
|
|
|
public function setStatus($status) : void |
248
|
|
|
{ |
249
|
|
|
if (!is_int($status) && !is_string($status) && !is_null($status)) { |
250
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting error status to be a string, integer or null.'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$this->status = (string) $status; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function getStatus() : ?string |
260
|
|
|
{ |
261
|
|
|
return $this->hasStatus() ? (string) $this->status : NULL; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
|
|
public function hasStatus() : bool |
268
|
|
|
{ |
269
|
|
|
return $this->status !== NULL; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* {@inheritdoc} |
274
|
|
|
*/ |
275
|
|
|
public function setCode($code) : void |
276
|
|
|
{ |
277
|
|
|
if (!is_string($code) && !is_int($code) && !is_null($code)) { |
278
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting error code to be a string, integer or null.'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$this->code = (string) $code; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* {@inheritdoc} |
286
|
|
|
*/ |
287
|
|
|
public function getCode() : ?string |
288
|
|
|
{ |
289
|
|
|
return $this->hasCode() ? $this->code : NULL; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* {@inheritdoc} |
294
|
|
|
*/ |
295
|
|
|
public function hasCode() : bool |
296
|
|
|
{ |
297
|
|
|
return $this->code !== NULL; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* {@inheritdoc} |
302
|
|
|
*/ |
303
|
|
|
public function setTitle(?string $title) : void |
304
|
|
|
{ |
305
|
|
|
if (!is_string($title) && !is_null($title)) { |
306
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting error title to be a string or null.'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$this->title = $title; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* {@inheritdoc} |
314
|
|
|
*/ |
315
|
|
|
public function getTitle() : ?string |
316
|
|
|
{ |
317
|
|
|
return $this->hasTitle() ? $this->title : NULL; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* {@inheritdoc} |
322
|
|
|
*/ |
323
|
|
|
public function hasTitle() : bool |
324
|
|
|
{ |
325
|
|
|
return $this->title !== NULL; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* {@inheritdoc} |
330
|
|
|
*/ |
331
|
|
|
public function setDetail(?string $detail) : void |
332
|
|
|
{ |
333
|
|
|
if (!is_string($detail) && !is_null($detail)) { |
334
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting error detail to be a string or null.'); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
$this->detail = $detail; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* {@inheritdoc} |
342
|
|
|
*/ |
343
|
|
|
public function getDetail() : ?string |
344
|
|
|
{ |
345
|
|
|
return $this->hasDetail() ? $this->detail : NULL; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* {@inheritdoc} |
350
|
|
|
*/ |
351
|
|
|
public function hasDetail() : bool |
352
|
|
|
{ |
353
|
|
|
return $this->detail !== NULL; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* {@inheritdoc} |
358
|
|
|
*/ |
359
|
|
|
public function setSource(array $source = NULL) : void |
360
|
|
|
{ |
361
|
|
|
$this->source = (array) $source; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* {@inheritdoc} |
366
|
|
|
*/ |
367
|
|
|
public function getSource() : ?array |
368
|
|
|
{ |
369
|
|
|
return $this->source !== [] ? $this->source : NULL; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* {@inheritdoc} |
374
|
|
|
*/ |
375
|
|
|
public function setSourcePointer(?string $pointer) : void |
376
|
|
|
{ |
377
|
|
|
if (!is_string($pointer) && !is_null($pointer)) { |
378
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting error source pointer to be a string or null'); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
if ($pointer === NULL) { |
382
|
|
|
unset($this->source[self::SOURCE_POINTER]); |
383
|
|
|
|
384
|
|
|
} else { |
385
|
|
|
$this->source[self::SOURCE_POINTER] = $pointer; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* {@inheritdoc} |
391
|
|
|
*/ |
392
|
|
|
public function getSourcePointer() : ?string |
393
|
|
|
{ |
394
|
|
|
return $this->hasSourcePointer() ? $this->source[self::SOURCE_POINTER] : NULL; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* {@inheritdoc} |
399
|
|
|
*/ |
400
|
|
|
public function hasSourcePointer() : bool |
401
|
|
|
{ |
402
|
|
|
return isset($this->source[self::SOURCE_POINTER]); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* {@inheritdoc} |
407
|
|
|
*/ |
408
|
|
|
public function setSourceParameter(?string $parameter) : void |
409
|
|
|
{ |
410
|
|
|
if (!is_string($parameter) && !is_null($parameter)) { |
411
|
|
|
throw new Exceptions\InvalidArgumentException('Expecting source parameter to be a string or null'); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
if ($parameter === NULL) { |
415
|
|
|
unset($this->source[self::SOURCE_PARAMETER]); |
416
|
|
|
|
417
|
|
|
} else { |
418
|
|
|
$this->source[self::SOURCE_PARAMETER] = $parameter; |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* {@inheritdoc} |
424
|
|
|
*/ |
425
|
|
|
public function getSourceParameter() : ?string |
426
|
|
|
{ |
427
|
|
|
return $this->hasSourceParameter() ? $this->source[self::SOURCE_PARAMETER] : NULL; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* {@inheritdoc} |
432
|
|
|
*/ |
433
|
|
|
public function hasSourceParameter() : bool |
434
|
|
|
{ |
435
|
|
|
return isset($this->source[self::SOURCE_PARAMETER]); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* {@inheritdoc} |
440
|
|
|
*/ |
441
|
|
|
public function setMeta(?array $meta = []) : void |
442
|
|
|
{ |
443
|
|
|
$this->meta = $meta; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* {@inheritdoc} |
448
|
|
|
*/ |
449
|
|
|
public function addMeta(array $meta) : void |
450
|
|
|
{ |
451
|
|
|
$this->meta = $this->meta !== NULL ? array_replace_recursive($this->meta, $meta) : $meta; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* {@inheritdoc} |
456
|
|
|
*/ |
457
|
|
|
public function getMeta() : ?array |
458
|
|
|
{ |
459
|
|
|
return $this->meta; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* {@inheritdoc} |
464
|
|
|
*/ |
465
|
|
|
public function merge(ErrorInterface $error) : void |
466
|
|
|
{ |
467
|
|
|
// Id |
468
|
|
|
if ($error->getId()) { |
469
|
|
|
$this->setId($error->getId()); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
// Links |
473
|
|
|
if ($error->getLinks()) { |
474
|
|
|
$this->addLinks($error->getLinks()); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
// Status |
478
|
|
|
if ($error->getStatus()) { |
479
|
|
|
$this->setStatus($error->getStatus()); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
// Code |
483
|
|
|
if ($error->getCode()) { |
484
|
|
|
$this->setCode($error->getCode()); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
// Title |
488
|
|
|
if ($error->getTitle()) { |
489
|
|
|
$this->setTitle($error->getTitle()); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
// Detail |
493
|
|
|
if ($error->getDetail()) { |
494
|
|
|
$this->setDetail($error->getDetail()); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
// Source |
498
|
|
|
if ($error->getSource()) { |
499
|
|
|
$this->setSource($error->getSource()); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
// Meta |
503
|
|
|
if ($error->getMeta()) { |
504
|
|
|
$this->addMeta($error->getMeta()); |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* {@inheritdoc} |
510
|
|
|
*/ |
511
|
|
|
public function exchangeArray(array $input) : void |
512
|
|
|
{ |
513
|
|
|
// Id |
514
|
|
|
if (array_key_exists(self::ID, $input)) { |
515
|
|
|
$this->setId($input[self::ID]); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
// Links |
519
|
|
|
if (array_key_exists(self::LINKS, $input)) { |
520
|
|
|
$this->addLinks((array) $input[self::LINKS]); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
// About Link |
524
|
|
|
if (array_key_exists(self::LINKS_ABOUT, $input) && $input[self::LINKS_ABOUT] instanceof LinkInterface) { |
|
|
|
|
525
|
|
|
$this->setAboutLink($input[self::LINKS_ABOUT]); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
// Status |
529
|
|
|
if (array_key_exists(self::STATUS, $input)) { |
530
|
|
|
$this->setStatus($input[self::STATUS]); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
// Code |
534
|
|
|
if (array_key_exists(self::CODE, $input)) { |
535
|
|
|
$this->setCode($input[self::CODE]); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
// Title |
539
|
|
|
if (array_key_exists(self::TITLE, $input)) { |
540
|
|
|
$this->setTitle($input[self::TITLE]); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
// Detail |
544
|
|
|
if (array_key_exists(self::DETAIL, $input)) { |
545
|
|
|
$this->setDetail($input[self::DETAIL]); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
// Source |
549
|
|
|
if (array_key_exists(self::SOURCE, $input)) { |
550
|
|
|
$this->setSource((array) $input[self::SOURCE]); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
// Source Pointer |
554
|
|
|
if (array_key_exists(self::SOURCE_POINTER, $input)) { |
555
|
|
|
$this->setSourcePointer($input[self::SOURCE_POINTER]); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
// Source Parameter |
559
|
|
|
if (array_key_exists(self::SOURCE_PARAMETER, $input)) { |
560
|
|
|
$this->setSourceParameter($input[self::SOURCE_PARAMETER]); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
// Meta |
564
|
|
|
if (array_key_exists(self::META, $input)) { |
565
|
|
|
$this->addMeta((array) $input[self::META]); |
566
|
|
|
} |
567
|
|
|
} |
568
|
|
|
} |
569
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.