Passed
Push — master ( 685595...680e76 )
by Julien
01:32
created

ModelHydrator::hydrateList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Model;
4
5
use Mapado\RestClientSdk\Collection\Collection;
6
use Mapado\RestClientSdk\Collection\HalCollection;
7
use Mapado\RestClientSdk\Collection\HydraPaginatedCollection;
8
use Mapado\RestClientSdk\Helper\ArrayHelper;
9
use Mapado\RestClientSdk\SdkClient;
10
11
/**
12
 * Class ModelHydrator
13
 *
14
 * @author Julien Deniau <[email protected]>
15
 */
16
class ModelHydrator
17
{
18
    /**
19
     * sdk
20
     *
21
     * @var SdkClient
22
     */
23
    protected $sdk;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param SdkClient $sdk
29
     */
30
    public function __construct(SdkClient $sdk)
31
    {
32 1
        $this->sdk = $sdk;
33 1
    }
34
35
    /**
36
     * convertId
37
     *
38
     * @param mixed $id
39
     * @param string $modelName
40
     *
41
     * @return string
42
     */
43
    public function convertId($id, $modelName)
44
    {
45 1
        $id = (string) $id;
46
47
        // add slash if needed to have a valid hydra id
48 1
        if (false === strpos($id, '/')) {
49 1
            $mapping = $this->sdk->getMapping();
50 1
            $key = $mapping->getKeyFromModel($modelName);
51 1
            $id = '/' . $key . '/' . $id;
52
53 1
            if ($prefix = $mapping->getIdPrefix()) {
54 1
                $id = $prefix . $id;
55
            }
56
        }
57
58 1
        return $id;
59
    }
60
61
    /**
62
     * convert data as array to entity
63
     *
64
     * @param array $data
65
     * @param string $modelName
66
     *
67
     * @return object
68
     */
69
    public function hydrate($data, $modelName)
70
    {
71 1
        $mapping = $this->sdk->getMapping();
72 1
        $key = $mapping->getKeyFromModel($modelName);
73 1
        $modelName = $mapping->getModelName($key);
74
75 1
        return $this->deserialize($data, $modelName);
76
    }
77
78
    /**
79
     * convert API response to Collection containing entities
80
     *
81
     * @param array $data
82
     * @param string $modelName
83
     *
84
     * @return Collection
85
     */
86
    public function hydrateList($data, $modelName)
87
    {
88 1
        $collectionKey = $this->sdk->getMapping()->getConfig()['collectionKey'];
89
90 1
        if (is_array($data) && ArrayHelper::arrayHas($data, $collectionKey)) {
91 1
            return $this->deserializeAll($data, $modelName);
92
        }
93
94
        return new Collection();
95
    }
96
97
    /**
98
     * convert list of data as array to Collection containing entities
99
     *
100
     * @param array  $data
101
     * @param string $modelName
102
     *
103
     * @return Collection
104
     */
105
    private function deserializeAll($data, $modelName)
106
    {
107 1
        $collectionKey = $this->sdk->getMapping()->getConfig()['collectionKey'];
108
109 1
        $itemList = array_map(function ($member) use ($modelName) {
110 1
            return $this->deserialize($member, $modelName);
111 1
        }, ArrayHelper::arrayGet($data, $collectionKey));
112
113 1
        $extraProperties = array_filter(
114 1
            $data,
115 1
            function ($key) use ($collectionKey) {
116 1
                return $key !== $collectionKey;
117 1
            },
118 1
            ARRAY_FILTER_USE_KEY
119
        );
120
121 1
        $collectionClassName = $this->guessCollectionClassname($data);
122
123 1
        return new $collectionClassName($itemList, $extraProperties);
124
    }
125
126
    /**
127
     * convert array to entity
128
     *
129
     * @param array  $data
130
     * @param string $modelName
131
     *
132
     * @return object|null
133
     */
134
    private function deserialize($data, $modelName)
135
    {
136 1
        if (empty($data)) {
137
            return null;
138
        }
139
140 1
        if (!is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
141
            return null;
142
        }
143
144 1
        return $this->sdk->getSerializer()->deserialize($data, $modelName);
145
    }
146
147
    /**
148
     * guess collection classname according to response data
149
     *
150
     * @param array $data
151
     *
152
     * @return string
153
     */
154
    private function guessCollectionClassname($data)
155
    {
156
        switch (true) {
157 1
            case !empty($data['@type']) &&
158 1
            'hydra:PagedCollection' === $data['@type']:
159 1
                return HydraPaginatedCollection::class;
160
161 1
            case array_key_exists('_embedded', $data):
162 1
                return HalCollection::class;
163
164
            default:
165
                return Collection::class;
166
        }
167
    }
168
}
169