1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Fractal; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use League\Fractal\Manager; |
7
|
|
|
use Illuminate\Http\JsonResponse; |
8
|
|
|
use League\Fractal\Serializer\JsonApiSerializer; |
9
|
|
|
use Spatie\Fractalistic\Fractal as Fractalistic; |
10
|
|
|
use League\Fractal\Serializer\SerializerAbstract; |
11
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
12
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
13
|
|
|
|
14
|
|
|
class Fractal extends Fractalistic |
15
|
|
|
{ |
16
|
|
|
/** @param \League\Fractal\Manager $manager */ |
17
|
|
|
public function __construct(Manager $manager) |
18
|
|
|
{ |
19
|
|
|
parent::__construct($manager); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param null|mixed $data |
24
|
|
|
* @param null|callable|\League\Fractal\TransformerAbstract $transformer |
25
|
|
|
* @param null|\League\Fractal\Serializer\SerializerAbstract $serializer |
26
|
|
|
* |
27
|
|
|
* @return \Spatie\Fractalistic\Fractal |
28
|
|
|
*/ |
29
|
|
|
public static function create($data = null, $transformer = null, $serializer = null) |
30
|
|
|
{ |
31
|
|
|
$fractal = parent::create($data, $transformer, $serializer); |
32
|
|
|
|
33
|
|
|
if (empty($serializer)) { |
34
|
|
|
$serializer = config('laravel-fractal.default_serializer'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($data instanceof LengthAwarePaginator) { |
38
|
|
|
$fractal->paginateWith(new IlluminatePaginatorAdapter($data)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (empty($serializer)) { |
42
|
|
|
return $fractal; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($serializer instanceof SerializerAbstract) { |
46
|
|
|
return $fractal->serializeWith($serializer); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($serializer instanceof Closure) { |
50
|
|
|
return $fractal->serializeWith($serializer()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($serializer == JsonApiSerializer::class) { |
54
|
|
|
$baseUrl = config('laravel-fractal.base_url'); |
55
|
|
|
|
56
|
|
|
return $fractal->serializeWith(new $serializer($baseUrl)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $fractal->serializeWith(new $serializer); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return a new JSON response. |
64
|
|
|
* |
65
|
|
|
* @param callable|int $statusCode |
66
|
|
|
* @param callable|array $headers |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Http\JsonResponse |
69
|
|
|
*/ |
70
|
|
|
public function respond($statusCode = 200, $headers = []) |
71
|
|
|
{ |
72
|
|
|
$response = new JsonResponse(); |
73
|
|
|
|
74
|
|
|
$response->setData($this->createData()->toArray()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if (is_int($statusCode)) { |
77
|
|
|
$statusCode = function (JsonResponse $response) use ($statusCode) { |
78
|
|
|
return $response->setStatusCode($statusCode); |
79
|
|
|
}; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (is_array($headers)) { |
83
|
|
|
$headers = function (JsonResponse $response) use ($headers) { |
84
|
|
|
return $response->withHeaders($headers); |
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (is_callable($statusCode)) { |
89
|
|
|
$statusCode($response); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (is_callable($headers)) { |
93
|
|
|
$headers($response); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $response; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.