Completed
Pull Request — master (#18)
by Jasper
12:44
created

Hydrator::getCollection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Swis\JsonApi\Client\JsonApi;
4
5
use Art4\JsonApiClient\ElementInterface;
6
use Art4\JsonApiClient\ResourceCollectionInterface;
7
use Art4\JsonApiClient\ResourceIdentifierCollectionInterface;
8
use Art4\JsonApiClient\ResourceIdentifierInterface;
9
use Art4\JsonApiClient\ResourceItemInterface;
10
use Swis\JsonApi\Client\Collection;
11
use Swis\JsonApi\Client\Interfaces\ItemInterface;
12
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
13
use Swis\JsonApi\Client\Items\JenssegersItem;
14
use Swis\JsonApi\Client\Items\NullItem;
15
16
class Hydrator
17
{
18
    /**
19
     * @var \Swis\JsonApi\Client\Interfaces\TypeMapperInterface
20
     */
21
    protected $typeMapper;
22
23
    /**
24
     * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface $typeMapper
25
     */
26
    public function __construct(TypeMapperInterface $typeMapper)
27
    {
28 24
        $this->typeMapper = $typeMapper;
29
    }
30 24
31 24
    /**
32
     * @param \Art4\JsonApiClient\ResourceItemInterface $jsonApiItem
33
     *
34
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface
35
     */
36
    public function hydrateItem(ResourceItemInterface $jsonApiItem): ItemInterface
37
    {
38 3
        $item = $this->getItemClass($jsonApiItem->get('type'));
39
40 3
        $item->setId($jsonApiItem->get('id'));
41 3
42 3
        if ($jsonApiItem->has('attributes')) {
43
            $item->fill($jsonApiItem->get('attributes')->asArray(true));
44
        }
45 3
46
        return $item;
47
    }
48
49
    /**
50
     * @param \Art4\JsonApiClient\ResourceCollectionInterface $jsonApiCollection
51
     *
52
     * @return \Swis\JsonApi\Client\Collection
53 24
     */
54
    public function hydrateCollection(ResourceCollectionInterface $jsonApiCollection): Collection
55 24
    {
56
        $collection = new Collection();
57 24
        foreach ($jsonApiCollection->asArray() as $item) {
58 24
            $collection->push($this->hydrateItem($item));
59
        }
60 24
61
        return $collection;
62 24
    }
63
64
    /**
65
     * @param \Swis\JsonApi\Client\Collection $jsonApiItems
66
     * @param \Swis\JsonApi\Client\Collection $items
67
     */
68
    public function hydrateRelationships(Collection $jsonApiItems, Collection $items)
69
    {
70 24
        $keyedItems = $items->keyBy(
71
            function (ItemInterface $item) {
72 24
                return $this->getItemKey($item);
73 24
            }
74 18
        );
75
76
        $jsonApiItems->each(
77 6
            function (ResourceItemInterface $jsonApiItem) use ($keyedItems) {
78
                if (!$jsonApiItem->has('relationships')) {
79
                    return;
80
                }
81
82
                $item = $this->getItem($keyedItems, $jsonApiItem);
83
84 24
                if ($item instanceof NullItem) {
85
                    return;
86 24
                }
87 24
88
                foreach ($jsonApiItem->get('relationships')->asArray() as $name => $relationship) {
89 24
                    /** @var \Art4\JsonApiClient\ElementInterface $data */
90
                    $data = $relationship->get('data');
91
                    $method = camel_case($name);
92
93
                    if ($data instanceof ResourceIdentifierInterface) {
94
                        $includedItem = $this->getItem($keyedItems, $data);
95 15
96
                        if ($includedItem instanceof NullItem) {
97 15
                            continue;
98 15
                        }
99 15
100 12
                        $item->setRelation($method, $includedItem);
101
                    } elseif ($data instanceof ResourceIdentifierCollectionInterface) {
102
                        $collection = $this->getCollection($keyedItems, $data);
103 15
104
                        $item->setRelation($method, $collection);
105 15
                    }
106
                }
107
            }
108
        );
109 15
    }
110
111 15
    /**
112
     * @param string $type
113 15
     *
114 15
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface
115
     */
116 15
    protected function getItemClass(string $type): ItemInterface
117 15
    {
118
        if ($this->typeMapper->hasMapping($type)) {
119 15
            return $this->typeMapper->getMapping($type);
120 15
        }
121
122
        return (new JenssegersItem())->setType($type);
123 9
    }
124 15
125 15
    /**
126
     * @param \Swis\JsonApi\Client\Collection                                                           $included
127 15
     * @param \Art4\JsonApiClient\ResourceIdentifierInterface|\Art4\JsonApiClient\ResourceItemInterface $identifier
128
     *
129
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface
130 15
     */
131
    protected function getItem(Collection $included, $identifier): ItemInterface
132 15
    {
133
        return $included->get(
134
            $this->getElementKey($identifier),
135
            function () {
136
                return new NullItem();
137
            }
138
        );
139 15
    }
140
141 15
    /**
142
     * @param \Swis\JsonApi\Client\Collection                                                                           $included
143
     * @param \Art4\JsonApiClient\ResourceIdentifierCollectionInterface|\Art4\JsonApiClient\ResourceCollectionInterface $identifierCollection
144
     *
145
     * @return \Swis\JsonApi\Client\Collection
146
     */
147
    protected function getCollection(Collection $included, $identifierCollection): Collection
148
    {
149
        $items = new Collection();
150 15
151
        foreach ($identifierCollection->asArray() as $identifier) {
152 15
            $item = $this->getItem($included, $identifier);
153 15
154 15
            if ($item instanceof NullItem) {
155 15
                continue;
156 15
            }
157
158
            $items->push($item);
159
        }
160
161
        return $items;
162
    }
163
164
    /**
165
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
166 15
     *
167
     * @return string
168 15
     */
169 15
    protected function getItemKey(ItemInterface $item): string
170
    {
171
        return sprintf('%s:%s', $item->getType(), $item->getId());
172
    }
173
174
    /**
175
     * @param \Art4\JsonApiClient\ElementInterface $accessor
176
     *
177
     * @return string
178 15
     */
179
    protected function getElementKey(ElementInterface $accessor): string
180 15
    {
181 15
        return sprintf('%s:%s', $accessor->get('type'), $accessor->get('id'));
182 15
    }
183
}
184