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 League\Fractal\Serializer\SerializerAbstract; |
||
10 | use Spatie\Fractalistic\Exceptions\InvalidTransformation; |
||
11 | use Spatie\Fractalistic\Exceptions\NoTransformerSpecified; |
||
12 | |||
13 | class Fractal implements JsonSerializable |
||
14 | { |
||
15 | /** @var \League\Fractal\Manager */ |
||
16 | protected $manager; |
||
17 | |||
18 | /** @var \League\Fractal\Serializer\SerializerAbstract */ |
||
19 | protected $serializer; |
||
20 | |||
21 | /** @var \League\Fractal\TransformerAbstract|callable */ |
||
22 | protected $transformer; |
||
23 | |||
24 | /** @var \League\Fractal\Pagination\PaginatorInterface */ |
||
25 | protected $paginator; |
||
26 | |||
27 | /** @var \League\Fractal\Pagination\CursorInterface */ |
||
28 | protected $cursor; |
||
29 | |||
30 | /** @var array */ |
||
31 | protected $includes = []; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $excludes = []; |
||
35 | |||
36 | /** @var string */ |
||
37 | protected $dataType; |
||
38 | |||
39 | /** @var mixed */ |
||
40 | protected $data; |
||
41 | |||
42 | /** @var string */ |
||
43 | protected $resourceName; |
||
44 | |||
45 | /** @var array */ |
||
46 | protected $meta = []; |
||
47 | |||
48 | /** |
||
49 | * @param null|mixed $data |
||
0 ignored issues
–
show
|
|||
50 | * @param null|callable|\League\Fractal\TransformerAbstract $transformer |
||
0 ignored issues
–
show
There is no parameter named
$transformer . Was it maybe removed?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. Consider the following example. The parameter /**
* @param array $germany
* @param array $island
* @param array $italy
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was removed, but the annotation was not.
Loading history...
|
|||
51 | * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer |
||
0 ignored issues
–
show
There is no parameter named
$serializer . Was it maybe removed?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. Consider the following example. The parameter /**
* @param array $germany
* @param array $island
* @param array $italy
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was removed, but the annotation was not.
Loading history...
|
|||
52 | * |
||
53 | * @return \Spatie\Fractalistic\Fractal |
||
54 | */ |
||
55 | public static function create() |
||
56 | { |
||
57 | $factory = new FractalFactory(func_get_args()); |
||
58 | |||
59 | return $factory->getFractalInstance(); |
||
60 | } |
||
61 | |||
62 | /** @param \League\Fractal\Manager $manager */ |
||
63 | public function __construct(Manager $manager) |
||
64 | { |
||
65 | $this->manager = $manager; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Set the collection data that must be transformed. |
||
70 | * |
||
71 | * @param mixed $data |
||
72 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
73 | * @param string|null $resourceName |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function collection($data, $transformer = null, $resourceName = null) |
||
78 | { |
||
79 | $this->resourceName = $resourceName; |
||
80 | |||
81 | return $this->data('collection', $data, $transformer); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set the item data that must be transformed. |
||
86 | * |
||
87 | * @param mixed $data |
||
88 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
89 | * @param string|null $resourceName |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function item($data, $transformer = null, $resourceName = null) |
||
94 | { |
||
95 | $this->resourceName = $resourceName; |
||
96 | |||
97 | return $this->data('item', $data, $transformer); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Set the data that must be transformed. |
||
102 | * |
||
103 | * @param string $dataType |
||
104 | * @param mixed $data |
||
105 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function data($dataType, $data, $transformer = null) |
||
110 | { |
||
111 | $this->dataType = $dataType; |
||
112 | |||
113 | $this->data = $data; |
||
114 | |||
115 | if (! is_null($transformer)) { |
||
116 | $this->transformer = $transformer; |
||
117 | } |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Set the class or function that will perform the transform. |
||
124 | * |
||
125 | * @param \League\Fractal\TransformerAbstract|callable $transformer |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function transformWith($transformer) |
||
130 | { |
||
131 | $this->transformer = $transformer; |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Set the serializer to be used. |
||
138 | * |
||
139 | * @param \League\Fractal\Serializer\SerializerAbstract $serializer |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function serializeWith(SerializerAbstract $serializer) |
||
144 | { |
||
145 | $this->serializer = $serializer; |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Set a Fractal paginator for the data. |
||
152 | * |
||
153 | * @param \League\Fractal\Pagination\PaginatorInterface $paginator |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function paginateWith(PaginatorInterface $paginator) |
||
158 | { |
||
159 | $this->paginator = $paginator; |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set a Fractal cursor for the data. |
||
166 | * |
||
167 | * @param \League\Fractal\Pagination\CursorInterface $cursor |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function withCursor(CursorInterface $cursor) |
||
172 | { |
||
173 | $this->cursor = $cursor; |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Specify the includes. |
||
180 | * |
||
181 | * @param array|string $includes Array or string of resources to include. |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function parseIncludes($includes) |
||
186 | { |
||
187 | $includes = $this->normalizeIncludesOrExcludes($includes); |
||
188 | |||
189 | $this->includes = array_merge($this->includes, (array) $includes); |
||
190 | |||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Specify the excludes. |
||
196 | * |
||
197 | * @param array|string $excludes Array or string of resources to exclude. |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function parseExcludes($excludes) |
||
201 | { |
||
202 | $excludes = $this->normalizeIncludesOrExcludes($excludes); |
||
203 | |||
204 | $this->excludes = array_merge($this->excludes, (array) $excludes); |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Normalize the includes an excludes. |
||
211 | * |
||
212 | * @param array|string $includesOrExcludes |
||
213 | * |
||
214 | * @return array|string |
||
215 | */ |
||
216 | protected function normalizeIncludesOrExcludes($includesOrExcludes = '') |
||
217 | { |
||
218 | if (! is_string($includesOrExcludes)) { |
||
219 | return $includesOrExcludes; |
||
220 | } |
||
221 | |||
222 | return array_map(function ($value) { |
||
223 | return trim($value); |
||
224 | }, explode(',', $includesOrExcludes)); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Set the meta data. |
||
229 | * |
||
230 | * @param $array,... |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function addMeta() |
||
235 | { |
||
236 | foreach (func_get_args() as $meta) { |
||
237 | if (is_array($meta)) { |
||
238 | $this->meta += $meta; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Set the resource name, to replace 'data' as the root of the collection or item. |
||
247 | * |
||
248 | * @param string $resourceName |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function withResourceName($resourceName) |
||
253 | { |
||
254 | $this->resourceName = $resourceName; |
||
255 | |||
256 | return $this; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Perform the transformation to json. |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function toJson() |
||
265 | { |
||
266 | return $this->createData()->toJson(); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Perform the transformation to array. |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | public function toArray() |
||
275 | { |
||
276 | return $this->createData()->toArray(); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Create fractal data. |
||
281 | * |
||
282 | * @return \League\Fractal\Scope |
||
283 | * |
||
284 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
285 | * @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified |
||
286 | */ |
||
287 | public function createData() |
||
288 | { |
||
289 | if (is_null($this->transformer)) { |
||
290 | throw new NoTransformerSpecified(); |
||
291 | } |
||
292 | |||
293 | if (! is_null($this->serializer)) { |
||
294 | $this->manager->setSerializer($this->serializer); |
||
295 | } |
||
296 | |||
297 | if (! is_null($this->includes)) { |
||
298 | $this->manager->parseIncludes($this->includes); |
||
299 | } |
||
300 | |||
301 | if (! is_null($this->excludes)) { |
||
302 | $this->manager->parseExcludes($this->excludes); |
||
303 | } |
||
304 | |||
305 | return $this->manager->createData($this->getResource()); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get the resource. |
||
310 | * |
||
311 | * @return \League\Fractal\Resource\ResourceInterface |
||
312 | * |
||
313 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
314 | */ |
||
315 | public function getResource() |
||
316 | { |
||
317 | $resourceClass = 'League\\Fractal\\Resource\\'.ucfirst($this->dataType); |
||
318 | |||
319 | if (! class_exists($resourceClass)) { |
||
320 | throw new InvalidTransformation(); |
||
321 | } |
||
322 | |||
323 | $resource = new $resourceClass($this->data, $this->transformer, $this->resourceName); |
||
324 | |||
325 | $resource->setMeta($this->meta); |
||
326 | |||
327 | if (! is_null($this->paginator)) { |
||
328 | $resource->setPaginator($this->paginator); |
||
329 | } |
||
330 | |||
331 | if (! is_null($this->cursor)) { |
||
332 | $resource->setCursor($this->cursor); |
||
333 | } |
||
334 | |||
335 | return $resource; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Convert the object into something JSON serializable. |
||
340 | */ |
||
341 | public function jsonSerialize() |
||
342 | { |
||
343 | return $this->toArray(); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Support for magic methods to included data. |
||
348 | * |
||
349 | * @param string $name |
||
350 | * @param array $arguments |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function __call($name, array $arguments) |
||
355 | { |
||
356 | View Code Duplication | if ($this->startsWith($name, ['include'])) { |
|
357 | $includeName = lcfirst(substr($name, strlen('include'))); |
||
358 | |||
359 | return $this->parseIncludes($includeName); |
||
360 | } |
||
361 | |||
362 | View Code Duplication | if ($this->startsWith($name, ['exclude'])) { |
|
363 | $excludeName = lcfirst(substr($name, strlen('exclude'))); |
||
364 | |||
365 | return $this->parseExcludes($excludeName); |
||
366 | } |
||
367 | |||
368 | trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Determine if a given string starts with a given substring. |
||
373 | * |
||
374 | * @param string $haystack |
||
375 | * @param string|array $needles |
||
376 | * @return bool |
||
377 | */ |
||
378 | protected function startsWith($haystack, $needles) |
||
379 | { |
||
380 | foreach ((array) $needles as $needle) { |
||
381 | if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) { |
||
382 | return true; |
||
383 | } |
||
384 | } |
||
385 | |||
386 | return false; |
||
387 | } |
||
388 | } |
||
389 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.