1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk; |
4
|
|
|
|
5
|
|
|
use Mapado\RestClientSdk\Exception\MappingException; |
6
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Mapping |
10
|
|
|
* |
11
|
|
|
* @author Julien Deniau <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Mapping |
14
|
|
|
{ |
15
|
|
|
public const DEFAULT_CONFIG = [ |
16
|
|
|
'collectionKey' => 'hydra:member', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $idPrefix; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $idPrefixLength; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ClassMetadata[] |
31
|
|
|
*/ |
32
|
|
|
private $classMetadataList = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* config |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $idPrefix |
45
|
|
|
*/ |
46
|
|
|
public function __construct($idPrefix = '', $config = []) |
47
|
|
|
{ |
48
|
1 |
|
$this->idPrefix = $idPrefix; |
49
|
1 |
|
$this->idPrefixLength = strlen($idPrefix); |
50
|
1 |
|
$this->setConfig($config); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* getIdPrefix |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getIdPrefix() |
59
|
|
|
{ |
60
|
|
|
return $this->idPrefix; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Getter for config |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getConfig() |
69
|
|
|
{ |
70
|
1 |
|
return $this->config; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Setter for config |
75
|
|
|
* |
76
|
|
|
* @param array $config |
77
|
|
|
* |
78
|
|
|
* @return Mapping |
79
|
|
|
*/ |
80
|
|
|
public function setConfig(array $config) |
81
|
|
|
{ |
82
|
1 |
|
$this->config = array_merge( |
83
|
1 |
|
self::DEFAULT_CONFIG, |
84
|
1 |
|
$config |
85
|
|
|
); |
86
|
|
|
|
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* setMapping |
92
|
|
|
* |
93
|
|
|
* @param ClassMetadata[] $classMetadataList |
94
|
|
|
* |
95
|
|
|
* @return Mapping |
96
|
|
|
*/ |
97
|
|
|
public function setMapping(array $classMetadataList) |
98
|
|
|
{ |
99
|
1 |
|
$this->classMetadataList = $classMetadataList; |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* return a model class name for a given key |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getModelName($key) |
112
|
|
|
{ |
113
|
1 |
|
$this->checkMappingExistence($key, 'modelName'); |
114
|
|
|
|
115
|
1 |
|
return $this->getClassMetadataByKey($key)->getModelName(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* return the list of mapping keys |
120
|
|
|
* |
121
|
|
|
* @return string[] |
122
|
|
|
*/ |
123
|
|
|
public function getMappingKeys() |
124
|
|
|
{ |
125
|
1 |
|
return array_map( |
126
|
1 |
|
function (ClassMetadata $classMetadata) { |
127
|
1 |
|
return $classMetadata->getKey(); |
128
|
1 |
|
}, |
129
|
1 |
|
$this->classMetadataList |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* get the key from an id (path) |
135
|
|
|
* |
136
|
|
|
* @param string $id |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getKeyFromId($id) |
141
|
|
|
{ |
142
|
1 |
|
$key = $this->parseKeyFromId($id); |
143
|
1 |
|
$this->checkMappingExistence($key); |
144
|
|
|
|
145
|
1 |
|
return $key; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* getKeyFromModel |
150
|
|
|
* |
151
|
|
|
* @param string $modelName model name |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
* |
155
|
|
|
* @throws MappingException |
156
|
|
|
*/ |
157
|
|
|
public function getKeyFromModel($modelName) |
158
|
|
|
{ |
159
|
1 |
|
foreach ($this->classMetadataList as $classMetadata) { |
160
|
1 |
|
if ($modelName === $classMetadata->getModelName()) { |
161
|
1 |
|
return $classMetadata->getKey(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
throw new MappingException('Model name ' . $modelName . ' not found in mapping'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* getClassMetadata for model name |
170
|
|
|
* |
171
|
|
|
* @param string $modelName |
172
|
|
|
* |
173
|
|
|
* @return ClassMetadata |
174
|
|
|
* |
175
|
|
|
* @throws MappingException |
176
|
|
|
*/ |
177
|
|
|
public function getClassMetadata($modelName) |
178
|
|
|
{ |
179
|
1 |
|
foreach ($this->classMetadataList as $classMetadata) { |
180
|
1 |
|
if ($modelName === $classMetadata->getModelName()) { |
181
|
1 |
|
return $classMetadata; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
throw new MappingException($modelName . ' model is not mapped'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* getClassMetadata for id |
190
|
|
|
* |
191
|
|
|
* @param string $id |
192
|
|
|
* |
193
|
|
|
* @return ClassMetadata|null |
194
|
|
|
*/ |
195
|
|
|
public function tryGetClassMetadataById($id) |
196
|
|
|
{ |
197
|
1 |
|
$key = $this->parseKeyFromId($id); |
198
|
|
|
|
199
|
1 |
|
foreach ($this->classMetadataList as $classMetadata) { |
200
|
1 |
|
if ($key === $classMetadata->getKey()) { |
201
|
1 |
|
return $classMetadata; |
202
|
|
|
} |
203
|
|
|
} |
204
|
1 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* hasClassMetadata |
208
|
|
|
* |
209
|
|
|
* @param string $modelName |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function hasClassMetadata($modelName) |
214
|
|
|
{ |
215
|
1 |
|
foreach ($this->classMetadataList as $classMetadata) { |
216
|
1 |
|
if ($modelName === $classMetadata->getModelName()) { |
217
|
1 |
|
return true; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
1 |
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* getMappingByKey |
226
|
|
|
* |
227
|
|
|
* @param string $key |
228
|
|
|
* |
229
|
|
|
* @return ClassMetadata|null |
230
|
|
|
*/ |
231
|
|
|
public function getClassMetadataByKey($key) |
232
|
|
|
{ |
233
|
1 |
|
foreach ($this->classMetadataList as $classMetadata) { |
234
|
1 |
|
if ($key === $classMetadata->getKey()) { |
235
|
1 |
|
return $classMetadata; |
236
|
|
|
} |
237
|
|
|
} |
238
|
1 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Parse the key from an id (path) |
242
|
|
|
* |
243
|
|
|
* @param string $id |
244
|
|
|
* |
245
|
|
|
* @return string|null |
246
|
|
|
*/ |
247
|
|
|
private function parseKeyFromId($id) |
248
|
|
|
{ |
249
|
1 |
|
$id = $this->removePrefix($id); |
250
|
|
|
|
251
|
1 |
|
$matches = []; |
252
|
1 |
|
if (1 === preg_match('|/([^/]+)/[^/]+$|', $id, $matches)) { |
253
|
1 |
|
return $matches[1]; |
254
|
|
|
} |
255
|
1 |
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* checkMappingExistence |
259
|
|
|
* |
260
|
|
|
* @param string $key |
261
|
|
|
* @param string|null $subKey |
262
|
|
|
*/ |
263
|
|
|
private function checkMappingExistence($key, $subKey = null) |
264
|
|
|
{ |
265
|
1 |
|
if (empty($key)) { |
266
|
1 |
|
throw new MappingException('key is not set'); |
267
|
|
|
} |
268
|
|
|
|
269
|
1 |
|
$metadata = $this->getClassMetadataByKey($key); |
270
|
1 |
|
if (!$metadata) { |
271
|
1 |
|
throw new MappingException($key . ' key is not mapped'); |
272
|
|
|
} |
273
|
|
|
|
274
|
1 |
|
if (!empty($subKey)) { |
275
|
1 |
|
$methodName = 'get' . ucfirst($subKey); |
276
|
1 |
|
if (!$metadata->$methodName()) { |
277
|
1 |
|
throw new MappingException($key . ' key is mapped but no ' . $subKey . ' found'); |
278
|
|
|
} |
279
|
|
|
} |
280
|
1 |
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* removePrefix |
284
|
|
|
* |
285
|
|
|
* @param mixed $value |
286
|
|
|
* |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
private function removePrefix($value) |
290
|
|
|
{ |
291
|
1 |
|
if (($this->idPrefixLength > 0) && (0 === strpos($value, $this->idPrefix))) { |
292
|
1 |
|
return substr($value, $this->idPrefixLength); |
293
|
|
|
} |
294
|
|
|
|
295
|
1 |
|
return $value; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|