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