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() |
||
28 | |||
29 | /** |
||
30 | * Sets the fractal transformer |
||
31 | * |
||
32 | * @return mixed |
||
33 | */ |
||
34 | public function setTransformer($transformer) { |
||
38 | |||
39 | /** |
||
40 | * Sets model builder |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function setModel($model) { |
||
48 | |||
49 | /** |
||
50 | * Sets resource key for fractal |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function setResourceKey($resourceKey) { |
||
58 | |||
59 | /** |
||
60 | * Parses includes from either the header or query string. |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | protected function parseIncludes() |
||
76 | |||
77 | /** |
||
78 | * Returns the current status code. |
||
79 | * |
||
80 | * @return int |
||
81 | */ |
||
82 | protected function getStatusCode() |
||
86 | |||
87 | /** |
||
88 | * Sets the current status code. |
||
89 | * |
||
90 | * @param $statusCode |
||
91 | * @return $this |
||
92 | */ |
||
93 | protected function setStatusCode($statusCode) |
||
99 | |||
100 | private function prepareBuilder($builder) |
||
108 | |||
109 | /** |
||
110 | * Returns a json response that contains the specified resource |
||
111 | * passed through fractal and optionally a transformer. |
||
112 | * |
||
113 | * @param $item |
||
114 | * @param null $callback |
||
115 | * @return \Illuminate\Http\JsonResponse |
||
116 | */ |
||
117 | protected function respondWithItem($item, $callback = null) |
||
128 | |||
129 | /** |
||
130 | * Returns a json response that indicates the resource was successfully created also |
||
131 | * returns the resource passed through fractal and optionally a transformer. |
||
132 | * |
||
133 | * @param $item |
||
134 | * @param null $callback |
||
135 | * @return \Illuminate\Http\JsonResponse |
||
136 | */ |
||
137 | protected function respondWithItemCreated($item, $callback = null) |
||
144 | |||
145 | /** |
||
146 | * Returns a json response that contains the specified collection |
||
147 | * passed through fractal and optionally a transformer. |
||
148 | * |
||
149 | * @param $collection |
||
150 | * @param $callback |
||
151 | * @return \Illuminate\Http\JsonResponse |
||
152 | */ |
||
153 | protected function respondWithCollection($collection, $callback) |
||
159 | |||
160 | /** |
||
161 | * Returns a json response that contains the specified paginated collection |
||
162 | * passed through fractal and optionally a transformer. |
||
163 | * |
||
164 | * @param $builder |
||
165 | * @param $callback |
||
166 | * @param int $perPage |
||
167 | * @return \Illuminate\Http\JsonResponse |
||
168 | */ |
||
169 | protected function respondWithPaginatedCollection($builder = null, $perPage = 10) |
||
182 | |||
183 | /** |
||
184 | * Returns an array of Query Parameters, not including pagination. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function getQueryParameters() |
||
192 | |||
193 | /** |
||
194 | * Returns a json response that contains the specified array, |
||
195 | * the current status code and optional headers. |
||
196 | * |
||
197 | * @param array $array |
||
198 | * @param array $headers |
||
199 | * @return \Illuminate\Http\JsonResponse |
||
200 | */ |
||
201 | protected function respondWithArray(array $array, array $headers = []) |
||
205 | |||
206 | /** |
||
207 | * Returns a response that indicates success but no content returned. |
||
208 | * |
||
209 | * @return \Illuminate\Http\Response |
||
210 | */ |
||
211 | protected function respondWithNoContent() |
||
215 | } |
||
216 |
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: