|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sarala\Http\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
|
8
|
|
|
use Illuminate\Http\JsonResponse; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Routing\Controller; |
|
11
|
|
|
use League\Fractal\Manager; |
|
12
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
|
13
|
|
|
use League\Fractal\Resource\Collection; |
|
14
|
|
|
use League\Fractal\Resource\Item; |
|
15
|
|
|
use Sarala\Query\ApiRequestInspector; |
|
16
|
|
|
|
|
17
|
|
|
class BaseController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
protected $manager; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Manager $manager, Request $request) |
|
22
|
|
|
{ |
|
23
|
|
|
if ($request->filled('include')) { |
|
24
|
|
|
$manager->parseIncludes($request->get('include')); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if ($request->filled('exclude')) { |
|
28
|
|
|
$manager->parseExcludes($request->get('exclude')); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->manager = $manager; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function callAction($method, $parameters) |
|
35
|
|
|
{ |
|
36
|
|
|
if (request()->filled('include')) { |
|
37
|
|
|
(new ApiRequestInspector($parameters))->inspect(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return parent::callAction($method, $parameters); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function responseItem($object, $transformer, $resourceKey, $status = 200): JsonResponse |
|
44
|
|
|
{ |
|
45
|
|
|
$resource = new Item($object, $transformer, $resourceKey); |
|
46
|
|
|
|
|
47
|
|
|
return $this->response($this->manager->createData($resource)->toArray(), $status); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function responseCollection($collection, $transformer, $resourceKey, $status = 200): JsonResponse |
|
51
|
|
|
{ |
|
52
|
|
|
$resource = new Collection($collection, $transformer, $resourceKey); |
|
53
|
|
|
|
|
54
|
|
|
if ($collection instanceof LengthAwarePaginator) { |
|
55
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($collection)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->response($this->manager->createData($resource)->toArray(), $status); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function response($data, $status) |
|
62
|
|
|
{ |
|
63
|
|
|
return response()->json($data, $status); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: