1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of JSON-API. |
5
|
|
|
* |
6
|
|
|
* (c) Toby Zerner <[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 Tobscure\JsonApi; |
13
|
|
|
|
14
|
|
|
use JsonSerializable; |
15
|
|
|
|
16
|
|
|
class Document implements JsonSerializable |
17
|
|
|
{ |
18
|
|
|
use LinksTrait; |
19
|
|
|
use MetaTrait; |
20
|
|
|
|
21
|
|
|
const MEDIA_TYPE = 'application/vnd.api+json'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The data object. |
25
|
|
|
* |
26
|
|
|
* @var ResourceInterface|ResourceInterface[]|null |
27
|
|
|
*/ |
28
|
|
|
protected $data; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The errors array. |
32
|
|
|
* |
33
|
|
|
* @var array|null |
34
|
|
|
*/ |
35
|
|
|
protected $errors; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The jsonapi array. |
39
|
|
|
* |
40
|
|
|
* @var array|null |
41
|
|
|
*/ |
42
|
|
|
protected $jsonapi; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Relationships to include. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $include = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sparse fieldsets. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $fields = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ResourceInterface|ResourceInterface[] $data |
|
|
|
|
60
|
|
|
*/ |
61
|
30 |
|
public function __construct($data = null) |
62
|
|
|
{ |
63
|
30 |
|
$this->data = $data; |
64
|
30 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the data object. |
68
|
|
|
* |
69
|
|
|
* @return ResourceInterface|ResourceInterface[]|null $data |
70
|
|
|
*/ |
71
|
|
|
public function getData() |
72
|
|
|
{ |
73
|
|
|
return $this->data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the data object. |
78
|
|
|
* |
79
|
|
|
* @param ResourceInterface|ResourceInterface[]|null $data |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setData($data) |
84
|
|
|
{ |
85
|
|
|
$this->data = $data; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the errors array. |
92
|
|
|
* |
93
|
|
|
* @return array|null $errors |
94
|
|
|
*/ |
95
|
|
|
public function getErrors() |
96
|
|
|
{ |
97
|
|
|
return $this->errors; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set the errors array. |
102
|
|
|
* |
103
|
|
|
* @param array|null $errors |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
3 |
|
public function setErrors(array $errors = null) |
108
|
|
|
{ |
109
|
3 |
|
$this->errors = $errors; |
110
|
|
|
|
111
|
3 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the jsonapi array. |
116
|
|
|
* |
117
|
|
|
* @return array|null $jsonapi |
118
|
|
|
*/ |
119
|
|
|
public function getJsonapi() |
120
|
|
|
{ |
121
|
|
|
return $this->jsonapi; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the jsonapi array. |
126
|
|
|
* |
127
|
|
|
* @param array|null $jsonapi |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
3 |
|
public function setJsonapi(array $jsonapi = null) |
132
|
|
|
{ |
133
|
3 |
|
$this->jsonapi = $jsonapi; |
134
|
|
|
|
135
|
3 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the relationships to include. |
140
|
|
|
* |
141
|
|
|
* @return array $include |
142
|
|
|
*/ |
143
|
|
|
public function getInclude() |
144
|
|
|
{ |
145
|
|
|
return $this->include; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the relationships to include. |
150
|
|
|
* |
151
|
|
|
* @param array $include |
152
|
|
|
* |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
3 |
|
public function setInclude(array $include) |
156
|
|
|
{ |
157
|
3 |
|
$this->include = $include; |
158
|
|
|
|
159
|
3 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the sparse fieldsets. |
164
|
|
|
* |
165
|
|
|
* @return array $fields |
166
|
|
|
*/ |
167
|
|
|
public function getFields() |
168
|
|
|
{ |
169
|
|
|
return $this->fields; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the sparse fieldsets. |
174
|
|
|
* |
175
|
|
|
* @param array $fields |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
3 |
|
public function setFields(array $fields) |
180
|
|
|
{ |
181
|
3 |
|
$this->fields = $fields; |
182
|
|
|
|
183
|
3 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Build the JSON-API document as an array. |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
30 |
|
public function toArray() |
192
|
|
|
{ |
193
|
30 |
|
$document = []; |
194
|
|
|
|
195
|
30 |
|
if ($this->links) { |
|
|
|
|
196
|
3 |
|
$document['links'] = $this->links; |
197
|
3 |
|
} |
198
|
|
|
|
199
|
30 |
|
if ($this->data) { |
200
|
15 |
|
$isCollection = is_array($this->data); |
201
|
|
|
|
202
|
|
|
// Build a multi-dimensional map of all of the distinct resources |
203
|
|
|
// that are present in the document, indexed by type and ID. This is |
204
|
|
|
// done by recursively looping through each of the resources and |
205
|
|
|
// their included relationships. We do this so that any resources |
206
|
|
|
// that are duplicated may be merged back into a single instance. |
207
|
15 |
|
$map = []; |
208
|
15 |
|
$resources = $isCollection ? $this->data : [$this->data]; |
209
|
|
|
|
210
|
15 |
|
$this->addResourcesToMap($map, $resources, $this->include); |
211
|
|
|
|
212
|
|
|
// Now extract the document's primary resource(s) from the resource |
213
|
|
|
// map, and flatten the map's remaining resources to be included in |
214
|
|
|
// the document's "included" array. |
215
|
15 |
|
foreach ($resources as $resource) { |
216
|
15 |
|
$type = $resource->getType(); |
217
|
15 |
|
$id = $resource->getId(); |
218
|
|
|
|
219
|
15 |
|
if (isset($map[$type][$id])) { |
220
|
15 |
|
$primary[] = $map[$type][$id]; |
|
|
|
|
221
|
15 |
|
unset($map[$type][$id]); |
222
|
15 |
|
} |
223
|
15 |
|
} |
224
|
|
|
|
225
|
15 |
|
$included = call_user_func_array('array_merge', $map); |
226
|
|
|
|
227
|
15 |
|
$document['data'] = $isCollection ? $primary : $primary[0]; |
|
|
|
|
228
|
|
|
|
229
|
15 |
|
if ($included) { |
230
|
3 |
|
$document['included'] = $included; |
231
|
3 |
|
} |
232
|
15 |
|
} |
233
|
|
|
|
234
|
30 |
|
if ($this->meta) { |
|
|
|
|
235
|
3 |
|
$document['meta'] = $this->meta; |
236
|
3 |
|
} |
237
|
|
|
|
238
|
30 |
|
if ($this->errors) { |
239
|
3 |
|
$document['errors'] = $this->errors; |
240
|
3 |
|
} |
241
|
|
|
|
242
|
30 |
|
if ($this->jsonapi) { |
243
|
3 |
|
$document['jsonapi'] = $this->jsonapi; |
244
|
3 |
|
} |
245
|
|
|
|
246
|
30 |
|
return $document; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Build the JSON-API document and encode it as a JSON string. |
251
|
|
|
* |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
3 |
|
public function __toString() |
255
|
|
|
{ |
256
|
3 |
|
return json_encode($this->toArray()); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Serialize for JSON usage. |
261
|
|
|
* |
262
|
|
|
* @return array |
263
|
|
|
*/ |
264
|
|
|
public function jsonSerialize() |
265
|
|
|
{ |
266
|
|
|
return $this->toArray(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Recursively add the given resources and their relationships to a map. |
271
|
|
|
* |
272
|
|
|
* @param array &$map The map to merge resources into. |
273
|
|
|
* @param ResourceInterface[] $resources |
274
|
|
|
* @param array $include An array of relationship paths to include. |
275
|
|
|
*/ |
276
|
15 |
|
private function addResourcesToMap(array &$map, array $resources, array $include) |
277
|
|
|
{ |
278
|
|
|
// Index relationship paths so that we have a list of the direct |
279
|
|
|
// relationships that will be included on these resources, and arrays |
280
|
|
|
// of their respective nested relationships. |
281
|
15 |
|
$include = $this->indexRelationshipPaths($include); |
282
|
|
|
|
283
|
15 |
|
foreach ($resources as $resource) { |
284
|
15 |
|
$relationships = []; |
285
|
|
|
|
286
|
|
|
// Get each of the relationships we're including on this resource, |
287
|
|
|
// and add their resources (and their relationships, and so on) to |
288
|
|
|
// the map. |
289
|
15 |
|
foreach ($include as $name => $nested) { |
290
|
3 |
|
if (! ($relationship = $resource->getRelationship($name))) { |
|
|
|
|
291
|
|
|
continue; |
292
|
|
|
} |
293
|
|
|
|
294
|
3 |
|
$relationships[$name] = $relationship; |
295
|
|
|
|
296
|
3 |
|
if ($data = $relationship->getData()) { |
297
|
3 |
|
$children = is_array($data) ? $data : [$data]; |
298
|
|
|
|
299
|
3 |
|
$this->addResourcesToMap($map, $children, $nested); |
300
|
3 |
|
} |
301
|
15 |
|
} |
302
|
|
|
|
303
|
|
|
// Serialize the resource into an array and add it to the map. If |
304
|
|
|
// it is already present, its properties will be merged into the |
305
|
|
|
// existing resource. |
306
|
15 |
|
$this->addResourceToMap($map, $resource, $relationships); |
307
|
15 |
|
} |
308
|
15 |
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Serialize the given resource as an array and add it to the given map. |
312
|
|
|
* |
313
|
|
|
* If it is already present in the map, its properties will be merged into |
314
|
|
|
* the existing array. |
315
|
|
|
* |
316
|
|
|
* @param array &$map |
317
|
|
|
* @param ResourceInterface $resource |
318
|
|
|
* @param Relationship[] $resource |
319
|
|
|
*/ |
320
|
15 |
|
private function addResourceToMap(array &$map, ResourceInterface $resource, array $relationships) |
321
|
|
|
{ |
322
|
15 |
|
$type = $resource->getType(); |
323
|
15 |
|
$id = $resource->getId(); |
324
|
|
|
|
325
|
15 |
|
if (empty($map[$type][$id])) { |
326
|
15 |
|
$map[$type][$id] = [ |
327
|
15 |
|
'type' => $type, |
328
|
|
|
'id' => $id |
329
|
15 |
|
]; |
330
|
15 |
|
} |
331
|
|
|
|
332
|
15 |
|
$array = &$map[$type][$id]; |
333
|
15 |
|
$fields = $this->getFieldsForType($type); |
334
|
|
|
|
335
|
15 |
View Code Duplication |
if ($meta = $resource->getMeta()) { |
|
|
|
|
336
|
3 |
|
$array['meta'] = array_replace_recursive(isset($array['meta']) ? $array['meta'] : [], $meta); |
337
|
3 |
|
} |
338
|
|
|
|
339
|
15 |
View Code Duplication |
if ($links = $resource->getLinks()) { |
|
|
|
|
340
|
3 |
|
$array['links'] = array_replace_recursive(isset($array['links']) ? $array['links'] : [], $links); |
341
|
3 |
|
} |
342
|
|
|
|
343
|
15 |
|
if ($attributes = $resource->getAttributes($fields)) { |
344
|
6 |
|
if ($fields) { |
345
|
3 |
|
$attributes = array_intersect_key($attributes, array_flip($fields)); |
346
|
3 |
|
} |
347
|
6 |
|
if ($attributes) { |
|
|
|
|
348
|
6 |
|
$array['attributes'] = array_replace_recursive(isset($array['attributes']) ? $array['attributes'] : [], $attributes); |
|
|
|
|
349
|
6 |
|
} |
350
|
6 |
|
} |
351
|
|
|
|
352
|
15 |
|
if ($relationships && $fields) { |
|
|
|
|
353
|
|
|
$relationships = array_intersect_key($relationships, array_flip($fields)); |
354
|
|
|
} |
355
|
15 |
|
if ($relationships) { |
|
|
|
|
356
|
3 |
|
$relationships = array_map(function ($relationship) { |
357
|
3 |
|
return $relationship->toArray(); |
358
|
3 |
|
}, $relationships); |
359
|
|
|
|
360
|
3 |
|
$array['relationships'] = array_replace_recursive(isset($array['relationships']) ? $array['relationships'] : [], $relationships); |
|
|
|
|
361
|
3 |
|
} |
362
|
15 |
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Index relationship paths by top-level relationships. |
366
|
|
|
* |
367
|
|
|
* Given an array of relationship paths such as: |
368
|
|
|
* |
369
|
|
|
* ['user', 'user.employer', 'user.employer.country', 'comments'] |
370
|
|
|
* |
371
|
|
|
* Returns an array with key-value pairs of top-level relationships and |
372
|
|
|
* their nested relationships: |
373
|
|
|
* |
374
|
|
|
* ['user' => ['employer', 'employer.country'], 'comments' => []] |
375
|
|
|
* |
376
|
|
|
* @param array $paths |
377
|
|
|
* |
378
|
|
|
* @return array |
379
|
|
|
*/ |
380
|
15 |
|
private function indexRelationshipPaths(array $paths) |
381
|
|
|
{ |
382
|
15 |
|
$tree = []; |
383
|
|
|
|
384
|
15 |
|
foreach ($paths as $path) { |
385
|
3 |
|
list($primary, $nested) = array_pad(explode('.', $path, 2), 2, null); |
386
|
|
|
|
387
|
3 |
|
if (! isset($tree[$primary])) { |
388
|
3 |
|
$tree[$primary] = []; |
389
|
3 |
|
} |
390
|
|
|
|
391
|
3 |
|
if ($nested) { |
392
|
3 |
|
$tree[$primary][] = $nested; |
393
|
3 |
|
} |
394
|
15 |
|
} |
395
|
|
|
|
396
|
15 |
|
return $tree; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Get the fields that should be included for resources of the given type. |
401
|
|
|
* |
402
|
|
|
* @param string $type |
403
|
|
|
* |
404
|
|
|
* @return array|null |
405
|
|
|
*/ |
406
|
15 |
|
private function getFieldsForType($type) |
407
|
|
|
{ |
408
|
15 |
|
return isset($this->fields[$type]) ? $this->fields[$type] : null; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.