Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 72 | class Form implements Renderable |
||
| 73 | { |
||
| 74 | use HasHooks; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Remove flag in `has many` form. |
||
| 78 | */ |
||
| 79 | const REMOVE_FLAG_NAME = '_remove_'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Eloquent model of the form. |
||
| 83 | * |
||
| 84 | * @var Model |
||
| 85 | */ |
||
| 86 | protected $model; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var \Illuminate\Validation\Validator |
||
| 90 | */ |
||
| 91 | protected $validator; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var Builder |
||
| 95 | */ |
||
| 96 | protected $builder; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Data for save to current model from input. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $updates = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Data for save to model's relations from input. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $relations = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Input data. |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected $inputs = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Available fields. |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | public static $availableFields = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Form field alias. |
||
| 128 | * |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | public static $fieldAlias = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Ignored saving fields. |
||
| 135 | * |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | protected $ignored = []; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Collected field assets. |
||
| 142 | * |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | protected static $collectedAssets = []; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var Form\Tab |
||
| 149 | */ |
||
| 150 | protected $tab = null; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Field rows in form. |
||
| 154 | * |
||
| 155 | * @var array |
||
| 156 | */ |
||
| 157 | public $rows = []; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var bool |
||
| 161 | */ |
||
| 162 | protected $isSoftDeletes = false; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Initialization closure array. |
||
| 166 | * |
||
| 167 | * @var []Closure |
||
| 168 | */ |
||
| 169 | protected static $initCallbacks; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Create a new form instance. |
||
| 173 | * |
||
| 174 | * @param $model |
||
| 175 | * @param \Closure $callback |
||
| 176 | */ |
||
| 177 | public function __construct($model, Closure $callback = null) |
||
| 178 | { |
||
| 179 | $this->model = $model; |
||
| 180 | |||
| 181 | $this->builder = new Builder($this); |
||
| 182 | |||
| 183 | if ($callback instanceof Closure) { |
||
| 184 | $callback($this); |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->isSoftDeletes = in_array(SoftDeletes::class, class_uses_deep($this->model)); |
||
| 188 | |||
| 189 | $this->callInitCallbacks(); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Initialize with user pre-defined default disables, etc. |
||
| 194 | * |
||
| 195 | * @param Closure $callback |
||
| 196 | */ |
||
| 197 | public static function init(Closure $callback = null) |
||
| 198 | { |
||
| 199 | static::$initCallbacks[] = $callback; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Call the initialization closure array in sequence. |
||
| 204 | */ |
||
| 205 | protected function callInitCallbacks() |
||
| 206 | { |
||
| 207 | if (empty(static::$initCallbacks)) { |
||
| 208 | return; |
||
| 209 | } |
||
| 210 | |||
| 211 | foreach (static::$initCallbacks as $callback) { |
||
| 212 | call_user_func($callback, $this); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Field $field |
||
| 218 | * |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function pushField(Field $field) |
||
| 222 | { |
||
| 223 | $field->setForm($this); |
||
| 224 | |||
| 225 | $this->builder->fields()->push($field); |
||
| 226 | |||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return Model |
||
| 232 | */ |
||
| 233 | public function model() |
||
| 234 | { |
||
| 235 | return $this->model; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return Builder |
||
| 240 | */ |
||
| 241 | public function builder() |
||
| 242 | { |
||
| 243 | return $this->builder; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Generate a edit form. |
||
| 248 | * |
||
| 249 | * @param $id |
||
| 250 | * |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | public function edit($id) |
||
| 254 | { |
||
| 255 | $this->builder->setMode(Builder::MODE_EDIT); |
||
| 256 | $this->builder->setResourceId($id); |
||
| 257 | |||
| 258 | $this->setFieldValue($id); |
||
| 259 | |||
| 260 | return $this; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Use tab to split form. |
||
| 265 | * |
||
| 266 | * @param string $title |
||
| 267 | * @param Closure $content |
||
| 268 | * |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function tab($title, Closure $content, $active = false) |
||
| 272 | { |
||
| 273 | $this->getTab()->append($title, $content, $active); |
||
| 274 | |||
| 275 | return $this; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get Tab instance. |
||
| 280 | * |
||
| 281 | * @return Tab |
||
| 282 | */ |
||
| 283 | public function getTab() |
||
| 284 | { |
||
| 285 | if (is_null($this->tab)) { |
||
| 286 | $this->tab = new Tab($this); |
||
| 287 | } |
||
| 288 | |||
| 289 | return $this->tab; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Destroy data entity and remove files. |
||
| 294 | * |
||
| 295 | * @param $id |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | public function destroy($id) |
||
| 300 | { |
||
| 301 | try { |
||
| 302 | if (($ret = $this->callDeleting()) instanceof Response) { |
||
| 303 | return $ret; |
||
| 304 | } |
||
| 305 | |||
| 306 | collect(explode(',', $id))->filter()->each(function ($id) { |
||
| 307 | $builder = $this->model()->newQuery(); |
||
| 308 | |||
| 309 | if ($this->isSoftDeletes) { |
||
| 310 | $builder = $builder->withTrashed(); |
||
| 311 | } |
||
| 312 | |||
| 313 | $model = $builder->with($this->getRelations())->findOrFail($id); |
||
| 314 | |||
| 315 | if ($this->isSoftDeletes && $model->trashed()) { |
||
| 316 | $this->deleteFiles($model, true); |
||
| 317 | $model->forceDelete(); |
||
| 318 | |||
| 319 | return; |
||
| 320 | } |
||
| 321 | |||
| 322 | $this->deleteFiles($model); |
||
| 323 | $model->delete(); |
||
| 324 | }); |
||
| 325 | |||
| 326 | if (($ret = $this->callDeleted()) instanceof Response) { |
||
| 327 | return $ret; |
||
| 328 | } |
||
| 329 | |||
| 330 | $response = [ |
||
| 331 | 'status' => true, |
||
| 332 | 'message' => trans('admin.delete_succeeded'), |
||
| 333 | ]; |
||
| 334 | } catch (\Exception $exception) { |
||
| 335 | $response = [ |
||
| 336 | 'status' => false, |
||
| 337 | 'message' => $exception->getMessage() ?: trans('admin.delete_failed'), |
||
| 338 | ]; |
||
| 339 | } |
||
| 340 | |||
| 341 | return response()->json($response); |
||
|
|
|||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Remove files in record. |
||
| 346 | * |
||
| 347 | * @param Model $model |
||
| 348 | * @param bool $forceDelete |
||
| 349 | */ |
||
| 350 | protected function deleteFiles(Model $model, $forceDelete = false) |
||
| 351 | { |
||
| 352 | // If it's a soft delete, the files in the data will not be deleted. |
||
| 353 | if (!$forceDelete && $this->isSoftDeletes) { |
||
| 354 | return; |
||
| 355 | } |
||
| 356 | |||
| 357 | $data = $model->toArray(); |
||
| 358 | |||
| 359 | $this->builder->fields()->filter(function ($field) { |
||
| 360 | return $field instanceof Field\File; |
||
| 361 | })->each(function (Field\File $file) use ($data) { |
||
| 362 | $file->setOriginal($data); |
||
| 363 | |||
| 364 | $file->destroy(); |
||
| 365 | }); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Store a new record. |
||
| 370 | * |
||
| 371 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
| 372 | */ |
||
| 373 | public function store() |
||
| 374 | { |
||
| 375 | $data = \request()->all(); |
||
| 376 | |||
| 377 | // Handle validation errors. |
||
| 378 | if ($validationMessages = $this->validationMessages($data)) { |
||
| 379 | return back()->withInput()->withErrors($validationMessages); |
||
| 380 | } |
||
| 381 | |||
| 382 | if (($response = $this->prepare($data)) instanceof Response) { |
||
| 383 | return $response; |
||
| 384 | } |
||
| 385 | |||
| 386 | View Code Duplication | DB::transaction(function () { |
|
| 387 | $inserts = $this->prepareInsert($this->updates); |
||
| 388 | |||
| 389 | foreach ($inserts as $column => $value) { |
||
| 390 | $this->model->setAttribute($column, $value); |
||
| 391 | } |
||
| 392 | |||
| 393 | $this->model->save(); |
||
| 394 | |||
| 395 | $this->updateRelation($this->relations); |
||
| 396 | }); |
||
| 397 | |||
| 398 | if (($response = $this->callSaved()) instanceof Response) { |
||
| 399 | return $response; |
||
| 400 | } |
||
| 401 | |||
| 402 | if ($response = $this->ajaxResponse(trans('admin.save_succeeded'))) { |
||
| 403 | return $response; |
||
| 404 | } |
||
| 405 | |||
| 406 | return $this->redirectAfterStore(); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get ajax response. |
||
| 411 | * |
||
| 412 | * @param string $message |
||
| 413 | * |
||
| 414 | * @return bool|\Illuminate\Http\JsonResponse |
||
| 415 | */ |
||
| 416 | protected function ajaxResponse($message) |
||
| 417 | { |
||
| 418 | $request = Request::capture(); |
||
| 419 | |||
| 420 | // ajax but not pjax |
||
| 421 | if ($request->ajax() && !$request->pjax()) { |
||
| 422 | return response()->json([ |
||
| 423 | 'status' => true, |
||
| 424 | 'message' => $message, |
||
| 425 | ]); |
||
| 426 | } |
||
| 427 | |||
| 428 | return false; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Prepare input data for insert or update. |
||
| 433 | * |
||
| 434 | * @param array $data |
||
| 435 | * |
||
| 436 | * @return mixed |
||
| 437 | */ |
||
| 438 | protected function prepare($data = []) |
||
| 439 | { |
||
| 440 | if (($response = $this->callSubmitted()) instanceof Response) { |
||
| 441 | return $response; |
||
| 442 | } |
||
| 443 | |||
| 444 | $this->inputs = array_merge($this->removeIgnoredFields($data), $this->inputs); |
||
| 445 | |||
| 446 | if (($response = $this->callSaving()) instanceof Response) { |
||
| 447 | return $response; |
||
| 448 | } |
||
| 449 | |||
| 450 | $this->relations = $this->getRelationInputs($this->inputs); |
||
| 451 | |||
| 452 | $this->updates = Arr::except($this->inputs, array_keys($this->relations)); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Remove ignored fields from input. |
||
| 457 | * |
||
| 458 | * @param array $input |
||
| 459 | * |
||
| 460 | * @return array |
||
| 461 | */ |
||
| 462 | protected function removeIgnoredFields($input) |
||
| 463 | { |
||
| 464 | Arr::forget($input, $this->ignored); |
||
| 465 | |||
| 466 | return $input; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get inputs for relations. |
||
| 471 | * |
||
| 472 | * @param array $inputs |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | protected function getRelationInputs($inputs = []) |
||
| 477 | { |
||
| 478 | $relations = []; |
||
| 479 | |||
| 480 | foreach ($inputs as $column => $value) { |
||
| 481 | if (!method_exists($this->model, $column)) { |
||
| 482 | continue; |
||
| 483 | } |
||
| 484 | |||
| 485 | $relation = call_user_func([$this->model, $column]); |
||
| 486 | |||
| 487 | if ($relation instanceof Relations\Relation) { |
||
| 488 | $relations[$column] = $value; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | return $relations; |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Handle update. |
||
| 497 | * |
||
| 498 | * @param int $id |
||
| 499 | * @param null $data |
||
| 500 | * |
||
| 501 | * @return bool|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|mixed|null|Response |
||
| 502 | */ |
||
| 503 | public function update($id, $data = null) |
||
| 504 | { |
||
| 505 | $data = ($data) ?: request()->all(); |
||
| 506 | |||
| 507 | $isEditable = $this->isEditable($data); |
||
| 508 | |||
| 509 | if (($data = $this->handleColumnUpdates($id, $data)) instanceof Response) { |
||
| 510 | return $data; |
||
| 511 | } |
||
| 512 | |||
| 513 | /* @var Model $this->model */ |
||
| 514 | $builder = $this->model(); |
||
| 515 | |||
| 516 | if ($this->isSoftDeletes) { |
||
| 517 | $builder = $builder->withTrashed(); |
||
| 518 | } |
||
| 519 | |||
| 520 | $this->model = $builder->with($this->getRelations())->findOrFail($id); |
||
| 521 | |||
| 522 | $this->setFieldOriginalValue(); |
||
| 523 | |||
| 524 | // Handle validation errors. |
||
| 525 | if ($validationMessages = $this->validationMessages($data)) { |
||
| 526 | if (!$isEditable) { |
||
| 527 | return back()->withInput()->withErrors($validationMessages); |
||
| 528 | } |
||
| 529 | |||
| 530 | return response()->json(['errors' => Arr::dot($validationMessages->getMessages())], 422); |
||
| 531 | } |
||
| 532 | |||
| 533 | if (($response = $this->prepare($data)) instanceof Response) { |
||
| 534 | return $response; |
||
| 535 | } |
||
| 536 | |||
| 537 | View Code Duplication | DB::transaction(function () { |
|
| 538 | $updates = $this->prepareUpdate($this->updates); |
||
| 539 | |||
| 540 | foreach ($updates as $column => $value) { |
||
| 541 | /* @var Model $this->model */ |
||
| 542 | $this->model->setAttribute($column, $value); |
||
| 543 | } |
||
| 544 | |||
| 545 | $this->model->save(); |
||
| 546 | |||
| 547 | $this->updateRelation($this->relations); |
||
| 548 | }); |
||
| 549 | |||
| 550 | if (($result = $this->callSaved()) instanceof Response) { |
||
| 551 | return $result; |
||
| 552 | } |
||
| 553 | |||
| 554 | if ($response = $this->ajaxResponse(trans('admin.update_succeeded'))) { |
||
| 555 | return $response; |
||
| 556 | } |
||
| 557 | |||
| 558 | return $this->redirectAfterUpdate($id); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get RedirectResponse after store. |
||
| 563 | * |
||
| 564 | * @return \Illuminate\Http\RedirectResponse |
||
| 565 | */ |
||
| 566 | protected function redirectAfterStore() |
||
| 567 | { |
||
| 568 | $resourcesPath = $this->resource(0); |
||
| 569 | |||
| 570 | $key = $this->model->getKey(); |
||
| 571 | |||
| 572 | return $this->redirectAfterSaving($resourcesPath, $key); |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Get RedirectResponse after update. |
||
| 577 | * |
||
| 578 | * @param mixed $key |
||
| 579 | * |
||
| 580 | * @return \Illuminate\Http\RedirectResponse |
||
| 581 | */ |
||
| 582 | protected function redirectAfterUpdate($key) |
||
| 583 | { |
||
| 584 | $resourcesPath = $this->resource(-1); |
||
| 585 | |||
| 586 | return $this->redirectAfterSaving($resourcesPath, $key); |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get RedirectResponse after data saving. |
||
| 591 | * |
||
| 592 | * @param string $resourcesPath |
||
| 593 | * @param string $key |
||
| 594 | * |
||
| 595 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 596 | */ |
||
| 597 | protected function redirectAfterSaving($resourcesPath, $key) |
||
| 598 | { |
||
| 599 | if (request('after-save') == 1) { |
||
| 600 | // continue editing |
||
| 601 | $url = rtrim($resourcesPath, '/')."/{$key}/edit"; |
||
| 602 | } elseif (request('after-save') == 2) { |
||
| 603 | // continue creating |
||
| 604 | $url = rtrim($resourcesPath, '/').'/create'; |
||
| 605 | } elseif (request('after-save') == 3) { |
||
| 606 | // view resource |
||
| 607 | $url = rtrim($resourcesPath, '/')."/{$key}"; |
||
| 608 | } else { |
||
| 609 | $url = request(Builder::PREVIOUS_URL_KEY) ?: $resourcesPath; |
||
| 610 | } |
||
| 611 | |||
| 612 | admin_toastr(trans('admin.save_succeeded')); |
||
| 613 | |||
| 614 | return redirect($url); |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Check if request is from editable. |
||
| 619 | * |
||
| 620 | * @param array $input |
||
| 621 | * |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | protected function isEditable(array $input = []) |
||
| 625 | { |
||
| 626 | return array_key_exists('_editable', $input); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Handle updates for single column. |
||
| 631 | * |
||
| 632 | * @param int $id |
||
| 633 | * @param array $data |
||
| 634 | * |
||
| 635 | * @return array|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|Response |
||
| 636 | */ |
||
| 637 | protected function handleColumnUpdates($id, $data) |
||
| 638 | { |
||
| 639 | $data = $this->handleEditable($data); |
||
| 640 | |||
| 641 | $data = $this->handleFileDelete($data); |
||
| 642 | |||
| 643 | $data = $this->handleFileSort($data); |
||
| 644 | |||
| 645 | if ($this->handleOrderable($id, $data)) { |
||
| 646 | return response([ |
||
| 647 | 'status' => true, |
||
| 648 | 'message' => trans('admin.update_succeeded'), |
||
| 649 | ]); |
||
| 650 | } |
||
| 651 | |||
| 652 | return $data; |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Handle editable update. |
||
| 657 | * |
||
| 658 | * @param array $input |
||
| 659 | * |
||
| 660 | * @return array |
||
| 661 | */ |
||
| 662 | protected function handleEditable(array $input = []) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param array $input |
||
| 677 | * |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | protected function handleFileDelete(array $input = []) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @param array $input |
||
| 694 | * |
||
| 695 | * @return array |
||
| 696 | */ |
||
| 697 | protected function handleFileSort(array $input = []) |
||
| 698 | { |
||
| 699 | if (!array_key_exists(Field::FILE_SORT_FLAG, $input)) { |
||
| 700 | return $input; |
||
| 701 | } |
||
| 702 | |||
| 703 | $sorts = array_filter($input[Field::FILE_SORT_FLAG]); |
||
| 704 | |||
| 705 | if (empty($sorts)) { |
||
| 706 | return $input; |
||
| 707 | } |
||
| 708 | |||
| 709 | foreach ($sorts as $column => $order) { |
||
| 710 | $input[$column] = $order; |
||
| 711 | } |
||
| 712 | |||
| 713 | request()->replace($input); |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Handle orderable update. |
||
| 720 | * |
||
| 721 | * @param int $id |
||
| 722 | * @param array $input |
||
| 723 | * |
||
| 724 | * @return bool |
||
| 725 | */ |
||
| 726 | protected function handleOrderable($id, array $input = []) |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Update relation data. |
||
| 743 | * |
||
| 744 | * @param array $relationsData |
||
| 745 | * |
||
| 746 | * @return void |
||
| 747 | */ |
||
| 748 | protected function updateRelation($relationsData) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Prepare input data for update. |
||
| 858 | * |
||
| 859 | * @param array $updates |
||
| 860 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
| 861 | * |
||
| 862 | * @return array |
||
| 863 | */ |
||
| 864 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
| 865 | { |
||
| 866 | $prepared = []; |
||
| 867 | |||
| 868 | /** @var Field $field */ |
||
| 869 | foreach ($this->builder->fields() as $field) { |
||
| 870 | $columns = $field->column(); |
||
| 871 | |||
| 872 | // If column not in input array data, then continue. |
||
| 873 | if (!Arr::has($updates, $columns)) { |
||
| 874 | continue; |
||
| 875 | } |
||
| 876 | |||
| 877 | if ($this->isInvalidColumn($columns, $oneToOneRelation || $field->isJsonType)) { |
||
| 878 | continue; |
||
| 879 | } |
||
| 880 | |||
| 881 | $value = $this->getDataByColumn($updates, $columns); |
||
| 882 | |||
| 883 | $value = $field->prepare($value); |
||
| 884 | |||
| 885 | View Code Duplication | if (is_array($columns)) { |
|
| 886 | foreach ($columns as $name => $column) { |
||
| 887 | Arr::set($prepared, $column, $value[$name]); |
||
| 888 | } |
||
| 889 | } elseif (is_string($columns)) { |
||
| 890 | Arr::set($prepared, $columns, $value); |
||
| 891 | } |
||
| 892 | } |
||
| 893 | |||
| 894 | return $prepared; |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * @param string|array $columns |
||
| 899 | * @param bool $containsDot |
||
| 900 | * |
||
| 901 | * @return bool |
||
| 902 | */ |
||
| 903 | protected function isInvalidColumn($columns, $containsDot = false) |
||
| 904 | { |
||
| 905 | foreach ((array) $columns as $column) { |
||
| 906 | if ((!$containsDot && Str::contains($column, '.')) || |
||
| 907 | ($containsDot && !Str::contains($column, '.'))) { |
||
| 908 | return true; |
||
| 909 | } |
||
| 910 | } |
||
| 911 | |||
| 912 | return false; |
||
| 913 | } |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Prepare input data for insert. |
||
| 917 | * |
||
| 918 | * @param $inserts |
||
| 919 | * |
||
| 920 | * @return array |
||
| 921 | */ |
||
| 922 | protected function prepareInsert($inserts) |
||
| 945 | |||
| 946 | /** |
||
| 947 | * Is input data is has-one relation. |
||
| 948 | * |
||
| 949 | * @param array $inserts |
||
| 950 | * |
||
| 951 | * @return bool |
||
| 952 | */ |
||
| 953 | protected function isHasOneRelation($inserts) |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Ignore fields to save. |
||
| 970 | * |
||
| 971 | * @param string|array $fields |
||
| 972 | * |
||
| 973 | * @return $this |
||
| 974 | */ |
||
| 975 | public function ignore($fields) |
||
| 981 | |||
| 982 | /** |
||
| 983 | * @param array $data |
||
| 984 | * @param string|array $columns |
||
| 985 | * |
||
| 986 | * @return array|mixed |
||
| 987 | */ |
||
| 988 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Find field object by column. |
||
| 1009 | * |
||
| 1010 | * @param $column |
||
| 1011 | * |
||
| 1012 | * @return mixed |
||
| 1013 | */ |
||
| 1014 | protected function getFieldByColumn($column) |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Set original data for each field. |
||
| 1029 | * |
||
| 1030 | * @return void |
||
| 1031 | */ |
||
| 1032 | protected function setFieldOriginalValue() |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Set all fields value in form. |
||
| 1045 | * |
||
| 1046 | * @param $id |
||
| 1047 | * |
||
| 1048 | * @return void |
||
| 1049 | */ |
||
| 1050 | protected function setFieldValue($id) |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Don't snake case attributes. |
||
| 1077 | * |
||
| 1078 | * @param Model $model |
||
| 1079 | * |
||
| 1080 | * @return void |
||
| 1081 | */ |
||
| 1082 | protected static function doNotSnakeAttributes(Model $model) |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Get validation messages. |
||
| 1091 | * |
||
| 1092 | * @param array $input |
||
| 1093 | * |
||
| 1094 | * @return MessageBag|bool |
||
| 1095 | */ |
||
| 1096 | public function validationMessages($input) |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Merge validation messages from input validators. |
||
| 1118 | * |
||
| 1119 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 1120 | * |
||
| 1121 | * @return MessageBag |
||
| 1122 | */ |
||
| 1123 | protected function mergeValidationMessages($validators) |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Get all relations of model from callable. |
||
| 1136 | * |
||
| 1137 | * @return array |
||
| 1138 | */ |
||
| 1139 | public function getRelations() |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Set action for form. |
||
| 1169 | * |
||
| 1170 | * @param string $action |
||
| 1171 | * |
||
| 1172 | * @return $this |
||
| 1173 | */ |
||
| 1174 | public function setAction($action) |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Set field and label width in current form. |
||
| 1183 | * |
||
| 1184 | * @param int $fieldWidth |
||
| 1185 | * @param int $labelWidth |
||
| 1186 | * |
||
| 1187 | * @return $this |
||
| 1188 | */ |
||
| 1189 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Set view for form. |
||
| 1203 | * |
||
| 1204 | * @param string $view |
||
| 1205 | * |
||
| 1206 | * @return $this |
||
| 1207 | */ |
||
| 1208 | public function setView($view) |
||
| 1214 | |||
| 1215 | /** |
||
| 1216 | * Set title for form. |
||
| 1217 | * |
||
| 1218 | * @param string $title |
||
| 1219 | * |
||
| 1220 | * @return $this |
||
| 1221 | */ |
||
| 1222 | public function setTitle($title = '') |
||
| 1228 | |||
| 1229 | /** |
||
| 1230 | * Add a row in form. |
||
| 1231 | * |
||
| 1232 | * @param Closure $callback |
||
| 1233 | * |
||
| 1234 | * @return $this |
||
| 1235 | */ |
||
| 1236 | public function row(Closure $callback) |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Tools setting for form. |
||
| 1245 | * |
||
| 1246 | * @param Closure $callback |
||
| 1247 | */ |
||
| 1248 | public function tools(Closure $callback) |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * @param Closure|null $callback |
||
| 1255 | * |
||
| 1256 | * @return Form\Tools |
||
| 1257 | */ |
||
| 1258 | public function header(Closure $callback = null) |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Disable form submit. |
||
| 1269 | * |
||
| 1270 | * @param bool $disable |
||
| 1271 | * |
||
| 1272 | * @return $this |
||
| 1273 | * |
||
| 1274 | * @deprecated |
||
| 1275 | */ |
||
| 1276 | public function disableSubmit(bool $disable = true) |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Disable form reset. |
||
| 1285 | * |
||
| 1286 | * @param bool $disable |
||
| 1287 | * |
||
| 1288 | * @return $this |
||
| 1289 | * |
||
| 1290 | * @deprecated |
||
| 1291 | */ |
||
| 1292 | public function disableReset(bool $disable = true) |
||
| 1298 | |||
| 1299 | /** |
||
| 1300 | * Disable View Checkbox on footer. |
||
| 1301 | * |
||
| 1302 | * @param bool $disable |
||
| 1303 | * |
||
| 1304 | * @return $this |
||
| 1305 | */ |
||
| 1306 | public function disableViewCheck(bool $disable = true) |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Disable Editing Checkbox on footer. |
||
| 1315 | * |
||
| 1316 | * @param bool $disable |
||
| 1317 | * |
||
| 1318 | * @return $this |
||
| 1319 | */ |
||
| 1320 | public function disableEditingCheck(bool $disable = true) |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * Disable Creating Checkbox on footer. |
||
| 1329 | * |
||
| 1330 | * @param bool $disable |
||
| 1331 | * |
||
| 1332 | * @return $this |
||
| 1333 | */ |
||
| 1334 | public function disableCreatingCheck(bool $disable = true) |
||
| 1340 | |||
| 1341 | /** |
||
| 1342 | * Footer setting for form. |
||
| 1343 | * |
||
| 1344 | * @param Closure $callback |
||
| 1345 | */ |
||
| 1346 | public function footer(Closure $callback = null) |
||
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Get current resource route url. |
||
| 1357 | * |
||
| 1358 | * @param int $slice |
||
| 1359 | * |
||
| 1360 | * @return string |
||
| 1361 | */ |
||
| 1362 | public function resource($slice = -2) |
||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Render the form contents. |
||
| 1375 | * |
||
| 1376 | * @return string |
||
| 1377 | */ |
||
| 1378 | public function render() |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Get or set input data. |
||
| 1389 | * |
||
| 1390 | * @param string $key |
||
| 1391 | * @param null $value |
||
| 1392 | * |
||
| 1393 | * @return array|mixed |
||
| 1394 | */ |
||
| 1395 | public function input($key, $value = null) |
||
| 1403 | |||
| 1404 | /** |
||
| 1405 | * Register builtin fields. |
||
| 1406 | * |
||
| 1407 | * @return void |
||
| 1408 | */ |
||
| 1409 | public static function registerBuiltinFields() |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * Register custom field. |
||
| 1467 | * |
||
| 1468 | * @param string $abstract |
||
| 1469 | * @param string $class |
||
| 1470 | * |
||
| 1471 | * @return void |
||
| 1472 | */ |
||
| 1473 | public static function extend($abstract, $class) |
||
| 1477 | |||
| 1478 | /** |
||
| 1479 | * Set form field alias. |
||
| 1480 | * |
||
| 1481 | * @param string $field |
||
| 1482 | * @param string $alias |
||
| 1483 | * |
||
| 1484 | * @return void |
||
| 1485 | */ |
||
| 1486 | public static function alias($field, $alias) |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Remove registered field. |
||
| 1493 | * |
||
| 1494 | * @param array|string $abstract |
||
| 1495 | */ |
||
| 1496 | public static function forget($abstract) |
||
| 1500 | |||
| 1501 | /** |
||
| 1502 | * Find field class. |
||
| 1503 | * |
||
| 1504 | * @param string $method |
||
| 1505 | * |
||
| 1506 | * @return bool|mixed |
||
| 1507 | */ |
||
| 1508 | public static function findFieldClass($method) |
||
| 1523 | |||
| 1524 | /** |
||
| 1525 | * Collect assets required by registered field. |
||
| 1526 | * |
||
| 1527 | * @return array |
||
| 1528 | */ |
||
| 1529 | public static function collectFieldAssets() |
||
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Getter. |
||
| 1557 | * |
||
| 1558 | * @param string $name |
||
| 1559 | * |
||
| 1560 | * @return array|mixed |
||
| 1561 | */ |
||
| 1562 | public function __get($name) |
||
| 1566 | |||
| 1567 | /** |
||
| 1568 | * Setter. |
||
| 1569 | * |
||
| 1570 | * @param string $name |
||
| 1571 | * @param mixed $value |
||
| 1572 | * |
||
| 1573 | * @return array |
||
| 1574 | */ |
||
| 1575 | public function __set($name, $value) |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * Generate a Field object and add to form builder if Field exists. |
||
| 1582 | * |
||
| 1583 | * @param string $method |
||
| 1584 | * @param array $arguments |
||
| 1585 | * |
||
| 1586 | * @return Field |
||
| 1587 | */ |
||
| 1588 | public function __call($method, $arguments) |
||
| 1604 | } |
||
| 1605 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: