1 | <?php |
||||
2 | |||||
3 | namespace VGirol\JsonApi\Resources; |
||||
4 | |||||
5 | use Illuminate\Support\Arr; |
||||
6 | use VGirol\JsonApiConstant\Members; |
||||
7 | |||||
8 | trait HasDocumentConstructor |
||||
9 | { |
||||
10 | /** |
||||
11 | * Undocumented variable |
||||
12 | * |
||||
13 | * @var array |
||||
14 | */ |
||||
15 | protected $resultingArray; |
||||
16 | |||||
17 | /** |
||||
18 | * Undocumented function |
||||
19 | * |
||||
20 | * @param string $key |
||||
21 | * @param mixed $value |
||||
22 | * |
||||
23 | * @return static |
||||
24 | */ |
||||
25 | public function addDocumentMeta(string $key, $value) |
||||
26 | { |
||||
27 | return $this->addToWith(Members::META, $value, $key); |
||||
28 | } |
||||
29 | |||||
30 | /** |
||||
31 | * Undocumented function |
||||
32 | * |
||||
33 | * @param Request $request |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
34 | * |
||||
35 | * @return array|null |
||||
36 | */ |
||||
37 | public function getDocumentMeta($request) |
||||
38 | { |
||||
39 | return $this->getFromWith(Members::META, $request); |
||||
40 | } |
||||
41 | |||||
42 | /** |
||||
43 | * Undocumented function |
||||
44 | * |
||||
45 | * @param Request $request |
||||
46 | * |
||||
47 | * @return boolean |
||||
48 | */ |
||||
49 | public function hasDocumentMeta($request): bool |
||||
50 | { |
||||
51 | return $this->has($this->with($request), Members::META); |
||||
0 ignored issues
–
show
It seems like
with() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * Undocumented function |
||||
56 | * |
||||
57 | * @param string $key |
||||
58 | * @param array|string|null $value |
||||
59 | * |
||||
60 | * @return static |
||||
61 | */ |
||||
62 | public function addDocumentLink(string $key, $value) |
||||
63 | { |
||||
64 | return $this->addToWith(Members::LINKS, $value, $key); |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * Undocumented function |
||||
69 | * |
||||
70 | * @param Request $request |
||||
71 | * |
||||
72 | * @return mixed |
||||
73 | */ |
||||
74 | public function getDocumentLinks($request) |
||||
75 | { |
||||
76 | return $this->getFromWith(Members::LINKS, $request); |
||||
77 | } |
||||
78 | |||||
79 | /** |
||||
80 | * Undocumented function |
||||
81 | * |
||||
82 | * @param Request $request |
||||
83 | * |
||||
84 | * @return boolean |
||||
85 | */ |
||||
86 | public function hasDocumentLinks($request): bool |
||||
87 | { |
||||
88 | return $this->has($this->with($request), Members::LINKS); |
||||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * Undocumented function |
||||
93 | * |
||||
94 | * @param array $identification |
||||
95 | * |
||||
96 | * @return void |
||||
97 | */ |
||||
98 | public function setResourceIdentification(array $identification) |
||||
99 | { |
||||
100 | if (!isset($this->resultingArray)) { |
||||
101 | $this->resultingArray = []; |
||||
102 | } |
||||
103 | |||||
104 | return $this->resultingArray = array_merge($this->resultingArray, $identification); |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * Undocumented function |
||||
109 | * |
||||
110 | * @param array $attributes |
||||
111 | * |
||||
112 | * @return void |
||||
113 | */ |
||||
114 | public function setResourceAttributes(array $attributes) |
||||
115 | { |
||||
116 | return $this->set('resultingArray', Members::ATTRIBUTES, $attributes); |
||||
117 | } |
||||
118 | |||||
119 | /** |
||||
120 | * Undocumented function |
||||
121 | * |
||||
122 | * @param string $key |
||||
123 | * @param mixed $value |
||||
124 | * |
||||
125 | * @return static |
||||
126 | */ |
||||
127 | public function addResourceAttribute(string $key, $value) |
||||
128 | { |
||||
129 | return $this->addToResultingArray(Members::ATTRIBUTES, $value, $key); |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * Undocumented function |
||||
134 | * |
||||
135 | * @param string $key |
||||
136 | * @param mixed $value |
||||
137 | * |
||||
138 | * @return static |
||||
139 | */ |
||||
140 | public function addResourceRelationship(string $key, $value) |
||||
141 | { |
||||
142 | return $this->addToResultingArray(Members::RELATIONSHIPS, $value, $key); |
||||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * Undocumented function |
||||
147 | * |
||||
148 | * @param string $key |
||||
149 | * @param mixed $value |
||||
150 | * |
||||
151 | * @return static |
||||
152 | */ |
||||
153 | public function addResourceMeta(string $key, $value) |
||||
154 | { |
||||
155 | return $this->addToResultingArray(Members::META, $value, $key); |
||||
156 | } |
||||
157 | |||||
158 | /** |
||||
159 | * Undocumented function |
||||
160 | * |
||||
161 | * @param string $key |
||||
162 | * @param array|string|null $value |
||||
163 | * |
||||
164 | * @return static |
||||
165 | */ |
||||
166 | public function addResourceLink(string $key, $value) |
||||
167 | { |
||||
168 | return $this->addToResultingArray(Members::LINKS, $value, $key); |
||||
169 | } |
||||
170 | |||||
171 | /** |
||||
172 | * Undocumented function |
||||
173 | * |
||||
174 | * @param string $key |
||||
175 | * @param mixed $value |
||||
176 | * |
||||
177 | * @return static |
||||
178 | */ |
||||
179 | public function addRelationshipMeta(string $key, $value) |
||||
180 | { |
||||
181 | return $this->addToAdditional(Members::META, $value, $key); |
||||
182 | } |
||||
183 | |||||
184 | /** |
||||
185 | * Undocumented function |
||||
186 | * |
||||
187 | * @param string $key |
||||
188 | * @param array|string|null $value |
||||
189 | * |
||||
190 | * @return static |
||||
191 | */ |
||||
192 | public function addRelationshipLink(string $key, $value) |
||||
193 | { |
||||
194 | return $this->addToAdditional(Members::LINKS, $value, $key); |
||||
195 | } |
||||
196 | |||||
197 | /** |
||||
198 | * Undocumented function |
||||
199 | * |
||||
200 | * @param array $resource A single resource object or an array of resource objects |
||||
201 | * |
||||
202 | * @return static |
||||
203 | */ |
||||
204 | public function addIncludedResources($resource) |
||||
205 | { |
||||
206 | return $this->addToWith(Members::INCLUDED, $resource); |
||||
207 | } |
||||
208 | |||||
209 | /** |
||||
210 | * Undocumented function |
||||
211 | * |
||||
212 | * @param Request $request |
||||
213 | * |
||||
214 | * @return mixed |
||||
215 | */ |
||||
216 | public function getIncluded($request) |
||||
217 | { |
||||
218 | return $this->getFromWith(Members::INCLUDED, $request); |
||||
219 | } |
||||
220 | |||||
221 | /** |
||||
222 | * Undocumented function |
||||
223 | * |
||||
224 | * @param Request $request |
||||
225 | * |
||||
226 | * @return boolean |
||||
227 | */ |
||||
228 | public function hasIncluded($request): bool |
||||
229 | { |
||||
230 | return $this->has($this->with($request), Members::INCLUDED); |
||||
231 | } |
||||
232 | |||||
233 | /** |
||||
234 | * Undocumented function |
||||
235 | * |
||||
236 | * @param string $key |
||||
237 | * @param mixed $value |
||||
238 | * |
||||
239 | * @return static |
||||
240 | */ |
||||
241 | public function addErrorMeta(string $key, $value) |
||||
242 | { |
||||
243 | return $this->addToResultingArray(Members::META, $value, $key); |
||||
244 | } |
||||
245 | |||||
246 | /** |
||||
247 | * Undocumented function |
||||
248 | * |
||||
249 | * @param string $key |
||||
250 | * @param mixed $value |
||||
251 | * |
||||
252 | * @return static |
||||
253 | */ |
||||
254 | public function addJsonapiMeta(string $key, $value) |
||||
255 | { |
||||
256 | if (!isset($this->with[Members::JSONAPI])) { |
||||
257 | $this->with[Members::JSONAPI] = []; |
||||
0 ignored issues
–
show
|
|||||
258 | } |
||||
259 | if (!isset($this->with[Members::JSONAPI][Members::META])) { |
||||
260 | $this->with[Members::JSONAPI][Members::META] = []; |
||||
261 | } |
||||
262 | $this->with[Members::JSONAPI][Members::META][$key] = $value; |
||||
263 | |||||
264 | return $this; |
||||
265 | } |
||||
266 | |||||
267 | /** |
||||
268 | * Undocumented function |
||||
269 | * |
||||
270 | * @param mixed $value |
||||
271 | * |
||||
272 | * @return static |
||||
273 | */ |
||||
274 | public function addJsonapiVersion($value) |
||||
275 | { |
||||
276 | return $this->addToWith(Members::JSONAPI, $value, Members::JSONAPI_VERSION); |
||||
277 | } |
||||
278 | |||||
279 | /** |
||||
280 | * Undocumented function |
||||
281 | * |
||||
282 | * @param string $member |
||||
283 | * @param string $key |
||||
284 | * @param mixed $value |
||||
285 | * |
||||
286 | * @return static |
||||
287 | */ |
||||
288 | private function addToResultingArray(string $member, $value, string $key = null) |
||||
289 | { |
||||
290 | return $this->addTo('resultingArray', $member, $value, $key); |
||||
291 | } |
||||
292 | |||||
293 | /** |
||||
294 | * Undocumented function |
||||
295 | * |
||||
296 | * @param string $member |
||||
297 | * @param mixed $value |
||||
298 | * @param string $key |
||||
299 | * |
||||
300 | * @return static |
||||
301 | */ |
||||
302 | private function addToWith(string $member, $value, string $key = null) |
||||
303 | { |
||||
304 | return $this->addTo('with', $member, $value, $key); |
||||
305 | } |
||||
306 | |||||
307 | /** |
||||
308 | * Undocumented function |
||||
309 | * |
||||
310 | * @param string $member |
||||
311 | * @param string $key |
||||
312 | * @param mixed $value |
||||
313 | * |
||||
314 | * @return static |
||||
315 | */ |
||||
316 | private function addToAdditional(string $member, $value, string $key = null) |
||||
317 | { |
||||
318 | return $this->addTo('additional', $member, $value, $key); |
||||
319 | } |
||||
320 | |||||
321 | /** |
||||
322 | * Undocumented function |
||||
323 | * |
||||
324 | * @param string $property |
||||
325 | * @param string $member |
||||
326 | * @param mixed $value |
||||
327 | * @param string $key |
||||
328 | * |
||||
329 | * @return static |
||||
330 | */ |
||||
331 | private function addTo(string $property, string $member, $value, string $key = null) |
||||
332 | { |
||||
333 | if (!isset(($this->$property)[$member])) { |
||||
334 | ($this->$property)[$member] = []; |
||||
335 | } |
||||
336 | if (is_null($key)) { |
||||
337 | if (Arr::isAssoc($value)) { |
||||
338 | $value = [$value]; |
||||
339 | } |
||||
340 | ($this->$property)[$member] = array_merge(($this->$property)[$member], $value); |
||||
341 | } else { |
||||
342 | ($this->$property)[$member][$key] = $value; |
||||
343 | } |
||||
344 | |||||
345 | return $this; |
||||
346 | } |
||||
347 | |||||
348 | /** |
||||
349 | * Undocumented function |
||||
350 | * |
||||
351 | * @param string $property |
||||
352 | * @param string $member |
||||
353 | * @param mixed $value |
||||
354 | * |
||||
355 | * @return static |
||||
356 | */ |
||||
357 | private function set(string $property, string $member, $value) |
||||
358 | { |
||||
359 | if (!isset(($this->$property)[$member])) { |
||||
360 | ($this->$property)[$member] = []; |
||||
361 | } |
||||
362 | ($this->$property)[$member] = $value; |
||||
363 | |||||
364 | return $this; |
||||
365 | } |
||||
366 | |||||
367 | /** |
||||
368 | * Undocumented function |
||||
369 | * |
||||
370 | * @param array $member |
||||
371 | * @param string $key |
||||
372 | * |
||||
373 | * @return boolean |
||||
374 | */ |
||||
375 | private function has(array $member, string $key): bool |
||||
376 | { |
||||
377 | return array_key_exists($key, $member); |
||||
378 | } |
||||
379 | |||||
380 | /** |
||||
381 | * Undocumented function |
||||
382 | * |
||||
383 | * @param string $key |
||||
384 | * @param Request $request |
||||
385 | * |
||||
386 | * @return mixed |
||||
387 | */ |
||||
388 | private function getFromWith(string $key, $request) |
||||
389 | { |
||||
390 | return $this->getFrom($this->with($request), $key); |
||||
391 | } |
||||
392 | |||||
393 | /** |
||||
394 | * Undocumented function |
||||
395 | * |
||||
396 | * @param string $key |
||||
397 | * @param Request $request |
||||
398 | * |
||||
399 | * @return mixed |
||||
400 | */ |
||||
401 | private function getFromAdditional(string $key) |
||||
402 | { |
||||
403 | return $this->getFrom($this->additional, $key); |
||||
404 | } |
||||
405 | |||||
406 | /** |
||||
407 | * Undocumented function |
||||
408 | * |
||||
409 | * @param array $member |
||||
410 | * @param string $key |
||||
411 | * |
||||
412 | * @return mixed |
||||
413 | */ |
||||
414 | private function getFrom(array $member, string $key) |
||||
415 | { |
||||
416 | return array_filter( |
||||
417 | $member, |
||||
418 | function ($k) use ($key) { |
||||
419 | return $k == $key; |
||||
420 | }, |
||||
421 | ARRAY_FILTER_USE_KEY |
||||
422 | ); |
||||
423 | } |
||||
424 | } |
||||
425 |