|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swis\JsonApi\Client\Parsers; |
|
4
|
|
|
|
|
5
|
|
|
use Art4\JsonApiClient\RelationshipCollectionInterface; |
|
6
|
|
|
use Art4\JsonApiClient\ResourceIdentifierCollectionInterface; |
|
7
|
|
|
use Art4\JsonApiClient\ResourceIdentifierInterface; |
|
8
|
|
|
use Art4\JsonApiClient\ResourceItemInterface; |
|
9
|
|
|
use Swis\JsonApi\Client\Collection; |
|
10
|
|
|
use Swis\JsonApi\Client\Interfaces\DataInterface; |
|
11
|
|
|
use Swis\JsonApi\Client\Interfaces\ItemInterface; |
|
12
|
|
|
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface; |
|
13
|
|
|
use Swis\JsonApi\Client\Item; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
|
|
class ItemParser |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Swis\JsonApi\Client\Interfaces\TypeMapperInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $typeMapper; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Swis\JsonApi\Client\Parsers\LinksParser |
|
27
|
|
|
*/ |
|
28
|
|
|
private $linksParser; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Swis\JsonApi\Client\Parsers\MetaParser |
|
32
|
|
|
*/ |
|
33
|
|
|
private $metaParser; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface $typeMapper |
|
37
|
|
|
* @param \Swis\JsonApi\Client\Parsers\LinksParser $linksParser |
|
38
|
|
|
* @param \Swis\JsonApi\Client\Parsers\MetaParser $metaParser |
|
39
|
|
|
*/ |
|
40
|
130 |
|
public function __construct(TypeMapperInterface $typeMapper, LinksParser $linksParser, MetaParser $metaParser) |
|
41
|
|
|
{ |
|
42
|
130 |
|
$this->typeMapper = $typeMapper; |
|
43
|
130 |
|
$this->linksParser = $linksParser; |
|
44
|
130 |
|
$this->metaParser = $metaParser; |
|
45
|
130 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param \Art4\JsonApiClient\ResourceItemInterface $jsonApiItem |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface |
|
51
|
|
|
*/ |
|
52
|
75 |
|
public function parse(ResourceItemInterface $jsonApiItem): ItemInterface |
|
53
|
|
|
{ |
|
54
|
75 |
|
$item = $this->getItemInstance($jsonApiItem->get('type')); |
|
55
|
|
|
|
|
56
|
75 |
|
if ($jsonApiItem->has('id')) { |
|
57
|
75 |
|
$item->setId($jsonApiItem->get('id')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
75 |
|
if ($jsonApiItem->has('attributes')) { |
|
61
|
75 |
|
$item->fill($jsonApiItem->get('attributes')->asArray(true)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
75 |
|
if ($jsonApiItem->has('relationships')) { |
|
65
|
60 |
|
$this->setRelations($item, $jsonApiItem->get('relationships')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
75 |
|
if ($jsonApiItem->has('links')) { |
|
69
|
50 |
|
$item->setLinks($this->linksParser->parse($jsonApiItem->get('links')->asArray())); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
75 |
|
if ($jsonApiItem->has('meta')) { |
|
73
|
50 |
|
$item->setMeta($this->metaParser->parse($jsonApiItem->get('meta'))); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
75 |
|
return $item; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $type |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface |
|
83
|
|
|
*/ |
|
84
|
75 |
|
private function getItemInstance(string $type): ItemInterface |
|
85
|
|
|
{ |
|
86
|
75 |
|
if ($this->typeMapper->hasMapping($type)) { |
|
87
|
50 |
|
return $this->typeMapper->getMapping($type); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
25 |
|
return (new Item())->setType($type); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item |
|
95
|
|
|
* @param \Art4\JsonApiClient\RelationshipCollectionInterface $relationships |
|
96
|
|
|
*/ |
|
97
|
60 |
|
private function setRelations(ItemInterface $item, RelationshipCollectionInterface $relationships): void |
|
98
|
|
|
{ |
|
99
|
|
|
/** @var \Art4\JsonApiClient\RelationshipInterface $relationship */ |
|
100
|
60 |
|
foreach ($relationships->asArray() as $name => $relationship) { |
|
101
|
60 |
|
$data = new Collection(); |
|
102
|
60 |
|
if ($relationship->has('data')) { |
|
103
|
60 |
|
$data = $this->parseRelationshipData($relationship->get('data')); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
60 |
|
$links = null; |
|
107
|
60 |
|
if ($relationship->has('links')) { |
|
108
|
50 |
|
$links = $this->linksParser->parse($relationship->get('links')->asArray()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
60 |
|
$meta = null; |
|
112
|
60 |
|
if ($relationship->has('meta')) { |
|
113
|
50 |
|
$meta = $this->metaParser->parse($relationship->get('meta')); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
60 |
|
$item->setRelation($name, $data, $links, $meta); |
|
117
|
|
|
} |
|
118
|
60 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param \Art4\JsonApiClient\ResourceIdentifierInterface|\Art4\JsonApiClient\ResourceIdentifierCollectionInterface $data |
|
122
|
|
|
* |
|
123
|
|
|
* @throws \InvalidArgumentException |
|
124
|
|
|
* |
|
125
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DataInterface |
|
126
|
|
|
*/ |
|
127
|
60 |
|
private function parseRelationshipData($data): DataInterface |
|
128
|
|
|
{ |
|
129
|
60 |
|
if ($data instanceof ResourceIdentifierInterface) { |
|
130
|
55 |
|
return $this->getItemInstance($data->get('type')) |
|
131
|
55 |
|
->setId($data->get('id')); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
55 |
|
if ($data instanceof ResourceIdentifierCollectionInterface) { |
|
|
|
|
|
|
135
|
55 |
|
return Collection::make($data->asArray()) |
|
136
|
55 |
|
->map( |
|
137
|
22 |
|
function (ResourceIdentifierInterface $identifier) { |
|
138
|
55 |
|
return $this->getItemInstance($identifier->get('type')) |
|
139
|
55 |
|
->setId($identifier->get('id')); |
|
140
|
55 |
|
} |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
throw new \InvalidArgumentException(sprintf('Expected either %s or %s', ResourceIdentifierInterface::class, ResourceIdentifierCollectionInterface::class)); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|