1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Fractal; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use League\Fractal\Manager; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Http\JsonResponse; |
9
|
|
|
use Illuminate\Support\Traits\Macroable; |
10
|
|
|
use League\Fractal\Serializer\JsonApiSerializer; |
11
|
|
|
use Spatie\Fractalistic\Fractal as Fractalistic; |
12
|
|
|
use League\Fractal\Serializer\SerializerAbstract; |
13
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
14
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
15
|
|
|
|
16
|
|
|
class Fractal extends Fractalistic |
17
|
|
|
{ |
18
|
|
|
use Macroable { |
19
|
|
|
Macroable::__call as macroCall; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** @param \League\Fractal\Manager $manager */ |
23
|
|
|
public function __construct(Manager $manager) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($manager); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param null|mixed $data |
30
|
|
|
* @param null|callable|\League\Fractal\TransformerAbstract $transformer |
31
|
|
|
* @param null|\League\Fractal\Serializer\SerializerAbstract $serializer |
32
|
|
|
* |
33
|
|
|
* @return \Spatie\Fractalistic\Fractal |
34
|
|
|
*/ |
35
|
|
|
public static function create($data = null, $transformer = null, $serializer = null) |
36
|
|
|
{ |
37
|
|
|
$fractal = parent::create($data, $transformer, $serializer); |
38
|
|
|
|
39
|
|
|
if (config('fractal.auto_includes.enabled')) { |
40
|
|
|
$requestKey = config('fractal.auto_includes.request_key'); |
41
|
|
|
|
42
|
|
|
if ($include = app('request')->query($requestKey)) { |
43
|
|
|
$fractal->parseIncludes($include); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (empty($serializer)) { |
48
|
|
|
$serializer = config('fractal.default_serializer'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($data instanceof LengthAwarePaginator) { |
52
|
|
|
$paginator = config('fractal.default_paginator'); |
53
|
|
|
|
54
|
|
|
if (empty($paginator)) { |
55
|
|
|
$paginator = IlluminatePaginatorAdapter::class; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$fractal->paginateWith(new $paginator($data)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (empty($serializer)) { |
62
|
|
|
return $fractal; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($serializer instanceof SerializerAbstract) { |
66
|
|
|
return $fractal->serializeWith($serializer); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($serializer instanceof Closure) { |
70
|
|
|
return $fractal->serializeWith($serializer()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($serializer == JsonApiSerializer::class) { |
74
|
|
|
$baseUrl = config('fractal.base_url'); |
75
|
|
|
|
76
|
|
|
return $fractal->serializeWith(new $serializer($baseUrl)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $fractal->serializeWith(new $serializer); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return a new JSON response. |
84
|
|
|
* |
85
|
|
|
* @param callable|int $statusCode |
86
|
|
|
* @param callable|array $headers |
87
|
|
|
* @param callable|int $options |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Http\JsonResponse |
90
|
|
|
*/ |
91
|
|
|
public function respond($statusCode = 200, $headers = [], $options = 0) |
92
|
|
|
{ |
93
|
|
|
$response = new JsonResponse(); |
94
|
|
|
|
95
|
|
|
$response->setData($this->createData()->toArray()); |
|
|
|
|
96
|
|
|
|
97
|
|
|
if (is_int($statusCode)) { |
98
|
|
|
$statusCode = function (JsonResponse $response) use ($statusCode) { |
99
|
|
|
return $response->setStatusCode($statusCode); |
100
|
|
|
}; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (is_array($headers)) { |
104
|
|
|
$headers = function (JsonResponse $response) use ($headers) { |
105
|
|
|
return $response->withHeaders($headers); |
106
|
|
|
}; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (is_int($options)) { |
110
|
|
|
$options = function (JsonResponse $response) use ($options) { |
111
|
|
|
$response->setEncodingOptions($options); |
112
|
|
|
|
113
|
|
|
return $response; |
114
|
|
|
}; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (is_callable($options)) { |
118
|
|
|
$options($response); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (is_callable($statusCode)) { |
122
|
|
|
$statusCode($response); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (is_callable($headers)) { |
126
|
|
|
$headers($response); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $response; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function __call($name, array $arguments) |
133
|
|
|
{ |
134
|
|
|
if (static::hasMacro($name)) { |
135
|
|
|
return $this->macroCall($name, $arguments); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return parent::__call($name, $arguments); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.