1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NavJobs\Transmit; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
use Illuminate\Support\Facades\Input; |
8
|
|
|
use NavJobs\Transmit\Traits\ErrorResponsesTrait; |
9
|
|
|
use NavJobs\Transmit\Traits\QueryHelperTrait; |
10
|
|
|
use Illuminate\Routing\Controller as BaseController; |
11
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
12
|
|
|
|
13
|
|
|
abstract class Controller extends BaseController |
14
|
|
|
{ |
15
|
|
|
use QueryHelperTrait, ErrorResponsesTrait; |
16
|
|
|
|
17
|
|
|
protected $statusCode = 200; |
18
|
|
|
protected $fractal; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->fractal = App::make(Fractal::class); |
23
|
|
|
|
24
|
|
|
$this->parseIncludes(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Parses includes from either the header or query string. |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
|
|
protected function parseIncludes() |
33
|
|
|
{ |
34
|
|
|
if (Input::header('include')) { |
35
|
|
|
return $this->fractal->parseIncludes(Input::header('include')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (Input::get('include')) { |
39
|
|
|
return $this->fractal->parseIncludes(Input::get('include')); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the current status code. |
47
|
|
|
* |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
protected function getStatusCode() |
51
|
|
|
{ |
52
|
|
|
return $this->statusCode; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets the current status code. |
57
|
|
|
* |
58
|
|
|
* @param $statusCode |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
protected function setStatusCode($statusCode) |
62
|
|
|
{ |
63
|
|
|
$this->statusCode = $statusCode; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function prepareBuilder($builder) |
69
|
|
|
{ |
70
|
|
|
$model = $builder ?: $this->model; |
|
|
|
|
71
|
|
|
|
72
|
|
|
$includes = $this->transformer->getEagerLoads($this->fractal->getRequestedIncludes()); |
|
|
|
|
73
|
|
|
$includedItems = $this->eagerLoadIncludes($this->model, $includes); |
74
|
|
|
return $this->applyParameters($includedItems, request()->query); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns a json response that contains the specified resource |
79
|
|
|
* passed through fractal and optionally a transformer. |
80
|
|
|
* |
81
|
|
|
* @param $item |
82
|
|
|
* @param null $callback |
83
|
|
|
* @param null $resourceKey |
84
|
|
|
* @return \Illuminate\Http\JsonResponse |
85
|
|
|
*/ |
86
|
|
|
protected function respondWithItem($item, $callback = null, $resourceKey = false) |
87
|
|
|
{ |
88
|
|
|
if($callback) { |
89
|
|
|
$builder = $this->prepareBuilder($item); |
90
|
|
|
$item = $callback($builder); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$rootScope = $this->fractal->item($item, $this->transformer, $resourceKey); |
94
|
|
|
|
95
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns a json response that indicates the resource was successfully created also |
100
|
|
|
* returns the resource passed through fractal and optionally a transformer. |
101
|
|
|
* |
102
|
|
|
* @param $item |
103
|
|
|
* @param null $callback |
104
|
|
|
* @param null $resourceKey |
105
|
|
|
* @return \Illuminate\Http\JsonResponse |
106
|
|
|
*/ |
107
|
|
|
protected function respondWithItemCreated($item, $callback = null, $resourceKey = false) |
108
|
|
|
{ |
109
|
|
|
$this->setStatusCode(201); |
110
|
|
|
$rootScope = $this->fractal->item($item, $callback, $resourceKey); |
111
|
|
|
|
112
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns a json response that contains the specified collection |
117
|
|
|
* passed through fractal and optionally a transformer. |
118
|
|
|
* |
119
|
|
|
* @param $collection |
120
|
|
|
* @param $callback |
121
|
|
|
* @param null $resourceKey |
122
|
|
|
* @return \Illuminate\Http\JsonResponse |
123
|
|
|
*/ |
124
|
|
|
protected function respondWithCollection($collection, $callback, $resourceKey = false) |
125
|
|
|
{ |
126
|
|
|
$rootScope = $this->fractal->collection($collection, $callback, $resourceKey); |
127
|
|
|
|
128
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns a json response that contains the specified paginated collection |
133
|
|
|
* passed through fractal and optionally a transformer. |
134
|
|
|
* |
135
|
|
|
* @param $builder |
136
|
|
|
* @param $callback |
137
|
|
|
* @param int $perPage |
138
|
|
|
* @param null $resourceKey |
139
|
|
|
* @return \Illuminate\Http\JsonResponse |
140
|
|
|
*/ |
141
|
|
|
protected function respondWithPaginatedCollection($builder = null, $perPage = 10, $resourceKey = null) |
142
|
|
|
{ |
143
|
|
|
$builder = $this->prepareBuilder($builder); |
144
|
|
|
|
145
|
|
|
$paginator = $builder->paginate($perPage); |
146
|
|
|
$paginator->appends($this->getQueryParameters()); |
147
|
|
|
|
148
|
|
|
$rootScope = $this->fractal |
149
|
|
|
->collection($paginator->getCollection(), $this->transformer, $resourceKey) |
150
|
|
|
->paginateWith(new IlluminatePaginatorAdapter($paginator)); |
151
|
|
|
|
152
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns an array of Query Parameters, not including pagination. |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
protected function getQueryParameters() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return array_diff_key($_GET, array_flip(['page'])); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns a json response that contains the specified array, |
167
|
|
|
* the current status code and optional headers. |
168
|
|
|
* |
169
|
|
|
* @param array $array |
170
|
|
|
* @param array $headers |
171
|
|
|
* @return \Illuminate\Http\JsonResponse |
172
|
|
|
*/ |
173
|
|
|
protected function respondWithArray(array $array, array $headers = []) |
174
|
|
|
{ |
175
|
|
|
return response()->json($array, $this->statusCode, $headers); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns a response that indicates success but no content returned. |
180
|
|
|
* |
181
|
|
|
* @return \Illuminate\Http\Response |
182
|
|
|
*/ |
183
|
|
|
protected function respondWithNoContent() |
184
|
|
|
{ |
185
|
|
|
return response()->make('', 204); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: