1 | <?php |
||
12 | abstract class CRUDController extends JSONController |
||
13 | { |
||
14 | /** |
||
15 | * Make sure that the data of a form is valid, only called when creating a |
||
16 | * new object |
||
17 | * @param Form $form The submitted form |
||
18 | * @return void |
||
19 | */ |
||
20 | 1 | protected function validateNew($form) |
|
23 | |||
24 | /** |
||
25 | * Make sure that the data of a form is valid |
||
26 | * @param Form $form The submitted form |
||
27 | * @return void |
||
28 | */ |
||
29 | protected function validate($form) |
||
32 | |||
33 | /** |
||
34 | * Delete a model |
||
35 | * @param PermissionModel $model The model we want to delete |
||
36 | * @param Player $me The user who wants to delete the model |
||
37 | * @param Closure|null $onSuccess Something to do when the model is deleted |
||
38 | * @throws ForbiddenException |
||
39 | * @return mixed The response to show to the user |
||
40 | */ |
||
41 | 1 | protected function delete(PermissionModel $model, Player $me, $onSuccess = null) |
|
78 | |||
79 | /** |
||
80 | * Create a model |
||
81 | * |
||
82 | * This method requires that you have implemented enter() and a form creator |
||
83 | * for the model |
||
84 | * |
||
85 | * @param Player $me The user who wants to create the model |
||
86 | * @param Closure|null $onSuccess The function to call on success |
||
87 | * @throws ForbiddenException |
||
88 | * @return mixed The response to show to the user |
||
89 | */ |
||
90 | 1 | protected function create(Player $me, $onSuccess = null) |
|
120 | |||
121 | /** |
||
122 | * Edit a model |
||
123 | * |
||
124 | * This method requires that you have implemented update() and a form creator |
||
125 | * for the model |
||
126 | * |
||
127 | * @param PermissionModel $model The model we want to edit |
||
128 | * @param Player $me The user who wants to edit the model |
||
129 | * @param string $type The name of the variable to pass to the view |
||
130 | * @throws ForbiddenException |
||
131 | * @return mixed The response to show to the user |
||
132 | */ |
||
133 | protected function edit(PermissionModel $model, Player $me, $type) |
||
155 | |||
156 | /** |
||
157 | * Find whether a player can delete a model |
||
158 | * |
||
159 | * @param Player $player The player who wants to delete the model |
||
160 | * @param PermissionModel $model The model that will be deleted |
||
161 | * @param bool $hard Whether to hard-delete the model instead of soft-deleting it |
||
162 | * @return bool |
||
163 | */ |
||
164 | 1 | protected function canDelete($player, $model, $hard = false) |
|
168 | |||
169 | /** |
||
170 | * Find whether a player can create a model |
||
171 | * |
||
172 | * @param Player $player The player who wants to create a model |
||
173 | * @return bool |
||
174 | */ |
||
175 | 1 | protected function canCreate($player) |
|
181 | |||
182 | /** |
||
183 | * Find whether a player can edit a model |
||
184 | * |
||
185 | * @param Player $player The player who wants to delete the model |
||
186 | * @param PermissionModel $model The model which will be edited |
||
187 | * @return bool |
||
188 | */ |
||
189 | protected function canEdit($player, $model) |
||
193 | |||
194 | /** |
||
195 | * Get a redirection response to a model |
||
196 | * |
||
197 | * Goes to a list of models of the same type if the provided model does not |
||
198 | * have a URL |
||
199 | * |
||
200 | * @param ModelInterface $model The model to redirect to |
||
201 | * @return Response |
||
202 | */ |
||
203 | 1 | protected function redirectTo($model) |
|
211 | |||
212 | /** |
||
213 | * Get a redirection response to a list of models |
||
214 | * |
||
215 | * @param ModelInterface $model The model to whose list we should redirect |
||
216 | * @return Response |
||
217 | */ |
||
218 | 1 | protected function redirectToList($model) |
|
225 | |||
226 | /** |
||
227 | * Dynamically get the form to show to the user |
||
228 | * |
||
229 | * @param \Model|null $model The model being edited, `null` if we're creating one |
||
230 | * @return ModelFormCreator |
||
231 | */ |
||
232 | 1 | private function getFormCreator($model = null) |
|
242 | |||
243 | /** |
||
244 | * Get a message to show to the user |
||
245 | * @param \ModelInterface|string $model The model (or type) to show a message for |
||
246 | * @param string $action The action that will be performed (softDelete, hardDelete, create or edit) |
||
247 | * @param string $status The message's status (confirm, error or success) |
||
248 | * @return string |
||
249 | */ |
||
250 | 1 | private function getMessage($model, $action, $status, $escape = true) |
|
276 | |||
277 | /** |
||
278 | * Get a list of messages to show to the user |
||
279 | * @param string $type The type of the model that the message refers to |
||
280 | * @param string $name The name of the model |
||
281 | * @return array |
||
282 | */ |
||
283 | 1 | protected function getMessages($type, $name = '') |
|
342 | } |
||
343 |
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: