|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client\Parsers; |
|
6
|
|
|
|
|
7
|
|
|
use Swis\JsonApi\Client\Collection; |
|
8
|
|
|
use Swis\JsonApi\Client\Exceptions\ValidationException; |
|
9
|
|
|
use Swis\JsonApi\Client\Link; |
|
10
|
|
|
use Swis\JsonApi\Client\Links; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
class LinksParser |
|
16
|
|
|
{ |
|
17
|
|
|
public const SOURCE_DOCUMENT = 'document'; |
|
18
|
|
|
|
|
19
|
|
|
public const SOURCE_ERROR = 'error'; |
|
20
|
|
|
|
|
21
|
|
|
public const SOURCE_ITEM = 'item'; |
|
22
|
|
|
|
|
23
|
|
|
public const SOURCE_RELATIONSHIP = 'relationship'; |
|
24
|
|
|
|
|
25
|
|
|
private const LINKS_THAT_MAY_NOT_BE_NULL_WHEN_PRESENT = [ |
|
26
|
|
|
'self', |
|
27
|
|
|
'related', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
private MetaParser $metaParser; |
|
31
|
|
|
|
|
32
|
508 |
|
public function __construct(MetaParser $metaParser) |
|
33
|
|
|
{ |
|
34
|
508 |
|
$this->metaParser = $metaParser; |
|
35
|
254 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param mixed $data |
|
39
|
|
|
*/ |
|
40
|
120 |
|
public function parse($data, string $source): Links |
|
41
|
|
|
{ |
|
42
|
120 |
|
if (! is_object($data)) { |
|
43
|
24 |
|
throw new ValidationException(sprintf('Links MUST be an object, "%s" given.', gettype($data))); |
|
44
|
|
|
} |
|
45
|
96 |
|
if ($source === self::SOURCE_RELATIONSHIP && ! property_exists($data, 'self') && ! property_exists($data, 'related')) { |
|
46
|
4 |
|
throw new ValidationException('Relationship links object MUST contain at least one of the following properties: `self`, `related`.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
92 |
|
return new Links( |
|
50
|
92 |
|
Collection::wrap((array) $data) |
|
51
|
92 |
|
->map(fn ($link, $name) => $this->buildLink($link, $name)) |
|
52
|
78 |
|
->toArray() |
|
53
|
46 |
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param mixed $data |
|
58
|
|
|
*/ |
|
59
|
92 |
|
private function buildLink($data, string $name): ?Link |
|
60
|
|
|
{ |
|
61
|
92 |
|
if (in_array($name, self::LINKS_THAT_MAY_NOT_BE_NULL_WHEN_PRESENT, true) && ! is_string($data) && ! is_object($data)) { |
|
62
|
8 |
|
throw new ValidationException(sprintf('Link "%s" MUST be an object or string, "%s" given.', $name, gettype($data))); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
84 |
|
if ($data === null) { |
|
66
|
4 |
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
84 |
|
if (is_string($data)) { |
|
70
|
60 |
|
return new Link($data); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
28 |
|
if (! is_object($data)) { |
|
74
|
16 |
|
throw new ValidationException(sprintf('Link "%s" MUST be an object, string or null, "%s" given.', $name, gettype($data))); |
|
75
|
|
|
} |
|
76
|
12 |
|
if (! property_exists($data, 'href')) { |
|
77
|
4 |
|
throw new ValidationException(sprintf('Link "%s" MUST have a "href" attribute.', $name)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
8 |
|
return new Link($data->href, property_exists($data, 'meta') ? $this->metaParser->parse($data->meta) : null); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|