This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\Fractalistic; |
||
4 | |||
5 | use JsonSerializable; |
||
6 | use League\Fractal\Manager; |
||
7 | use League\Fractal\Pagination\CursorInterface; |
||
8 | use League\Fractal\Pagination\PaginatorInterface; |
||
9 | use Spatie\Fractalistic\Exceptions\InvalidTransformation; |
||
10 | use Spatie\Fractalistic\Exceptions\NoTransformerSpecified; |
||
11 | use Traversable; |
||
12 | |||
13 | class Fractal implements JsonSerializable |
||
14 | { |
||
15 | /** @var \League\Fractal\Manager */ |
||
16 | protected $manager; |
||
17 | |||
18 | /** @var int */ |
||
19 | protected $recursionLimit = 10; |
||
20 | |||
21 | /** @var string|\League\Fractal\Serializer\SerializerAbstract */ |
||
22 | protected $serializer; |
||
23 | |||
24 | /** @var string|callable|\League\Fractal\TransformerAbstract */ |
||
25 | protected $transformer; |
||
26 | |||
27 | /** @var \League\Fractal\Pagination\PaginatorInterface */ |
||
28 | protected $paginator; |
||
29 | |||
30 | /** @var \League\Fractal\Pagination\CursorInterface */ |
||
31 | protected $cursor; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $includes = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $excludes = []; |
||
38 | |||
39 | /** @var array */ |
||
40 | protected $fieldsets = []; |
||
41 | |||
42 | /** @var string */ |
||
43 | protected $dataType; |
||
44 | |||
45 | /** @var mixed */ |
||
46 | protected $data; |
||
47 | |||
48 | /** @var string */ |
||
49 | protected $resourceName; |
||
50 | |||
51 | /** @var array */ |
||
52 | protected $meta = []; |
||
53 | |||
54 | /** |
||
55 | * @param null|mixed $data |
||
56 | * @param null|string|callable|\League\Fractal\TransformerAbstract $transformer |
||
57 | * @param null|string|\League\Fractal\Serializer\SerializerAbstract $serializer |
||
58 | * |
||
59 | * @return \Spatie\Fractalistic\Fractal |
||
60 | */ |
||
61 | public static function create($data = null, $transformer = null, $serializer = null) |
||
62 | { |
||
63 | $instance = new static(new Manager()); |
||
64 | |||
65 | $instance->data = $data; |
||
66 | $instance->dataType = $instance->determineDataType($data); |
||
67 | $instance->transformer = $transformer ?: null; |
||
68 | $instance->serializer = $serializer ?: null; |
||
69 | |||
70 | return $instance; |
||
71 | } |
||
72 | |||
73 | /** @param \League\Fractal\Manager $manager */ |
||
74 | public function __construct(Manager $manager) |
||
75 | { |
||
76 | $this->manager = $manager; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Set the collection data that must be transformed. |
||
81 | * |
||
82 | * @param mixed $data |
||
83 | * @param null|string|callable|\League\Fractal\TransformerAbstract $transformer |
||
84 | * @param null|string $resourceName |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | View Code Duplication | public function collection($data, $transformer = null, $resourceName = null) |
|
0 ignored issues
–
show
|
|||
89 | { |
||
90 | if (! is_null($resourceName)) { |
||
91 | $this->resourceName = $resourceName; |
||
92 | } |
||
93 | |||
94 | return $this->data('collection', $data, $transformer); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Set the item data that must be transformed. |
||
99 | * |
||
100 | * @param mixed $data |
||
101 | * @param null|string|callable|\League\Fractal\TransformerAbstract $transformer |
||
102 | * @param null|string $resourceName |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | View Code Duplication | public function item($data, $transformer = null, $resourceName = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
107 | { |
||
108 | if (! is_null($resourceName)) { |
||
109 | $this->resourceName = $resourceName; |
||
110 | } |
||
111 | |||
112 | return $this->data('item', $data, $transformer); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Set the primitive data that must be transformed. |
||
117 | * |
||
118 | * @param mixed $data |
||
119 | * @param null|string|callable|\League\Fractal\TransformerAbstract $transformer |
||
120 | * @param null|string $resourceName |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | View Code Duplication | public function primitive($data, $transformer = null, $resourceName = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
125 | { |
||
126 | if (! is_null($resourceName)) { |
||
127 | $this->resourceName = $resourceName; |
||
128 | } |
||
129 | |||
130 | return $this->data('primitive', $data, $transformer); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Set the data that must be transformed. |
||
135 | * |
||
136 | * @param string $dataType |
||
137 | * @param mixed $data |
||
138 | * @param null|string|callable|\League\Fractal\TransformerAbstract $transformer |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function data($dataType, $data, $transformer = null) |
||
143 | { |
||
144 | $this->dataType = $dataType; |
||
145 | |||
146 | $this->data = $data; |
||
147 | |||
148 | if (! is_null($transformer)) { |
||
149 | $this->transformer = $transformer; |
||
150 | } |
||
151 | |||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param mixed $data |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | protected function determineDataType($data) |
||
161 | { |
||
162 | if (is_null($data)) { |
||
163 | return 'NullResource'; |
||
164 | } |
||
165 | |||
166 | if (is_array($data)) { |
||
167 | return 'collection'; |
||
168 | } |
||
169 | |||
170 | if ($data instanceof Traversable) { |
||
171 | return 'collection'; |
||
172 | } |
||
173 | |||
174 | return 'item'; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Set the class or function that will perform the transform. |
||
179 | * |
||
180 | * @param string|callable|\League\Fractal\TransformerAbstract $transformer |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function transformWith($transformer) |
||
185 | { |
||
186 | $this->transformer = $transformer; |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Set the serializer to be used. |
||
193 | * |
||
194 | * @param string|\League\Fractal\Serializer\SerializerAbstract $serializer |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function serializeWith($serializer) |
||
199 | { |
||
200 | $this->serializer = $serializer; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Set a Fractal paginator for the data. |
||
207 | * |
||
208 | * @param \League\Fractal\Pagination\PaginatorInterface $paginator |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function paginateWith(PaginatorInterface $paginator) |
||
213 | { |
||
214 | $this->paginator = $paginator; |
||
215 | |||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Set a Fractal cursor for the data. |
||
221 | * |
||
222 | * @param \League\Fractal\Pagination\CursorInterface $cursor |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function withCursor(CursorInterface $cursor) |
||
227 | { |
||
228 | $this->cursor = $cursor; |
||
229 | |||
230 | return $this; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Specify the includes. |
||
235 | * |
||
236 | * @param array|string $includes Array or string of resources to include. |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function parseIncludes($includes) |
||
241 | { |
||
242 | $includes = $this->normalizeIncludesOrExcludes($includes); |
||
243 | |||
244 | $this->includes = array_merge($this->includes, (array) $includes); |
||
245 | |||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Specify the excludes. |
||
251 | * |
||
252 | * @param array|string $excludes Array or string of resources to exclude. |
||
253 | * |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function parseExcludes($excludes) |
||
257 | { |
||
258 | $excludes = $this->normalizeIncludesOrExcludes($excludes); |
||
259 | |||
260 | $this->excludes = array_merge($this->excludes, (array) $excludes); |
||
261 | |||
262 | return $this; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Specify the fieldsets to include in the response. |
||
267 | * |
||
268 | * @param array $fieldsets array with key = resourceName and value = fields to include |
||
269 | * (array or comma separated string with field names) |
||
270 | * |
||
271 | * @return $this |
||
272 | */ |
||
273 | public function parseFieldsets(array $fieldsets) |
||
274 | { |
||
275 | foreach ($fieldsets as $key => $fields) { |
||
276 | if (is_array($fields)) { |
||
277 | $fieldsets[$key] = implode(',', $fields); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | $this->fieldsets = array_merge($this->fieldsets, $fieldsets); |
||
282 | |||
283 | return $this; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Normalize the includes an excludes. |
||
288 | * |
||
289 | * @param array|string $includesOrExcludes |
||
290 | * |
||
291 | * @return array|string |
||
292 | */ |
||
293 | protected function normalizeIncludesOrExcludes($includesOrExcludes = '') |
||
294 | { |
||
295 | if (! is_string($includesOrExcludes)) { |
||
296 | return $includesOrExcludes; |
||
297 | } |
||
298 | |||
299 | return array_map(function ($value) { |
||
300 | return trim($value); |
||
301 | }, explode(',', $includesOrExcludes)); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Set the meta data. |
||
306 | * |
||
307 | * @param $array,... |
||
308 | * |
||
309 | * @return $this |
||
310 | */ |
||
311 | public function addMeta() |
||
312 | { |
||
313 | foreach (func_get_args() as $meta) { |
||
314 | if (is_array($meta)) { |
||
315 | $this->meta += $meta; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return $this; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Set the resource name, to replace 'data' as the root of the collection or item. |
||
324 | * |
||
325 | * @param string $resourceName |
||
326 | * |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function withResourceName($resourceName) |
||
330 | { |
||
331 | $this->resourceName = $resourceName; |
||
332 | |||
333 | return $this; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Upper limit to how many levels of included data are allowed. |
||
338 | * |
||
339 | * @param int $recursionLimit |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function limitRecursion(int $recursionLimit) |
||
344 | { |
||
345 | $this->recursionLimit = $recursionLimit; |
||
346 | |||
347 | return $this; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Perform the transformation to json. |
||
352 | * |
||
353 | * @param int $options |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | public function toJson($options = 0) |
||
358 | { |
||
359 | return $this->createData()->toJson($options); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Perform the transformation to array. |
||
364 | * |
||
365 | * @return array |
||
366 | */ |
||
367 | public function toArray() |
||
368 | { |
||
369 | return $this->createData()->toArray(); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Create fractal data. |
||
374 | * |
||
375 | * @return \League\Fractal\Scope |
||
376 | * |
||
377 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
378 | * @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified |
||
379 | */ |
||
380 | public function createData() |
||
381 | { |
||
382 | if (is_null($this->transformer)) { |
||
383 | throw new NoTransformerSpecified(); |
||
384 | } |
||
385 | |||
386 | if (is_string($this->serializer)) { |
||
387 | $this->serializer = new $this->serializer; |
||
388 | } |
||
389 | |||
390 | if (! is_null($this->serializer)) { |
||
391 | $this->manager->setSerializer($this->serializer); |
||
392 | } |
||
393 | |||
394 | $this->manager->setRecursionLimit($this->recursionLimit); |
||
395 | |||
396 | if (! empty($this->includes)) { |
||
397 | $this->manager->parseIncludes($this->includes); |
||
398 | } |
||
399 | |||
400 | if (! empty($this->excludes)) { |
||
401 | $this->manager->parseExcludes($this->excludes); |
||
402 | } |
||
403 | |||
404 | if (! empty($this->fieldsets)) { |
||
405 | $this->manager->parseFieldsets($this->fieldsets); |
||
406 | } |
||
407 | |||
408 | return $this->manager->createData($this->getResource(), $this->resourceName); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Get the resource. |
||
413 | * |
||
414 | * @return \League\Fractal\Resource\ResourceInterface |
||
415 | * |
||
416 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
417 | */ |
||
418 | public function getResource() |
||
419 | { |
||
420 | $resourceClass = 'League\\Fractal\\Resource\\'.ucfirst($this->dataType); |
||
421 | |||
422 | if (! class_exists($resourceClass)) { |
||
423 | throw new InvalidTransformation(); |
||
424 | } |
||
425 | |||
426 | if (is_string($this->transformer)) { |
||
427 | $this->transformer = new $this->transformer; |
||
428 | } |
||
429 | |||
430 | $resource = new $resourceClass($this->data, $this->transformer, $this->resourceName); |
||
431 | |||
432 | $resource->setMeta($this->meta); |
||
433 | |||
434 | if (! is_null($this->paginator)) { |
||
435 | $resource->setPaginator($this->paginator); |
||
436 | } |
||
437 | |||
438 | if (! is_null($this->cursor)) { |
||
439 | $resource->setCursor($this->cursor); |
||
440 | } |
||
441 | |||
442 | return $resource; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Return the name of the resource. |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | public function getResourceName() |
||
451 | { |
||
452 | return $this->resourceName; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Convert the object into something JSON serializable. |
||
457 | */ |
||
458 | public function jsonSerialize() |
||
459 | { |
||
460 | return $this->toArray(); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Support for magic methods to included data. |
||
465 | * |
||
466 | * @param string $name |
||
467 | * @param array $arguments |
||
468 | * |
||
469 | * @return $this |
||
470 | */ |
||
471 | public function __call($name, array $arguments) |
||
472 | { |
||
473 | View Code Duplication | if ($this->startsWith($name, ['include'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
474 | $includeName = lcfirst(substr($name, strlen('include'))); |
||
475 | |||
476 | return $this->parseIncludes($includeName); |
||
477 | } |
||
478 | |||
479 | View Code Duplication | if ($this->startsWith($name, ['exclude'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
480 | $excludeName = lcfirst(substr($name, strlen('exclude'))); |
||
481 | |||
482 | return $this->parseExcludes($excludeName); |
||
483 | } |
||
484 | |||
485 | trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR); |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * Determine if a given string starts with a given substring. |
||
490 | * |
||
491 | * @param string $haystack |
||
492 | * @param string|array $needles |
||
493 | * |
||
494 | * @return bool |
||
495 | */ |
||
496 | protected function startsWith($haystack, $needles) |
||
497 | { |
||
498 | foreach ((array) $needles as $needle) { |
||
499 | if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) { |
||
500 | return true; |
||
501 | } |
||
502 | } |
||
503 | |||
504 | return false; |
||
505 | } |
||
506 | } |
||
507 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.