1 | <?php |
||
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() |
||
67 | 9 | ||
68 | /** |
||
69 | 9 | * @param ResourceInterface|ResourceInterface[] $data |
|
70 | 9 | * |
|
71 | * @return self |
||
72 | */ |
||
73 | public static function fromData($data) |
||
80 | |||
81 | 9 | /** |
|
82 | 3 | * @param array $meta |
|
83 | * |
||
84 | 3 | * @return self |
|
85 | */ |
||
86 | public static function fromMeta(array $meta) |
||
93 | |||
94 | /** |
||
95 | * @param Error[] $errors |
||
96 | 3 | * |
|
97 | 3 | * @return self |
|
98 | 9 | */ |
|
99 | 9 | public static function fromErrors(array $errors) |
|
106 | |||
107 | 9 | /** |
|
108 | * Get the primary data. |
||
109 | * |
||
110 | * @return ResourceInterface|ResourceInterface[]|null $data |
||
111 | */ |
||
112 | public function getData() |
||
116 | 3 | ||
117 | /** |
||
118 | 3 | * Set the data object. |
|
119 | 3 | * |
|
120 | * @param ResourceInterface|ResourceInterface[]|null $data |
||
121 | 3 | */ |
|
122 | public function setData($data) |
||
126 | |||
127 | 3 | /** |
|
128 | * Get the errors array. |
||
129 | * |
||
130 | * @return Error[]|null $errors |
||
131 | */ |
||
132 | public function getErrors() |
||
136 | |||
137 | /** |
||
138 | * Set the errors array. |
||
139 | * |
||
140 | * @param Error[]|null $errors |
||
141 | */ |
||
142 | public function setErrors(array $errors = null) |
||
146 | |||
147 | /** |
||
148 | * Set the jsonapi version. |
||
149 | * |
||
150 | * @param string $version |
||
151 | */ |
||
152 | public function setApiVersion($version) |
||
156 | |||
157 | /** |
||
158 | * Set the jsonapi meta information. |
||
159 | * |
||
160 | * @param array $meta |
||
161 | */ |
||
162 | public function setApiMeta(array $meta) |
||
166 | |||
167 | /** |
||
168 | * Get the relationships to include. |
||
169 | * |
||
170 | * @return string[] $include |
||
171 | */ |
||
172 | public function getInclude() |
||
176 | |||
177 | 12 | /** |
|
178 | * Set the relationships to include. |
||
179 | 12 | * |
|
180 | * @param string[] $include |
||
181 | 12 | */ |
|
182 | public function setInclude(array $include) |
||
186 | 9 | ||
187 | /** |
||
188 | 9 | * Get the sparse fieldsets. |
|
189 | * |
||
190 | 9 | * @return array[] $fields |
|
191 | 3 | */ |
|
192 | 3 | public function getFields() |
|
196 | |||
197 | 12 | /** |
|
198 | * Set the sparse fieldsets. |
||
199 | * |
||
200 | * @param array[] $fields |
||
201 | 12 | */ |
|
202 | public function setFields(array $fields) |
||
206 | |||
207 | /** |
||
208 | * Serialize for JSON usage. |
||
209 | 12 | * |
|
210 | * @return array |
||
211 | */ |
||
212 | public function jsonSerialize() |
||
253 | |||
254 | /** |
||
255 | * Build the JSON-API document and encode it as a JSON string. |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | public function __toString() |
||
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) |
||
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) |
||
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) |
||
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.