|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the League\Fractal package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phil Sturgeon <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace League\Fractal\Serializer; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use League\Fractal\Pagination\PaginatorInterface; |
|
16
|
|
|
use League\Fractal\Resource\ResourceInterface; |
|
17
|
|
|
|
|
18
|
|
|
class JsonApiSerializer extends ArraySerializer |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string|null */ |
|
21
|
|
|
protected $baseUrl; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
protected $rootObjects; |
|
25
|
|
|
|
|
26
|
19 |
|
public function __construct(? string $baseUrl = null) |
|
27
|
|
|
{ |
|
28
|
19 |
|
$this->baseUrl = $baseUrl; |
|
29
|
19 |
|
$this->rootObjects = []; |
|
30
|
19 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Serialize a collection. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $resourceKey |
|
36
|
|
|
* @param array $data |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
13 |
|
public function collection($resourceKey, array $data) : array |
|
41
|
|
|
{ |
|
42
|
13 |
|
$resources = []; |
|
43
|
|
|
|
|
44
|
13 |
|
foreach ($data as $resource) { |
|
45
|
12 |
|
$resources[] = $this->serializeItem($resourceKey, $resource)['data']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
13 |
|
return ['data' => $resources]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Serialize an item. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $resourceKey |
|
55
|
|
|
* @param array $data |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
12 |
|
public function serializeItem($resourceKey, array $data) : array |
|
60
|
|
|
{ |
|
61
|
12 |
|
$id = $this->getIdFromData($data); |
|
62
|
|
|
|
|
63
|
|
|
$resource = [ |
|
64
|
|
|
'data' => [ |
|
65
|
12 |
|
'type' => $resourceKey, |
|
66
|
12 |
|
'id' => "$id", |
|
67
|
12 |
|
'attributes' => $data, |
|
68
|
|
|
], |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
12 |
|
unset($resource['data']['attributes']['id']); |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
12 |
|
if (isset($resource['data']['attributes']['links'])) { |
|
75
|
|
|
$custom_links = $data['links']; |
|
76
|
|
|
unset($resource['data']['attributes']['links']); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
12 |
|
if (isset($resource['data']['attributes']['meta'])) { |
|
80
|
1 |
|
$resource['data']['meta'] = $data['meta']; |
|
81
|
1 |
|
unset($resource['data']['attributes']['meta']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
12 |
|
if (empty($resource['data']['attributes'])) { |
|
85
|
|
|
$resource['data']['attributes'] = (object) []; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
12 |
|
if ($this->shouldIncludeLinks()) { |
|
89
|
5 |
|
$resource['data']['links'] = [ |
|
90
|
5 |
|
'self' => "{$this->baseUrl}/$resourceKey/$id", |
|
91
|
|
|
]; |
|
92
|
5 |
|
if (isset($custom_links)) { |
|
93
|
|
|
$resource['data']['links'] = array_merge($resource['data']['links'], $custom_links); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
12 |
|
return $resource; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Serialize the paginator. |
|
102
|
|
|
* |
|
103
|
|
|
* @param PaginatorInterface $paginator |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function serializePaginator(PaginatorInterface $paginator) : array |
|
108
|
|
|
{ |
|
109
|
|
|
$currentPage = (int)$paginator->getCurrentPage(); |
|
110
|
|
|
$lastPage = (int)$paginator->getLastPage(); |
|
111
|
|
|
|
|
112
|
|
|
$pagination = [ |
|
113
|
|
|
'total' => (int)$paginator->getTotal(), |
|
114
|
|
|
'count' => (int)$paginator->getCount(), |
|
115
|
|
|
'per_page' => (int)$paginator->getPerPage(), |
|
116
|
|
|
'current_page' => $currentPage, |
|
117
|
|
|
'total_pages' => $lastPage, |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
$pagination['links'] = []; |
|
121
|
|
|
|
|
122
|
|
|
$pagination['links']['self'] = $paginator->getUrl($currentPage); |
|
123
|
|
|
$pagination['links']['first'] = $paginator->getUrl(1); |
|
124
|
|
|
|
|
125
|
|
|
if ($currentPage > 1) { |
|
126
|
|
|
$pagination['links']['prev'] = $paginator->getUrl($currentPage - 1); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if ($currentPage < $lastPage) { |
|
130
|
|
|
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$pagination['links']['last'] = $paginator->getUrl($lastPage); |
|
134
|
|
|
|
|
135
|
|
|
return ['pagination' => $pagination]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
19 |
|
public function meta(array $meta) : array |
|
139
|
|
|
{ |
|
140
|
19 |
|
if (empty($meta)) { |
|
141
|
13 |
|
return []; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
6 |
|
$result['meta'] = $meta; |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
6 |
|
if (array_key_exists('pagination', $result['meta'])) { |
|
147
|
3 |
|
$result['links'] = $result['meta']['pagination']['links']; |
|
148
|
3 |
|
unset($result['meta']['pagination']['links']); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
6 |
|
return $result; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
public function null() : array |
|
155
|
|
|
{ |
|
156
|
|
|
return [ |
|
157
|
1 |
|
'data' => null, |
|
158
|
|
|
]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
19 |
|
public function includedData(ResourceInterface $resource, array $data) : array |
|
162
|
|
|
{ |
|
163
|
19 |
|
list($serializedData, $linkedIds) = $this->pullOutNestedIncludedData($data); |
|
164
|
|
|
|
|
165
|
19 |
|
foreach ($data as $value) { |
|
166
|
19 |
|
foreach ($value as $includeObject) { |
|
167
|
7 |
|
if ($this->isNull($includeObject) || $this->isEmpty($includeObject)) { |
|
168
|
3 |
|
continue; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
5 |
|
$includeObjects = $this->createIncludeObjects($includeObject); |
|
172
|
5 |
|
list($serializedData, $linkedIds) = $this->serializeIncludedObjectsWithCacheKey($includeObjects, $linkedIds, $serializedData); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
19 |
|
return empty($serializedData) ? [] : ['included' => $serializedData]; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Indicates if includes should be side-loaded. |
|
181
|
|
|
* |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
19 |
|
public function sideloadIncludes() |
|
185
|
|
|
{ |
|
186
|
19 |
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param array $data |
|
191
|
|
|
* @param array $includedData |
|
192
|
|
|
* |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
19 |
|
public function injectData($data, $includedData) : array |
|
196
|
|
|
{ |
|
197
|
19 |
|
$relationships = $this->parseRelationships($includedData); |
|
198
|
|
|
|
|
199
|
19 |
|
if (!empty($relationships)) { |
|
200
|
7 |
|
$data = $this->fillRelationships($data, $relationships); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
19 |
|
return $data; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Hook to manipulate the final sideloaded includes. |
|
208
|
|
|
* The JSON API specification does not allow the root object to be included |
|
209
|
|
|
* into the sideloaded `included`-array. We have to make sure it is |
|
210
|
|
|
* filtered out, in case some object links to the root object in a |
|
211
|
|
|
* relationship. |
|
212
|
|
|
* |
|
213
|
|
|
* @param array $includedData |
|
214
|
|
|
* @param array $data |
|
215
|
|
|
* |
|
216
|
|
|
* @return array |
|
217
|
|
|
*/ |
|
218
|
19 |
|
public function filterIncludes($includedData, $data) : array |
|
219
|
|
|
{ |
|
220
|
19 |
|
if (!isset($includedData['included'])) { |
|
221
|
16 |
|
return $includedData; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// Create the RootObjects |
|
225
|
3 |
|
$this->createRootObjects($data); |
|
226
|
|
|
|
|
227
|
|
|
// Filter out the root objects |
|
228
|
3 |
|
$filteredIncludes = array_filter($includedData['included'], [$this, 'filterRootObject']); |
|
229
|
|
|
|
|
230
|
|
|
// Reset array indices |
|
231
|
3 |
|
$includedData['included'] = array_merge([], $filteredIncludes); |
|
232
|
|
|
|
|
233
|
3 |
|
return $includedData; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get the mandatory fields for the serializer |
|
238
|
|
|
* |
|
239
|
|
|
* @return array |
|
240
|
|
|
*/ |
|
241
|
2 |
|
public function getMandatoryFields() : array |
|
242
|
|
|
{ |
|
243
|
2 |
|
return ['id']; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Filter function to delete root objects from array. |
|
248
|
|
|
* |
|
249
|
|
|
* @param array $object |
|
250
|
|
|
* |
|
251
|
|
|
* @return bool |
|
252
|
|
|
*/ |
|
253
|
3 |
|
protected function filterRootObject($object) : bool |
|
254
|
|
|
{ |
|
255
|
3 |
|
return !$this->isRootObject($object); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Set the root objects of the JSON API tree. |
|
260
|
|
|
* |
|
261
|
|
|
* @param array $objects |
|
262
|
|
|
*/ |
|
263
|
3 |
|
protected function setRootObjects(array $objects = []) : void |
|
264
|
|
|
{ |
|
265
|
|
|
$this->rootObjects = array_map(function ($object) { |
|
266
|
3 |
|
return "{$object['type']}:{$object['id']}"; |
|
267
|
3 |
|
}, $objects); |
|
268
|
3 |
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Determines whether an object is a root object of the JSON API tree. |
|
272
|
|
|
* |
|
273
|
|
|
* @param array $object |
|
274
|
|
|
* |
|
275
|
|
|
* @return bool |
|
276
|
|
|
*/ |
|
277
|
3 |
|
protected function isRootObject($object) : bool |
|
278
|
|
|
{ |
|
279
|
3 |
|
$objectKey = "{$object['type']}:{$object['id']}"; |
|
280
|
3 |
|
return in_array($objectKey, $this->rootObjects); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @param array|null $data |
|
285
|
|
|
* |
|
286
|
|
|
* @return bool |
|
287
|
|
|
*/ |
|
288
|
11 |
|
protected function isCollection($data) : bool |
|
289
|
|
|
{ |
|
290
|
11 |
|
if ($data === null) { |
|
291
|
|
|
return false; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
11 |
|
return array_key_exists('data', $data) && |
|
295
|
11 |
|
array_key_exists(0, $data['data']); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @param array|null $data |
|
300
|
|
|
* |
|
301
|
|
|
* @return bool |
|
302
|
|
|
*/ |
|
303
|
7 |
|
protected function isNull($data) : bool |
|
304
|
|
|
{ |
|
305
|
7 |
|
if ($data === null) { |
|
306
|
|
|
return true; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
7 |
|
return array_key_exists('data', $data) && $data['data'] === null; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param array|null $data |
|
314
|
|
|
* |
|
315
|
|
|
* @return bool |
|
316
|
|
|
*/ |
|
317
|
6 |
|
protected function isEmpty($data) : bool |
|
318
|
|
|
{ |
|
319
|
6 |
|
if ($data === null) { |
|
320
|
|
|
return true; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
6 |
|
return array_key_exists('data', $data) && $data['data'] === []; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* @param array $data |
|
328
|
|
|
* @param array $relationships |
|
329
|
|
|
* |
|
330
|
|
|
* @return array |
|
331
|
|
|
*/ |
|
332
|
7 |
|
protected function fillRelationships($data, $relationships) : array |
|
333
|
|
|
{ |
|
334
|
7 |
|
if ($this->isCollection($data)) { |
|
335
|
3 |
|
foreach ($relationships as $key => $relationship) { |
|
336
|
3 |
|
$data = $this->fillRelationshipAsCollection($data, $relationship, $key); |
|
337
|
|
|
} |
|
338
|
|
|
} else { // Single resource |
|
339
|
4 |
|
foreach ($relationships as $key => $relationship) { |
|
340
|
4 |
|
$data = $this->fillRelationshipAsSingleResource($data, $relationship, $key); |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
7 |
|
return $data; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param array $includedData |
|
349
|
|
|
* |
|
350
|
|
|
* @return array |
|
351
|
|
|
*/ |
|
352
|
19 |
|
protected function parseRelationships($includedData) : array |
|
353
|
|
|
{ |
|
354
|
19 |
|
$relationships = []; |
|
355
|
|
|
|
|
356
|
19 |
|
foreach ($includedData as $key => $inclusion) { |
|
357
|
19 |
|
foreach ($inclusion as $includeKey => $includeObject) { |
|
358
|
7 |
|
$relationships = $this->buildRelationships($includeKey, $relationships, $includeObject, $key); |
|
359
|
7 |
|
if (isset($includedData[0][$includeKey]['meta'])) { |
|
360
|
|
|
$relationships[$includeKey][0]['meta'] = $includedData[0][$includeKey]['meta']; |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
19 |
|
return $relationships; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* @param array $data |
|
370
|
|
|
* |
|
371
|
|
|
* @return integer |
|
372
|
|
|
*/ |
|
373
|
12 |
|
protected function getIdFromData(array $data) : int |
|
374
|
|
|
{ |
|
375
|
12 |
|
if (!array_key_exists('id', $data)) { |
|
376
|
|
|
throw new InvalidArgumentException( |
|
377
|
|
|
'JSON API resource objects MUST have a valid id' |
|
378
|
|
|
); |
|
379
|
|
|
} |
|
380
|
12 |
|
return $data['id']; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Keep all sideloaded inclusion data on the top level. |
|
385
|
|
|
* |
|
386
|
|
|
* @param array $data |
|
387
|
|
|
* |
|
388
|
|
|
* @return array |
|
389
|
|
|
*/ |
|
390
|
19 |
|
protected function pullOutNestedIncludedData(array $data) : array |
|
391
|
|
|
{ |
|
392
|
19 |
|
$includedData = []; |
|
393
|
19 |
|
$linkedIds = []; |
|
394
|
|
|
|
|
395
|
19 |
|
foreach ($data as $value) { |
|
396
|
19 |
|
foreach ($value as $includeObject) { |
|
397
|
7 |
|
if (isset($includeObject['included'])) { |
|
398
|
|
|
list($includedData, $linkedIds) = $this->serializeIncludedObjectsWithCacheKey($includeObject['included'], $linkedIds, $includedData); |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
19 |
|
return [$includedData, $linkedIds]; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Whether or not the serializer should include `links` for resource objects. |
|
408
|
|
|
* |
|
409
|
|
|
* @return bool |
|
410
|
|
|
*/ |
|
411
|
18 |
|
protected function shouldIncludeLinks() : bool |
|
412
|
|
|
{ |
|
413
|
18 |
|
return $this->baseUrl !== null; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* Check if the objects are part of a collection or not |
|
418
|
|
|
* |
|
419
|
|
|
* @param array $includeObject |
|
420
|
|
|
* |
|
421
|
|
|
* @return array |
|
422
|
|
|
*/ |
|
423
|
5 |
|
private function createIncludeObjects($includeObject) : array |
|
424
|
|
|
{ |
|
425
|
5 |
|
if ($this->isCollection($includeObject)) { |
|
426
|
5 |
|
$includeObjects = $includeObject['data']; |
|
427
|
|
|
|
|
428
|
5 |
|
return $includeObjects; |
|
429
|
|
|
} else { |
|
430
|
|
|
$includeObjects = [$includeObject['data']]; |
|
431
|
|
|
|
|
432
|
|
|
return $includeObjects; |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Sets the RootObjects, either as collection or not. |
|
438
|
|
|
* |
|
439
|
|
|
* @param array $data |
|
440
|
|
|
*/ |
|
441
|
3 |
|
private function createRootObjects(array $data) : void |
|
442
|
|
|
{ |
|
443
|
3 |
|
if ($this->isCollection($data)) { |
|
444
|
3 |
|
$this->setRootObjects($data['data']); |
|
445
|
|
|
} else { |
|
446
|
|
|
$this->setRootObjects([$data['data']]); |
|
447
|
|
|
} |
|
448
|
3 |
|
} |
|
449
|
|
|
|
|
450
|
3 |
|
private function fillRelationshipAsCollection($data, $relationship, $key) : array |
|
451
|
|
|
{ |
|
452
|
3 |
|
foreach ($relationship as $index => $relationshipData) { |
|
453
|
3 |
|
$data['data'][$index]['relationships'][$key] = $relationshipData; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
3 |
|
return $data; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
4 |
|
private function fillRelationshipAsSingleResource($data, $relationship, $key) : array |
|
460
|
|
|
{ |
|
461
|
4 |
|
$data['data']['relationships'][$key] = $relationship[0]; |
|
462
|
|
|
|
|
463
|
4 |
|
return $data; |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
7 |
|
private function buildRelationships(string $includeKey, array $relationships, array $includeObject, string $key) : array |
|
467
|
|
|
{ |
|
468
|
7 |
|
$relationships = $this->addIncludeKeyToRelationsIfNotSet($includeKey, $relationships); |
|
469
|
|
|
|
|
470
|
7 |
|
if ($this->isNull($includeObject)) { |
|
471
|
1 |
|
$relationship = $this->null(); |
|
472
|
6 |
|
} elseif ($this->isEmpty($includeObject)) { |
|
473
|
|
|
$relationship = [ |
|
474
|
2 |
|
'data' => [], |
|
475
|
|
|
]; |
|
476
|
5 |
|
} elseif (! empty($includeObject) && $this->isCollection($includeObject)) { |
|
477
|
5 |
|
$relationship = ['data' => []]; |
|
478
|
5 |
|
$relationship = $this->addIncludedDataToRelationship($includeObject, $relationship); |
|
479
|
|
|
} else { |
|
480
|
|
|
$relationship = [ |
|
481
|
|
|
'data' => [ |
|
482
|
|
|
'type' => $includeObject['data']['type'], |
|
483
|
|
|
'id' => $includeObject['data']['id'], |
|
484
|
|
|
], |
|
485
|
|
|
]; |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
7 |
|
$relationships[$includeKey][$key] = $relationship; |
|
489
|
|
|
|
|
490
|
7 |
|
return $relationships; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
7 |
|
private function addIncludeKeyToRelationsIfNotSet(string $includeKey, array $relationships) : array |
|
494
|
|
|
{ |
|
495
|
7 |
|
if (!array_key_exists($includeKey, $relationships)) { |
|
496
|
7 |
|
$relationships[$includeKey] = []; |
|
497
|
7 |
|
return $relationships; |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
3 |
|
return $relationships; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
* @param array $includeObject |
|
505
|
|
|
* @param array $relationship |
|
506
|
|
|
* |
|
507
|
|
|
* @return array |
|
508
|
|
|
*/ |
|
509
|
5 |
|
private function addIncludedDataToRelationship(array $includeObject, array $relationship) : array |
|
510
|
|
|
{ |
|
511
|
5 |
|
foreach ($includeObject['data'] as $object) { |
|
512
|
5 |
|
$relationship['data'][] = [ |
|
513
|
5 |
|
'type' => $object['type'], |
|
514
|
5 |
|
'id' => $object['id'], |
|
515
|
|
|
]; |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
5 |
|
return $relationship; |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
18 |
|
public function injectAvailableIncludeData($data, $availableIncludes) : array |
|
522
|
|
|
{ |
|
523
|
18 |
|
if (!$this->shouldIncludeLinks()) { |
|
524
|
13 |
|
return $data; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
5 |
|
if ($this->isCollection($data)) { |
|
528
|
|
|
$data['data'] = array_map(function ($resource) use ($availableIncludes) { |
|
529
|
5 |
|
foreach ($availableIncludes as $relationshipKey) { |
|
530
|
5 |
|
$resource = $this->addRelationshipLinks($resource, $relationshipKey); |
|
531
|
|
|
} |
|
532
|
5 |
|
return $resource; |
|
533
|
5 |
|
}, $data['data']); |
|
534
|
|
|
} else { |
|
535
|
|
|
foreach ($availableIncludes as $relationshipKey) { |
|
536
|
|
|
$data['data'] = $this->addRelationshipLinks($data['data'], $relationshipKey); |
|
537
|
|
|
} |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
5 |
|
return $data; |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
5 |
|
private function addRelationshipLinks(array $resource, string $relationshipKey) : array |
|
544
|
|
|
{ |
|
545
|
5 |
|
if (!isset($resource['relationships']) || !isset($resource['relationships'][$relationshipKey])) { |
|
546
|
5 |
|
$resource['relationships'][$relationshipKey] = []; |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
5 |
|
$resource['relationships'][$relationshipKey] = array_merge( |
|
550
|
|
|
[ |
|
551
|
|
|
'links' => [ |
|
552
|
5 |
|
'self' => "{$this->baseUrl}/{$resource['type']}/{$resource['id']}/relationships/{$relationshipKey}", |
|
553
|
5 |
|
'related' => "{$this->baseUrl}/{$resource['type']}/{$resource['id']}/{$relationshipKey}", |
|
554
|
|
|
] |
|
555
|
|
|
], |
|
556
|
5 |
|
$resource['relationships'][$relationshipKey] |
|
557
|
|
|
); |
|
558
|
|
|
|
|
559
|
5 |
|
return $resource; |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
5 |
|
private function serializeIncludedObjectsWithCacheKey(array $includeObjects, array $linkedIds, array $serializedData) : array |
|
563
|
|
|
{ |
|
564
|
5 |
|
foreach ($includeObjects as $object) { |
|
565
|
5 |
|
$includeType = $object['type']; |
|
566
|
5 |
|
$includeId = $object['id']; |
|
567
|
5 |
|
$cacheKey = "$includeType:$includeId"; |
|
568
|
5 |
|
if (!array_key_exists($cacheKey, $linkedIds)) { |
|
569
|
5 |
|
$serializedData[] = $object; |
|
570
|
5 |
|
$linkedIds[$cacheKey] = $object; |
|
571
|
|
|
} |
|
572
|
|
|
} |
|
573
|
5 |
|
return [$serializedData, $linkedIds]; |
|
574
|
|
|
} |
|
575
|
|
|
} |
|
576
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.