|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Mapado\RestClientSdk\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Mapado\RestClientSdk\Collection\Collection; |
|
8
|
|
|
use Mapado\RestClientSdk\Collection\HalCollection; |
|
9
|
|
|
use Mapado\RestClientSdk\Collection\HydraPaginatedCollection; |
|
10
|
|
|
use Mapado\RestClientSdk\Helper\ArrayHelper; |
|
11
|
|
|
use Mapado\RestClientSdk\SdkClient; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ModelHydrator |
|
15
|
|
|
* |
|
16
|
|
|
* @author Julien Deniau <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ModelHydrator |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var SdkClient |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $sdk; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(SdkClient $sdk) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->sdk = $sdk; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string|int|mixed $id |
|
32
|
|
|
*/ |
|
33
|
|
|
public function convertId($id, string $modelName): string |
|
34
|
|
|
{ |
|
35
|
1 |
|
$id = (string) $id; |
|
36
|
|
|
|
|
37
|
|
|
// add slash if needed to have a valid hydra id |
|
38
|
1 |
|
if (false === mb_strpos($id, '/')) { |
|
39
|
1 |
|
$mapping = $this->sdk->getMapping(); |
|
40
|
1 |
|
$key = $mapping->getKeyFromModel($modelName); |
|
41
|
1 |
|
$id = '/' . $key . '/' . $id; |
|
42
|
|
|
|
|
43
|
1 |
|
if ($prefix = $mapping->getIdPrefix()) { |
|
44
|
1 |
|
$id = $prefix . $id; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $id; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* convert data as array to entity |
|
53
|
|
|
*/ |
|
54
|
|
|
public function hydrate(?array $data, string $modelName): ?object |
|
55
|
|
|
{ |
|
56
|
1 |
|
$mapping = $this->sdk->getMapping(); |
|
57
|
1 |
|
$key = $mapping->getKeyFromModel($modelName); |
|
58
|
1 |
|
$modelName = $mapping->getModelName($key); |
|
59
|
|
|
|
|
60
|
1 |
|
return $this->deserialize($data, $modelName); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* convert API response to Collection containing entities |
|
65
|
|
|
*/ |
|
66
|
|
|
public function hydrateList(?array $data, string $modelName): Collection |
|
67
|
|
|
{ |
|
68
|
1 |
|
$collectionKey = $this->sdk->getMapping()->getConfig()['collectionKey']; |
|
69
|
|
|
|
|
70
|
1 |
|
if (is_array($data) && ArrayHelper::arrayHas($data, $collectionKey)) { |
|
71
|
1 |
|
return $this->deserializeAll($data, $modelName); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return new Collection(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* convert list of data as array to Collection containing entities |
|
79
|
|
|
*/ |
|
80
|
|
|
private function deserializeAll(array $data, string $modelName): Collection |
|
81
|
|
|
{ |
|
82
|
1 |
|
$collectionKey = $this->sdk->getMapping()->getConfig()['collectionKey']; |
|
83
|
|
|
|
|
84
|
1 |
|
$itemList = array_map(function ($member) use ($modelName) { |
|
85
|
1 |
|
return $this->deserialize($member, $modelName); |
|
86
|
1 |
|
}, ArrayHelper::arrayGet($data, $collectionKey)); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
1 |
|
$extraProperties = array_filter( |
|
89
|
1 |
|
$data, |
|
90
|
1 |
|
function ($key) use ($collectionKey) { |
|
91
|
1 |
|
return $key !== $collectionKey; |
|
92
|
1 |
|
}, |
|
93
|
1 |
|
ARRAY_FILTER_USE_KEY |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
1 |
|
$collectionClassName = $this->guessCollectionClassname($data); |
|
97
|
|
|
|
|
98
|
1 |
|
return new $collectionClassName($itemList, $extraProperties); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* convert array to entity |
|
103
|
|
|
*/ |
|
104
|
|
|
private function deserialize(?array $data, string $modelName): ?object |
|
105
|
|
|
{ |
|
106
|
1 |
|
if (null === $data) { |
|
|
|
|
|
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
return $this->sdk->getSerializer()->deserialize($data, $modelName); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* guess collection classname according to response data |
|
115
|
|
|
*/ |
|
116
|
|
|
private function guessCollectionClassname(array $data): string |
|
117
|
|
|
{ |
|
118
|
|
|
switch (true) { |
|
119
|
1 |
|
case !empty($data['@type']) && |
|
120
|
1 |
|
'hydra:PagedCollection' === $data['@type']: |
|
121
|
1 |
|
return HydraPaginatedCollection::class; |
|
122
|
|
|
|
|
123
|
1 |
|
case array_key_exists('_embedded', $data): |
|
124
|
1 |
|
return HalCollection::class; |
|
125
|
|
|
|
|
126
|
|
|
default: |
|
127
|
|
|
return Collection::class; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|