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 | * @return \Swis\JsonApi\Client\ItemDocument|\Swis\JsonApi\Client\CollectionDocument |
||||
16 | */ |
||||
17 | 56 | public function make(DataInterface $data): DocumentInterface |
|||
18 | { |
||||
19 | 56 | if ($data instanceof ItemInterface) { |
|||
20 | 48 | $document = new ItemDocument; |
|||
21 | 8 | } elseif ($data instanceof Collection) { |
|||
22 | 8 | $document = new CollectionDocument; |
|||
23 | } else { |
||||
24 | throw new UnsupportedDataException(sprintf('%s is not supported as input', get_class($data))); |
||||
25 | } |
||||
26 | |||||
27 | 56 | return $document->setData($data)->setIncluded($this->getIncluded($data)); |
|||
28 | } |
||||
29 | |||||
30 | 56 | private function getIncluded(DataInterface $data): Collection |
|||
31 | { |
||||
32 | 56 | return Collection::wrap($data) |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
33 | 56 | ->flatMap(fn (ItemInterface $item) => $this->getIncludedFromItem($item)) |
|||
34 | 56 | ->unique(static fn (ItemInterface $item) => sprintf('%s:%s', $item->getType(), $item->getId())) |
|||
35 | 56 | ->values(); |
|||
36 | } |
||||
37 | |||||
38 | 56 | private function getIncludedFromItem(ItemInterface $item): Collection |
|||
39 | { |
||||
40 | 56 | return Collection::make($item->getRelations()) |
|||
0 ignored issues
–
show
$item->getRelations() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
41 | 56 | ->reject( |
|||
42 | 56 | static function ($relationship) { |
|||
43 | /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */ |
||||
44 | 40 | return $relationship->shouldOmitIncluded() || ! $relationship->hasIncluded(); |
|||
45 | 56 | } |
|||
46 | 28 | ) |
|||
47 | 56 | ->flatMap( |
|||
48 | 56 | static function ($relationship) { |
|||
49 | /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */ |
||||
50 | 36 | return Collection::wrap($relationship->getIncluded()); |
|||
0 ignored issues
–
show
It seems like
$relationship->getIncluded() can also be of type Swis\JsonApi\Client\Interfaces\ItemInterface ; however, parameter $value of Illuminate\Support\Collection::wrap() does only seem to accept iterable , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
51 | 56 | } |
|||
52 | 28 | ) |
|||
53 | 56 | ->flatMap(fn (ItemInterface $item) => Collection::wrap($item)->merge($this->getIncludedFromItem($item))) |
|||
0 ignored issues
–
show
$item of type Swis\JsonApi\Client\Interfaces\ItemInterface is incompatible with the type iterable expected by parameter $value of Illuminate\Support\Collection::wrap() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
54 | 56 | ->filter(fn (ItemInterface $item) => $this->itemCanBeIncluded($item)); |
|||
55 | } |
||||
56 | |||||
57 | 36 | private function itemCanBeIncluded(ItemInterface $item): bool |
|||
58 | { |
||||
59 | 36 | return $item->hasType() |
|||
60 | 36 | && $item->hasId() |
|||
61 | 36 | && ($item->hasAttributes() || $item->hasRelationships()); |
|||
62 | } |
||||
63 | } |
||||
64 |