1 | <?php |
||
16 | class Document implements JsonSerializable |
||
17 | { |
||
18 | use LinksTrait, SelfLinkTrait, PaginationLinksTrait, MetaTrait; |
||
19 | |||
20 | const MEDIA_TYPE = 'application/vnd.api+json'; |
||
21 | |||
22 | private $data; |
||
23 | private $errors; |
||
24 | private $jsonapi; |
||
25 | |||
26 | private $include = []; |
||
27 | private $fields = []; |
||
28 | |||
29 | 24 | private function __construct() |
|
32 | |||
33 | /** |
||
34 | * @param ResourceInterface|ResourceInterface[] $data |
||
35 | * |
||
36 | * @return self |
||
37 | */ |
||
38 | 15 | public static function fromData($data) |
|
45 | |||
46 | /** |
||
47 | * @param array $meta |
||
48 | * |
||
49 | * @return self |
||
50 | */ |
||
51 | 6 | public static function fromMeta(array $meta) |
|
58 | |||
59 | /** |
||
60 | * @param Error[] $errors |
||
61 | * |
||
62 | * @return self |
||
63 | */ |
||
64 | 3 | public static function fromErrors(array $errors) |
|
71 | |||
72 | /** |
||
73 | * Set the primary data. |
||
74 | * |
||
75 | * @param ResourceInterface|ResourceInterface[]|null $data |
||
76 | */ |
||
77 | 15 | public function setData($data) |
|
81 | |||
82 | /** |
||
83 | * Set the errors array. |
||
84 | * |
||
85 | * @param Error[]|null $errors |
||
86 | */ |
||
87 | 3 | public function setErrors(array $errors = null) |
|
91 | |||
92 | /** |
||
93 | * Set the jsonapi version. |
||
94 | * |
||
95 | * @param string $version |
||
96 | */ |
||
97 | public function setApiVersion($version) |
||
101 | |||
102 | /** |
||
103 | * Set the jsonapi meta information. |
||
104 | * |
||
105 | * @param array $meta |
||
106 | */ |
||
107 | public function setApiMeta(array $meta) |
||
111 | |||
112 | /** |
||
113 | * Set the relationship paths to include. |
||
114 | * |
||
115 | * @param string[] $include |
||
116 | */ |
||
117 | 3 | public function setInclude($include) |
|
118 | { |
||
119 | 3 | $this->include = $include; |
|
120 | 3 | } |
|
121 | |||
122 | /** |
||
123 | * Set the sparse fieldsets. |
||
124 | * |
||
125 | * @param array $fields |
||
126 | */ |
||
127 | 3 | public function setFields($fields) |
|
131 | |||
132 | /** |
||
133 | * Serialize for JSON usage. |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 24 | public function jsonSerialize() |
|
163 | |||
164 | 15 | private function buildResourceMap(array $resources) |
|
174 | |||
175 | 15 | private function mergeResources(array &$map, array $resources, array $include) |
|
176 | { |
||
177 | 15 | foreach ($resources as $resource) { |
|
178 | 15 | $relationships = []; |
|
179 | |||
180 | 15 | foreach ($include as $name => $nested) { |
|
181 | 3 | if (! ($relationship = $resource->getRelationship($name))) { |
|
182 | continue; |
||
183 | } |
||
184 | |||
185 | 3 | $relationships[$name] = $relationship; |
|
186 | |||
187 | 3 | if ($data = $relationship->getData()) { |
|
188 | 3 | $children = is_array($data) ? $data : [$data]; |
|
189 | |||
190 | 3 | $this->mergeResources($map, $children, $nested); |
|
191 | 3 | } |
|
192 | 15 | } |
|
193 | |||
194 | 15 | $this->mergeResource($map, $resource, $relationships); |
|
195 | 15 | } |
|
196 | 15 | } |
|
197 | |||
198 | 15 | private function mergeResource(array &$map, ResourceInterface $resource, array $relationships) |
|
225 | |||
226 | 15 | private function extractResourcesFromMap(array &$map, array $resources) |
|
242 | |||
243 | 15 | private function buildRelationshipTree(array $paths) |
|
244 | { |
||
262 | } |
||
263 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.