1 | <?php |
||
12 | abstract class DataTablesEditor |
||
13 | { |
||
14 | use ValidatesRequests; |
||
15 | |||
16 | /** |
||
17 | * Allowed dataTables editor actions. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $actions = ['create', 'edit', 'remove']; |
||
22 | |||
23 | /** |
||
24 | * @var \Illuminate\Database\Eloquent\Model |
||
25 | */ |
||
26 | protected $model = null; |
||
27 | |||
28 | /** |
||
29 | * Process dataTables editor action request. |
||
30 | * |
||
31 | * @param Request $request |
||
32 | * @return JsonResponse|mixed |
||
33 | * @throws DataTablesEditorException |
||
34 | */ |
||
35 | public function process(Request $request) |
||
36 | { |
||
37 | $action = $request->get('action'); |
||
38 | |||
39 | if (! in_array($action, $this->actions)) { |
||
40 | throw new DataTablesEditorException('Requested action not supported!'); |
||
41 | } |
||
42 | |||
43 | return $this->{$action}($request); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Process create action request. |
||
48 | * |
||
49 | * @param Request $request |
||
50 | * @return JsonResponse |
||
51 | */ |
||
52 | public function create(Request $request) |
||
53 | { |
||
54 | $instance = $this->resolveModel(); |
||
55 | $connection = $instance->getConnection(); |
||
56 | $affected = []; |
||
57 | $errors = []; |
||
58 | |||
59 | $connection->beginTransaction(); |
||
60 | foreach ($request->get('data') as $data) { |
||
61 | $validator = $this->getValidationFactory()->make($data, $this->createRules(), $this->createMessages()); |
||
62 | if ($validator->fails()) { |
||
63 | foreach ($this->formatErrors($validator) as $error) { |
||
64 | $errors[] = $error; |
||
65 | }; |
||
66 | |||
67 | continue; |
||
68 | } |
||
69 | |||
70 | if (method_exists($this, 'creating')) { |
||
71 | $data = $this->creating($instance, $data); |
||
|
|||
72 | } |
||
73 | |||
74 | $model = $instance->newQuery()->create($data); |
||
75 | $model->setAttribute('DT_RowId', $model->getKey()); |
||
76 | |||
77 | if (method_exists($this, 'created')) { |
||
78 | $this->created($model, $data); |
||
79 | } |
||
80 | |||
81 | $affected[] = $model; |
||
82 | } |
||
83 | |||
84 | if (! $errors) { |
||
85 | $connection->commit(); |
||
86 | } else { |
||
87 | $connection->rollBack(); |
||
88 | } |
||
89 | |||
90 | return $this->toJson($affected, $errors); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Resolve model to used. |
||
95 | * |
||
96 | * @return Model |
||
97 | */ |
||
98 | protected function resolveModel() |
||
99 | { |
||
100 | if ($this->model instanceof Model) { |
||
101 | return $this->model; |
||
102 | } |
||
103 | |||
104 | return new $this->model; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get create action validation rules. |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | abstract public function createRules(); |
||
113 | |||
114 | /** |
||
115 | * Get create validation messages. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function createMessages() |
||
123 | |||
124 | /** |
||
125 | * @param Validator $validator |
||
126 | * @return array |
||
127 | */ |
||
128 | protected function formatErrors(Validator $validator) |
||
141 | |||
142 | /** |
||
143 | * Display success data in dataTables editor format. |
||
144 | * |
||
145 | * @param array $data |
||
146 | * @param array $errors |
||
147 | * @return JsonResponse |
||
148 | */ |
||
149 | protected function toJson(array $data, array $errors = []) |
||
158 | |||
159 | /** |
||
160 | * Process edit action request. |
||
161 | * |
||
162 | * @param Request $request |
||
163 | * @return JsonResponse |
||
164 | */ |
||
165 | public function edit(Request $request) |
||
206 | |||
207 | /** |
||
208 | * Get edit action validation rules. |
||
209 | * |
||
210 | * @param Model $model |
||
211 | * @return array |
||
212 | */ |
||
213 | abstract public function editRules(Model $model); |
||
214 | |||
215 | /** |
||
216 | * Get edit validation messages. |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | protected function editMessages() |
||
224 | |||
225 | /** |
||
226 | * Process remove action request. |
||
227 | * |
||
228 | * @param Request $request |
||
229 | * @return JsonResponse |
||
230 | */ |
||
231 | public function remove(Request $request) |
||
282 | |||
283 | /** |
||
284 | * Get remove action validation rules. |
||
285 | * |
||
286 | * @param Model $model |
||
287 | * @return array |
||
288 | */ |
||
289 | abstract public function removeRules(Model $model); |
||
290 | |||
291 | /** |
||
292 | * Get remove validation messages. |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | protected function removeMessages() |
||
300 | |||
301 | /** |
||
302 | * Get remove query exception message. |
||
303 | * |
||
304 | * @param QueryException $exception |
||
305 | * @param Model $model |
||
306 | * @return string |
||
307 | */ |
||
308 | protected function removeExceptionMessage(QueryException $exception, Model $model) |
||
312 | |||
313 | /** |
||
314 | * Display dataTables editor validation errors. |
||
315 | * |
||
316 | * @param Validator $validator |
||
317 | * @return JsonResponse |
||
318 | */ |
||
319 | protected function displayValidationErrors(Validator $validator) |
||
328 | } |
||
329 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.