|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client\Parsers; |
|
6
|
|
|
|
|
7
|
|
|
use Swis\JsonApi\Client\Error; |
|
8
|
|
|
use Swis\JsonApi\Client\ErrorSource; |
|
9
|
|
|
use Swis\JsonApi\Client\Exceptions\ValidationException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
class ErrorParser |
|
15
|
|
|
{ |
|
16
|
|
|
private LinksParser $linksParser; |
|
17
|
|
|
|
|
18
|
|
|
private MetaParser $metaParser; |
|
19
|
|
|
|
|
20
|
372 |
|
public function __construct(LinksParser $linksParser, MetaParser $metaParser) |
|
21
|
|
|
{ |
|
22
|
372 |
|
$this->linksParser = $linksParser; |
|
23
|
372 |
|
$this->metaParser = $metaParser; |
|
24
|
186 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param mixed $data |
|
28
|
|
|
*/ |
|
29
|
224 |
|
public function parse($data): Error |
|
30
|
|
|
{ |
|
31
|
224 |
|
if (! is_object($data)) { |
|
32
|
24 |
|
throw new ValidationException(sprintf('Error MUST be an object, "%s" given.', gettype($data))); |
|
33
|
|
|
} |
|
34
|
200 |
|
if (property_exists($data, 'id') && ! is_string($data->id)) { |
|
35
|
24 |
|
throw new ValidationException(sprintf('Error property "id" MUST be a string, "%s" given.', gettype($data->id))); |
|
36
|
|
|
} |
|
37
|
176 |
|
if (property_exists($data, 'status') && ! is_string($data->status)) { |
|
38
|
24 |
|
throw new ValidationException(sprintf('Error property "status" MUST be a string, "%s" given.', gettype($data->status))); |
|
39
|
|
|
} |
|
40
|
152 |
|
if (property_exists($data, 'code') && ! is_string($data->code)) { |
|
41
|
24 |
|
throw new ValidationException(sprintf('Error property "code" MUST be a string, "%s" given.', gettype($data->code))); |
|
42
|
|
|
} |
|
43
|
128 |
|
if (property_exists($data, 'title') && ! is_string($data->title)) { |
|
44
|
24 |
|
throw new ValidationException(sprintf('Error property "title" MUST be a string, "%s" given.', gettype($data->title))); |
|
45
|
|
|
} |
|
46
|
104 |
|
if (property_exists($data, 'detail') && ! is_string($data->detail)) { |
|
47
|
24 |
|
throw new ValidationException(sprintf('Error property "detail" MUST be a string, "%s" given.', gettype($data->detail))); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
80 |
|
return new Error( |
|
51
|
80 |
|
property_exists($data, 'id') ? $data->id : null, |
|
52
|
80 |
|
property_exists($data, 'links') ? $this->linksParser->parse($data->links, LinksParser::SOURCE_ERROR) : null, |
|
53
|
80 |
|
property_exists($data, 'status') ? $data->status : null, |
|
54
|
80 |
|
property_exists($data, 'code') ? $data->code : null, |
|
55
|
80 |
|
property_exists($data, 'title') ? $data->title : null, |
|
56
|
80 |
|
property_exists($data, 'detail') ? $data->detail : null, |
|
57
|
80 |
|
property_exists($data, 'source') ? $this->buildErrorSource($data->source) : null, |
|
58
|
44 |
|
property_exists($data, 'meta') ? $this->metaParser->parse($data->meta) : null |
|
59
|
40 |
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param mixed $data |
|
64
|
|
|
*/ |
|
65
|
76 |
|
private function buildErrorSource($data): ErrorSource |
|
66
|
|
|
{ |
|
67
|
76 |
|
if (! is_object($data)) { |
|
68
|
24 |
|
throw new ValidationException(sprintf('ErrorSource MUST be an object, "%s" given.', gettype($data))); |
|
69
|
|
|
} |
|
70
|
52 |
|
if (property_exists($data, 'pointer') && ! is_string($data->pointer)) { |
|
71
|
24 |
|
throw new ValidationException(sprintf('ErrorSource property "pointer" MUST be a string, "%s" given.', gettype($data->pointer))); |
|
72
|
|
|
} |
|
73
|
28 |
|
if (property_exists($data, 'parameter') && ! is_string($data->parameter)) { |
|
74
|
24 |
|
throw new ValidationException(sprintf('ErrorSource property "parameter" MUST be a string, "%s" given.', gettype($data->parameter))); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
return new ErrorSource( |
|
78
|
4 |
|
property_exists($data, 'pointer') ? $data->pointer : null, |
|
79
|
4 |
|
property_exists($data, 'parameter') ? $data->parameter : null |
|
80
|
2 |
|
); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|