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