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