1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client; |
6
|
|
|
|
7
|
|
|
use Swis\JsonApi\Client\Exceptions\UnsupportedDataException; |
8
|
|
|
use Swis\JsonApi\Client\Interfaces\DataInterface; |
9
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentInterface; |
10
|
|
|
use Swis\JsonApi\Client\Interfaces\ItemInterface; |
11
|
|
|
|
12
|
|
|
class DocumentFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data |
16
|
|
|
* |
17
|
|
|
* @return \Swis\JsonApi\Client\ItemDocument|\Swis\JsonApi\Client\CollectionDocument |
18
|
|
|
*/ |
19
|
56 |
|
public function make(DataInterface $data): DocumentInterface |
20
|
|
|
{ |
21
|
56 |
|
if ($data instanceof ItemInterface) { |
22
|
48 |
|
$document = new ItemDocument(); |
23
|
8 |
|
} elseif ($data instanceof Collection) { |
24
|
8 |
|
$document = new CollectionDocument(); |
25
|
|
|
} else { |
26
|
|
|
throw new UnsupportedDataException(sprintf('%s is not supported as input', get_class($data))); |
27
|
|
|
} |
28
|
|
|
|
29
|
56 |
|
return $document->setData($data)->setIncluded($this->getIncluded($data)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data |
34
|
|
|
* |
35
|
|
|
* @return \Swis\JsonApi\Client\Collection |
36
|
|
|
*/ |
37
|
56 |
|
private function getIncluded(DataInterface $data): Collection |
38
|
|
|
{ |
39
|
56 |
|
return Collection::wrap($data) |
|
|
|
|
40
|
56 |
|
->flatMap(fn (ItemInterface $item) => $this->getIncludedFromItem($item)) |
41
|
56 |
|
->unique(static fn (ItemInterface $item) => sprintf('%s:%s', $item->getType(), $item->getId())) |
42
|
56 |
|
->values(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item |
47
|
|
|
* |
48
|
|
|
* @return \Swis\JsonApi\Client\Collection |
49
|
|
|
*/ |
50
|
56 |
|
private function getIncludedFromItem(ItemInterface $item): Collection |
51
|
|
|
{ |
52
|
56 |
|
return Collection::make($item->getRelations()) |
|
|
|
|
53
|
56 |
|
->reject( |
54
|
56 |
|
static function ($relationship) { |
55
|
|
|
/* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */ |
56
|
40 |
|
return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded(); |
57
|
28 |
|
} |
58
|
|
|
) |
59
|
56 |
|
->flatMap( |
60
|
56 |
|
static function ($relationship) { |
61
|
|
|
/* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */ |
62
|
36 |
|
return Collection::wrap($relationship->getIncluded()); |
|
|
|
|
63
|
28 |
|
} |
64
|
|
|
) |
65
|
56 |
|
->flatMap(fn (ItemInterface $item) => Collection::wrap($item)->merge($this->getIncludedFromItem($item))) |
|
|
|
|
66
|
56 |
|
->filter(fn (ItemInterface $item) => $this->itemCanBeIncluded($item)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
36 |
|
private function itemCanBeIncluded(ItemInterface $item): bool |
75
|
|
|
{ |
76
|
36 |
|
return $item->hasType() |
77
|
36 |
|
&& $item->hasId() |
78
|
36 |
|
&& ($item->hasAttributes() || $item->hasRelationships()); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|