1 | <?php |
||
22 | abstract class AbstractApiController extends Controller |
||
23 | { |
||
24 | /** |
||
25 | * The HTTP response headers. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $headers = []; |
||
30 | |||
31 | /** |
||
32 | * The HTTP response meta data. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $meta = []; |
||
37 | |||
38 | /** |
||
39 | * The HTTP response data. |
||
40 | * |
||
41 | * @var mixed |
||
42 | */ |
||
43 | protected $data = null; |
||
44 | |||
45 | /** |
||
46 | * The HTTP response status code. |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $statusCode = 200; |
||
51 | |||
52 | /** |
||
53 | * Set the response headers. |
||
54 | * |
||
55 | * @param array $headers |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | protected function setHeaders(array $headers) |
||
65 | |||
66 | /** |
||
67 | * Set the response meta data. |
||
68 | * |
||
69 | * @param array $meta |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | protected function setMetaData(array $meta) |
||
79 | |||
80 | /** |
||
81 | * Set the response meta data. |
||
82 | * |
||
83 | * @param array $data |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | protected function setData($data) |
||
93 | |||
94 | /** |
||
95 | * Set the response status code. |
||
96 | * |
||
97 | * @param int $statusCode |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | protected function setStatusCode($statusCode) |
||
107 | |||
108 | /** |
||
109 | * Respond with an item response. |
||
110 | * |
||
111 | * @param mixed |
||
112 | * |
||
113 | * @return \Illuminate\Http\JsonResponse |
||
114 | */ |
||
115 | public function item($item) |
||
119 | |||
120 | /** |
||
121 | * Respond with a collection response. |
||
122 | * |
||
123 | * @param \Illuminate\Support\Collection $collection |
||
124 | * |
||
125 | * @return \Illuminate\Http\JsonResponse |
||
126 | */ |
||
127 | public function collection(Collection $collection) |
||
131 | |||
132 | /** |
||
133 | * Respond with a pagination response. |
||
134 | * |
||
135 | * @param \Illuminate\Pagination\Paginator $paginator |
||
136 | * @param \Illuminate\Http\Request $request |
||
137 | * |
||
138 | * @return \Illuminate\Http\JsonResponse |
||
139 | */ |
||
140 | protected function paginator(Paginator $paginator, Request $request) |
||
141 | { |
||
142 | foreach ($request->query as $key => $value) { |
||
143 | if ($key !== 'page') { |
||
144 | $paginator->addQuery($key, $value); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $pagination = [ |
||
149 | 'pagination' => [ |
||
150 | 'total' => (int) $paginator->total(), |
||
|
|||
151 | 'count' => count($paginator->items()), |
||
152 | 'per_page' => (int) $paginator->perPage(), |
||
153 | 'current_page' => (int) $paginator->currentPage(), |
||
154 | 'total_pages' => (int) $paginator->lastPage(), |
||
155 | 'links' => [ |
||
156 | 'next_page' => $paginator->nextPageUrl(), |
||
157 | 'previous_page' => $paginator->previousPageUrl(), |
||
158 | ], |
||
159 | ], |
||
160 | ]; |
||
161 | |||
162 | $items = $paginator->getCollection(); |
||
163 | |||
164 | if ($sortBy = $request->get('sort')) { |
||
165 | $direction = $request->has('order') && $request->get('order') === 'desc'; |
||
166 | |||
167 | $items = $items->sortBy($sortBy, SORT_REGULAR, $direction); |
||
168 | } |
||
169 | |||
170 | return $this->setMetaData($pagination)->setData(AutoPresenter::decorate($items->values()))->respond(); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Respond with a no content response. |
||
175 | * |
||
176 | * @param string $message |
||
177 | * |
||
178 | * @return \Illuminate\Http\JsonResponse |
||
179 | */ |
||
180 | protected function noContent() |
||
184 | |||
185 | /** |
||
186 | * Build the response. |
||
187 | * |
||
188 | * @return \Illuminate\Http\Response |
||
189 | */ |
||
190 | protected function respond() |
||
204 | } |
||
205 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: