1 | <?php |
||
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() |
||
54 | |||
55 | /** |
||
56 | * Sets the current status code. |
||
57 | * |
||
58 | * @param $statusCode |
||
59 | * @return $this |
||
60 | */ |
||
61 | protected function setStatusCode($statusCode) |
||
67 | |||
68 | private function prepareBuilder($builder) |
||
69 | { |
||
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) |
||
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) |
||
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) |
||
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) |
||
154 | |||
155 | /** |
||
156 | * Returns an array of Query Parameters, not including pagination. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function getQueryParameters() |
||
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 = []) |
||
177 | |||
178 | /** |
||
179 | * Returns a response that indicates success but no content returned. |
||
180 | * |
||
181 | * @return \Illuminate\Http\Response |
||
182 | */ |
||
183 | protected function respondWithNoContent() |
||
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: