1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NilPortugues\Api\Mapping; |
4
|
|
|
|
5
|
|
|
use NilPortugues\Api\Transformer\Helpers\RecursiveFormatterHelper; |
6
|
|
|
|
7
|
|
|
class Mapping |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $className = ''; |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $resourceUrlPattern = ''; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $classAlias = ''; |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $aliasedProperties = []; |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $hiddenProperties = []; |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $idProperties = []; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $relationships = []; |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $metaData = []; |
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $selfUrl = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $otherUrls = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $relationshipSelfUrl = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
private $filterKeys = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $curies = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private $properties = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
private $included = []; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $className |
78
|
|
|
* @param null $resourceUrlPattern |
79
|
|
|
* @param array $idProperties |
80
|
|
|
*/ |
81
|
|
|
public function __construct($className, $resourceUrlPattern = null, array $idProperties = []) |
82
|
|
|
{ |
83
|
|
|
$this->className = (string) $className; |
84
|
|
|
$this->resourceUrlPattern = $resourceUrlPattern; |
85
|
|
|
$this->idProperties = $idProperties; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getClassAlias() |
92
|
|
|
{ |
93
|
|
|
return $this->classAlias; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $aliasedClass |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setClassAlias($aliasedClass) |
102
|
|
|
{ |
103
|
|
|
$this->classAlias = RecursiveFormatterHelper::camelCaseToUnderscore( |
104
|
|
|
RecursiveFormatterHelper::namespaceAsArrayKey($aliasedClass) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getIdProperties() |
114
|
|
|
{ |
115
|
|
|
return (array) $this->idProperties; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $idProperty |
120
|
|
|
*/ |
121
|
|
|
public function addIdProperty($idProperty) |
122
|
|
|
{ |
123
|
|
|
$this->idProperties[] = (string) $idProperty; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $propertyName |
128
|
|
|
*/ |
129
|
|
|
public function hideProperty($propertyName) |
130
|
|
|
{ |
131
|
|
|
$this->hiddenProperties[] = $propertyName; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $propertyName |
136
|
|
|
* @param $propertyAlias |
137
|
|
|
*/ |
138
|
|
|
public function addPropertyAlias($propertyName, $propertyAlias) |
139
|
|
|
{ |
140
|
|
|
$this->aliasedProperties[$propertyName] = $propertyAlias; |
141
|
|
|
|
142
|
|
|
$this->updatePropertyMappings($propertyName, $propertyAlias); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $propertyName |
147
|
|
|
* @param $propertyAlias |
148
|
|
|
*/ |
149
|
|
|
private function updatePropertyMappings($propertyName, $propertyAlias) |
150
|
|
|
{ |
151
|
|
|
if (\in_array($propertyName, $this->idProperties)) { |
152
|
|
|
$position = \array_search($propertyName, $this->idProperties, true); |
153
|
|
|
$this->idProperties[$position] = $propertyAlias; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$search = \sprintf('{%s}', $propertyName); |
157
|
|
|
$replace = \sprintf('{%s}', $propertyAlias); |
158
|
|
|
|
159
|
|
|
$this->selfUrl = \str_replace($search, $replace, $this->selfUrl); |
160
|
|
|
$this->resourceUrlPattern = \str_replace($search, $replace, $this->resourceUrlPattern); |
161
|
|
|
$this->otherUrls = \str_replace($search, $replace, $this->otherUrls); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param array $properties |
166
|
|
|
*/ |
167
|
|
|
public function setPropertyNameAliases(array $properties) |
168
|
|
|
{ |
169
|
|
|
$this->aliasedProperties = \array_merge($this->aliasedProperties, $properties); |
170
|
|
|
|
171
|
|
|
foreach ($this->aliasedProperties as $propertyName => $propertyAlias) { |
172
|
|
|
$this->updatePropertyMappings($propertyName, $propertyAlias); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $properties |
178
|
|
|
*/ |
179
|
|
|
public function setProperties(array $properties) |
180
|
|
|
{ |
181
|
|
|
$this->properties = $properties; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
public function getProperties() |
188
|
|
|
{ |
189
|
|
|
return $this->properties; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
|
public function getClassName() |
196
|
|
|
{ |
197
|
|
|
return $this->className; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
*/ |
202
|
|
|
public function getResourceUrl() |
203
|
|
|
{ |
204
|
|
|
return $this->resourceUrlPattern; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public function getAliasedProperties() |
211
|
|
|
{ |
212
|
|
|
return $this->aliasedProperties; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
public function getHiddenProperties() |
219
|
|
|
{ |
220
|
|
|
return (array) $this->hiddenProperties; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param array $hidden |
225
|
|
|
*/ |
226
|
|
|
public function setHiddenProperties(array $hidden) |
227
|
|
|
{ |
228
|
|
|
$this->hiddenProperties = \array_merge($this->hiddenProperties, $hidden); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
public function getRelationships() |
235
|
|
|
{ |
236
|
|
|
return $this->relationships; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param array $relationships |
241
|
|
|
*/ |
242
|
|
|
public function addAdditionalRelationships(array $relationships) |
243
|
|
|
{ |
244
|
|
|
$this->relationships = $relationships; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function getMetaData() |
251
|
|
|
{ |
252
|
|
|
return $this->metaData; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param array $metaData |
257
|
|
|
*/ |
258
|
|
|
public function setMetaData(array $metaData) |
259
|
|
|
{ |
260
|
|
|
$this->metaData = $metaData; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $key |
265
|
|
|
* @param $value |
266
|
|
|
*/ |
267
|
|
|
public function addMetaData($key, $value) |
268
|
|
|
{ |
269
|
|
|
$this->metaData[$key] = $value; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
|
|
public function getSelfUrl() |
276
|
|
|
{ |
277
|
|
|
return $this->selfUrl; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param string $self |
282
|
|
|
* |
283
|
|
|
* @throws \InvalidArgumentException |
284
|
|
|
*/ |
285
|
|
|
public function setSelfUrl($self) |
286
|
|
|
{ |
287
|
|
|
$this->selfUrl = (string) $self; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param $propertyName |
292
|
|
|
* |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public function getRelatedUrl($propertyName) |
296
|
|
|
{ |
297
|
|
|
return (!empty($this->relationshipSelfUrl[$propertyName]['related'])) |
298
|
|
|
? $this->relationshipSelfUrl[$propertyName]['related'] |
299
|
|
|
: ''; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param array $filterKeys |
304
|
|
|
*/ |
305
|
|
|
public function setFilterKeys(array $filterKeys) |
306
|
|
|
{ |
307
|
|
|
$this->filterKeys = $filterKeys; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
public function getFilterKeys() |
314
|
|
|
{ |
315
|
|
|
return (array) $this->filterKeys; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param string $propertyName |
320
|
|
|
* @param string $urls |
321
|
|
|
* |
322
|
|
|
* @return $this |
323
|
|
|
*/ |
324
|
|
|
public function setRelationshipUrls($propertyName, $urls) |
325
|
|
|
{ |
326
|
|
|
$this->relationshipSelfUrl[$propertyName] = $urls; |
327
|
|
|
|
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param $propertyName |
333
|
|
|
* |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
public function getRelationshipSelfUrl($propertyName) |
337
|
|
|
{ |
338
|
|
|
return (!empty($this->relationshipSelfUrl[$propertyName]['self'])) |
339
|
|
|
? $this->relationshipSelfUrl[$propertyName]['self'] |
340
|
|
|
: ''; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param array $urls |
345
|
|
|
*/ |
346
|
|
|
public function setUrls(array $urls) |
347
|
|
|
{ |
348
|
|
|
$this->otherUrls = $urls; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @return array |
353
|
|
|
*/ |
354
|
|
|
public function getUrls() |
355
|
|
|
{ |
356
|
|
|
return $this->otherUrls; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param array $curies |
361
|
|
|
* |
362
|
|
|
* @throws MappingException |
363
|
|
|
*/ |
364
|
|
|
public function setCuries(array $curies) |
365
|
|
|
{ |
366
|
|
|
if (empty($curies['name']) || empty($curies['href'])) { |
367
|
|
|
throw new MappingException('Curies must define "name" and "href" properties'); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$this->curies = $curies; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @return array |
375
|
|
|
*/ |
376
|
|
|
public function getCuries() |
377
|
|
|
{ |
378
|
|
|
return $this->curies; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Used by JSON API included resource filtering. |
383
|
|
|
* |
384
|
|
|
* @param $resource |
385
|
|
|
*/ |
386
|
|
|
public function addIncludedResource($resource) |
387
|
|
|
{ |
388
|
|
|
$this->included[] = $resource; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Returns the allowed included resources. |
393
|
|
|
* |
394
|
|
|
* @return array |
395
|
|
|
*/ |
396
|
|
|
public function getIncludedResources() |
397
|
|
|
{ |
398
|
|
|
return $this->included; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Returns true if included resource filtering has been set, false otherwise. |
403
|
|
|
* |
404
|
|
|
* @return bool |
405
|
|
|
*/ |
406
|
|
|
public function isFilteringIncludedResources() |
407
|
|
|
{ |
408
|
|
|
return (empty($this->included)) ? false : true; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|