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 |
||
| 76 | class Form implements Renderable |
||
| 77 | { |
||
| 78 | use HasHooks, ShouldSnakeAttributes; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Remove flag in `has many` form. |
||
| 82 | */ |
||
| 83 | const REMOVE_FLAG_NAME = '_remove_'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Eloquent model of the form. |
||
| 87 | * |
||
| 88 | * @var Model |
||
| 89 | */ |
||
| 90 | protected $model; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var \Illuminate\Validation\Validator |
||
| 94 | */ |
||
| 95 | protected $validator; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var Builder |
||
| 99 | */ |
||
| 100 | protected $builder; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Data for save to current model from input. |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $updates = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Data for save to model's relations from input. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | protected $relations = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Input data. |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $inputs = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var Layout |
||
| 125 | */ |
||
| 126 | protected $layout; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Available fields. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | public static $availableFields = [ |
||
| 134 | 'button' => Field\Button::class, |
||
| 135 | 'checkbox' => Field\Checkbox::class, |
||
| 136 | 'color' => Field\Color::class, |
||
| 137 | 'currency' => Field\Currency::class, |
||
| 138 | 'date' => Field\Date::class, |
||
| 139 | 'dateRange' => Field\DateRange::class, |
||
| 140 | 'datetime' => Field\Datetime::class, |
||
| 141 | 'dateTimeRange' => Field\DatetimeRange::class, |
||
| 142 | 'datetimeRange' => Field\DatetimeRange::class, |
||
| 143 | 'decimal' => Field\Decimal::class, |
||
| 144 | 'display' => Field\Display::class, |
||
| 145 | 'divider' => Field\Divider::class, |
||
| 146 | 'embeds' => Field\Embeds::class, |
||
| 147 | 'email' => Field\Email::class, |
||
| 148 | 'file' => Field\File::class, |
||
| 149 | 'hasMany' => Field\HasMany::class, |
||
| 150 | 'hidden' => Field\Hidden::class, |
||
| 151 | 'id' => Field\Id::class, |
||
| 152 | 'image' => Field\Image::class, |
||
| 153 | 'ip' => Field\Ip::class, |
||
| 154 | 'mobile' => Field\Mobile::class, |
||
| 155 | 'month' => Field\Month::class, |
||
| 156 | 'multipleSelect' => Field\MultipleSelect::class, |
||
| 157 | 'number' => Field\Number::class, |
||
| 158 | 'password' => Field\Password::class, |
||
| 159 | 'radio' => Field\Radio::class, |
||
| 160 | 'rate' => Field\Rate::class, |
||
| 161 | 'select' => Field\Select::class, |
||
| 162 | 'slider' => Field\Slider::class, |
||
| 163 | 'switch' => Field\SwitchField::class, |
||
| 164 | 'text' => Field\Text::class, |
||
| 165 | 'textarea' => Field\Textarea::class, |
||
| 166 | 'time' => Field\Time::class, |
||
| 167 | 'timeRange' => Field\TimeRange::class, |
||
| 168 | 'url' => Field\Url::class, |
||
| 169 | 'year' => Field\Year::class, |
||
| 170 | 'html' => Field\Html::class, |
||
| 171 | 'tags' => Field\Tags::class, |
||
| 172 | 'icon' => Field\Icon::class, |
||
| 173 | 'multipleFile' => Field\MultipleFile::class, |
||
| 174 | 'multipleImage' => Field\MultipleImage::class, |
||
| 175 | 'captcha' => Field\Captcha::class, |
||
| 176 | 'listbox' => Field\Listbox::class, |
||
| 177 | 'table' => Field\Table::class, |
||
| 178 | 'timezone' => Field\Timezone::class, |
||
| 179 | 'keyValue' => Field\KeyValue::class, |
||
| 180 | 'list' => Field\ListField::class, |
||
| 181 | ]; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Form field alias. |
||
| 185 | * |
||
| 186 | * @var array |
||
| 187 | */ |
||
| 188 | public static $fieldAlias = []; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Ignored saving fields. |
||
| 192 | * |
||
| 193 | * @var array |
||
| 194 | */ |
||
| 195 | protected $ignored = []; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Collected field assets. |
||
| 199 | * |
||
| 200 | * @var array |
||
| 201 | */ |
||
| 202 | protected static $collectedAssets = []; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var Form\Tab |
||
| 206 | */ |
||
| 207 | protected $tab = null; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Field rows in form. |
||
| 211 | * |
||
| 212 | * @var array |
||
| 213 | */ |
||
| 214 | public $rows = []; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @var bool |
||
| 218 | */ |
||
| 219 | protected $isSoftDeletes = false; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Initialization closure array. |
||
| 223 | * |
||
| 224 | * @var []Closure |
||
| 225 | */ |
||
| 226 | protected static $initCallbacks; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Create a new form instance. |
||
| 230 | * |
||
| 231 | * @param $model |
||
| 232 | * @param \Closure $callback |
||
| 233 | */ |
||
| 234 | public function __construct($model, Closure $callback = null) |
||
| 235 | { |
||
| 236 | $this->model = $model; |
||
| 237 | |||
| 238 | $this->builder = new Builder($this); |
||
| 239 | |||
| 240 | $this->initLayout(); |
||
| 241 | |||
| 242 | if ($callback instanceof Closure) { |
||
| 243 | $callback($this); |
||
| 244 | } |
||
| 245 | |||
| 246 | $this->isSoftDeletes = in_array(SoftDeletes::class, class_uses_deep($this->model), true); |
||
| 247 | |||
| 248 | $this->callInitCallbacks(); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Initialize with user pre-defined default disables, etc. |
||
| 253 | * |
||
| 254 | * @param Closure $callback |
||
| 255 | */ |
||
| 256 | public static function init(Closure $callback = null) |
||
| 257 | { |
||
| 258 | static::$initCallbacks[] = $callback; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Call the initialization closure array in sequence. |
||
| 263 | */ |
||
| 264 | protected function callInitCallbacks() |
||
| 265 | { |
||
| 266 | if (empty(static::$initCallbacks)) { |
||
| 267 | return; |
||
| 268 | } |
||
| 269 | |||
| 270 | foreach (static::$initCallbacks as $callback) { |
||
| 271 | $callback($this); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param Field $field |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function pushField(Field $field): self |
||
| 281 | { |
||
| 282 | $field->setForm($this); |
||
| 283 | |||
| 284 | $width = $this->builder->getWidth(); |
||
| 285 | $field->setWidth($width['field'], $width['label']); |
||
| 286 | |||
| 287 | $this->builder->fields()->push($field); |
||
| 288 | $this->layout->addField($field); |
||
| 289 | |||
| 290 | return $this; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return Model |
||
| 295 | */ |
||
| 296 | public function model(): Model |
||
| 297 | { |
||
| 298 | return $this->model; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return Builder |
||
| 303 | */ |
||
| 304 | public function builder(): Builder |
||
| 305 | { |
||
| 306 | return $this->builder; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Generate a edit form. |
||
| 311 | * |
||
| 312 | * @param $id |
||
| 313 | * |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function edit($id): self |
||
| 317 | { |
||
| 318 | $this->builder->setMode(Builder::MODE_EDIT); |
||
| 319 | $this->builder->setResourceId($id); |
||
| 320 | |||
| 321 | $this->setFieldValue($id); |
||
| 322 | |||
| 323 | return $this; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Use tab to split form. |
||
| 328 | * |
||
| 329 | * @param string $title |
||
| 330 | * @param Closure $content |
||
| 331 | * @param bool $active |
||
| 332 | * |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function tab($title, Closure $content, bool $active = false): self |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get Tab instance. |
||
| 344 | * |
||
| 345 | * @return Tab |
||
| 346 | */ |
||
| 347 | public function getTab() |
||
| 348 | { |
||
| 349 | return $this->tab; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Set Tab instance. |
||
| 354 | * |
||
| 355 | * @return Tab |
||
| 356 | */ |
||
| 357 | public function setTab(): Tab |
||
| 358 | { |
||
| 359 | if ($this->tab === null) { |
||
| 360 | $this->tab = new Tab($this); |
||
| 361 | } |
||
| 362 | |||
| 363 | return $this->tab; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Destroy data entity and remove files. |
||
| 368 | * |
||
| 369 | * @param $id |
||
| 370 | * |
||
| 371 | * @return mixed |
||
| 372 | */ |
||
| 373 | public function destroy($id) |
||
| 374 | { |
||
| 375 | try { |
||
| 376 | if (($ret = $this->callDeleting($id)) instanceof Response) { |
||
| 377 | return $ret; |
||
| 378 | } |
||
| 379 | |||
| 380 | collect(explode(',', $id))->filter()->each(function ($id) { |
||
| 381 | $builder = $this->model()->newQuery(); |
||
| 382 | |||
| 383 | if ($this->isSoftDeletes) { |
||
| 384 | $builder = $builder->withTrashed(); |
||
| 385 | } |
||
| 386 | |||
| 387 | $model = $builder->with($this->getRelations())->findOrFail($id); |
||
| 388 | |||
| 389 | if ($this->isSoftDeletes && $model->trashed()) { |
||
| 390 | $this->deleteFiles($model, true); |
||
| 391 | $model->forceDelete(); |
||
| 392 | |||
| 393 | return; |
||
| 394 | } |
||
| 395 | |||
| 396 | $this->deleteFiles($model); |
||
| 397 | $model->delete(); |
||
| 398 | }); |
||
| 399 | |||
| 400 | if (($ret = $this->callDeleted()) instanceof Response) { |
||
| 401 | return $ret; |
||
| 402 | } |
||
| 403 | |||
| 404 | $response = [ |
||
| 405 | 'status' => true, |
||
| 406 | 'message' => trans('admin.delete_succeeded'), |
||
| 407 | ]; |
||
| 408 | } catch (\Exception $exception) { |
||
| 409 | $response = [ |
||
| 410 | 'status' => false, |
||
| 411 | 'message' => $exception->getMessage() ?: trans('admin.delete_failed'), |
||
| 412 | ]; |
||
| 413 | } |
||
| 414 | |||
| 415 | return response()->json($response); |
||
|
|
|||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Remove files in record. |
||
| 420 | * |
||
| 421 | * @param Model $model |
||
| 422 | * @param bool $forceDelete |
||
| 423 | */ |
||
| 424 | protected function deleteFiles(Model $model, $forceDelete = false) |
||
| 425 | { |
||
| 426 | // If it's a soft delete, the files in the data will not be deleted. |
||
| 427 | if (!$forceDelete && $this->isSoftDeletes) { |
||
| 428 | return; |
||
| 429 | } |
||
| 430 | |||
| 431 | $data = $model->toArray(); |
||
| 432 | |||
| 433 | $this->builder->fields()->filter(function ($field) { |
||
| 434 | return $field instanceof Field\File; |
||
| 435 | })->each(function (Field\File $file) use ($data) { |
||
| 436 | $file->setOriginal($data); |
||
| 437 | |||
| 438 | $file->destroy(); |
||
| 439 | }); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Store a new record. |
||
| 444 | * |
||
| 445 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
| 446 | */ |
||
| 447 | public function store() |
||
| 448 | { |
||
| 449 | $data = \request()->all(); |
||
| 450 | |||
| 451 | // Handle validation errors. |
||
| 452 | if ($validationMessages = $this->validationMessages($data)) { |
||
| 453 | return $this->responseValidationError($validationMessages); |
||
| 454 | } |
||
| 455 | |||
| 456 | if (($response = $this->prepare($data)) instanceof Response) { |
||
| 457 | return $response; |
||
| 458 | } |
||
| 459 | |||
| 460 | View Code Duplication | DB::transaction(function () { |
|
| 461 | $inserts = $this->prepareInsert($this->updates); |
||
| 462 | |||
| 463 | foreach ($inserts as $column => $value) { |
||
| 464 | $this->model->setAttribute($column, $value); |
||
| 465 | } |
||
| 466 | |||
| 467 | $this->model->save(); |
||
| 468 | |||
| 469 | $this->updateRelation($this->relations); |
||
| 470 | }); |
||
| 471 | |||
| 472 | if (($response = $this->callSaved()) instanceof Response) { |
||
| 473 | return $response; |
||
| 474 | } |
||
| 475 | |||
| 476 | if ($response = $this->ajaxResponse(trans('admin.save_succeeded'))) { |
||
| 477 | return $response; |
||
| 478 | } |
||
| 479 | |||
| 480 | return $this->redirectAfterStore(); |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param MessageBag $message |
||
| 485 | * |
||
| 486 | * @return $this|\Illuminate\Http\JsonResponse |
||
| 487 | */ |
||
| 488 | protected function responseValidationError(MessageBag $message) |
||
| 489 | { |
||
| 490 | if (\request()->ajax() && !\request()->pjax()) { |
||
| 491 | return response()->json([ |
||
| 492 | 'status' => false, |
||
| 493 | 'validation' => $message, |
||
| 494 | 'message' => $message->first(), |
||
| 495 | ]); |
||
| 496 | } |
||
| 497 | |||
| 498 | return back()->withInput()->withErrors($message); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Get ajax response. |
||
| 503 | * |
||
| 504 | * @param string $message |
||
| 505 | * |
||
| 506 | * @return bool|\Illuminate\Http\JsonResponse |
||
| 507 | */ |
||
| 508 | protected function ajaxResponse($message) |
||
| 509 | { |
||
| 510 | $request = Request::capture(); |
||
| 511 | |||
| 512 | // ajax but not pjax |
||
| 513 | if ($request->ajax() && !$request->pjax()) { |
||
| 514 | return response()->json([ |
||
| 515 | 'status' => true, |
||
| 516 | 'message' => $message, |
||
| 517 | ]); |
||
| 518 | } |
||
| 519 | |||
| 520 | return false; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Prepare input data for insert or update. |
||
| 525 | * |
||
| 526 | * @param array $data |
||
| 527 | * |
||
| 528 | * @return mixed |
||
| 529 | */ |
||
| 530 | protected function prepare($data = []) |
||
| 531 | { |
||
| 532 | if (($response = $this->callSubmitted()) instanceof Response) { |
||
| 533 | return $response; |
||
| 534 | } |
||
| 535 | |||
| 536 | $this->inputs = array_merge($this->removeIgnoredFields($data), $this->inputs); |
||
| 537 | |||
| 538 | if (($response = $this->callSaving()) instanceof Response) { |
||
| 539 | return $response; |
||
| 540 | } |
||
| 541 | |||
| 542 | $this->relations = $this->getRelationInputs($this->inputs); |
||
| 543 | |||
| 544 | $this->updates = Arr::except($this->inputs, array_keys($this->relations)); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Remove ignored fields from input. |
||
| 549 | * |
||
| 550 | * @param array $input |
||
| 551 | * |
||
| 552 | * @return array |
||
| 553 | */ |
||
| 554 | protected function removeIgnoredFields($input): array |
||
| 555 | { |
||
| 556 | Arr::forget($input, $this->ignored); |
||
| 557 | |||
| 558 | return $input; |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get inputs for relations. |
||
| 563 | * |
||
| 564 | * @param array $inputs |
||
| 565 | * |
||
| 566 | * @return array |
||
| 567 | */ |
||
| 568 | protected function getRelationInputs($inputs = []): array |
||
| 569 | { |
||
| 570 | $relations = []; |
||
| 571 | |||
| 572 | foreach ($inputs as $column => $value) { |
||
| 573 | if (method_exists($this->model, $column) || |
||
| 574 | method_exists($this->model, $column = Str::camel($column))) { |
||
| 575 | $relation = call_user_func([$this->model, $column]); |
||
| 576 | |||
| 577 | if ($relation instanceof Relations\Relation) { |
||
| 578 | $relations[$column] = $value; |
||
| 579 | } |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | return $relations; |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Handle update. |
||
| 588 | * |
||
| 589 | * @param int $id |
||
| 590 | * @param null $data |
||
| 591 | * |
||
| 592 | * @return bool|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|mixed|null|Response |
||
| 593 | */ |
||
| 594 | public function update($id, $data = null) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Get RedirectResponse after store. |
||
| 654 | * |
||
| 655 | * @return \Illuminate\Http\RedirectResponse |
||
| 656 | */ |
||
| 657 | protected function redirectAfterStore() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get RedirectResponse after update. |
||
| 668 | * |
||
| 669 | * @param mixed $key |
||
| 670 | * |
||
| 671 | * @return \Illuminate\Http\RedirectResponse |
||
| 672 | */ |
||
| 673 | protected function redirectAfterUpdate($key) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Get RedirectResponse after data saving. |
||
| 682 | * |
||
| 683 | * @param string $resourcesPath |
||
| 684 | * @param string $key |
||
| 685 | * |
||
| 686 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 687 | */ |
||
| 688 | protected function redirectAfterSaving($resourcesPath, $key) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Check if request is from editable. |
||
| 710 | * |
||
| 711 | * @param array $input |
||
| 712 | * |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | protected function isEditable(array $input = []): bool |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Handle updates for single column. |
||
| 722 | * |
||
| 723 | * @param int $id |
||
| 724 | * @param array $data |
||
| 725 | * |
||
| 726 | * @return array|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|Response |
||
| 727 | */ |
||
| 728 | protected function handleColumnUpdates($id, $data) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Handle editable update. |
||
| 748 | * |
||
| 749 | * @param array $input |
||
| 750 | * |
||
| 751 | * @return array |
||
| 752 | */ |
||
| 753 | protected function handleEditable(array $input = []): array |
||
| 765 | |||
| 766 | /** |
||
| 767 | * @param array $input |
||
| 768 | * |
||
| 769 | * @return array |
||
| 770 | */ |
||
| 771 | protected function handleFileDelete(array $input = []): array |
||
| 782 | |||
| 783 | /** |
||
| 784 | * @param array $input |
||
| 785 | * |
||
| 786 | * @return array |
||
| 787 | */ |
||
| 788 | protected function handleFileSort(array $input = []): array |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Handle orderable update. |
||
| 811 | * |
||
| 812 | * @param int $id |
||
| 813 | * @param array $input |
||
| 814 | * |
||
| 815 | * @return bool |
||
| 816 | */ |
||
| 817 | protected function handleOrderable($id, array $input = []) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Update relation data. |
||
| 834 | * |
||
| 835 | * @param array $relationsData |
||
| 836 | * |
||
| 837 | * @return void |
||
| 838 | */ |
||
| 839 | protected function updateRelation($relationsData) |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Prepare input data for update. |
||
| 949 | * |
||
| 950 | * @param array $updates |
||
| 951 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
| 952 | * |
||
| 953 | * @return array |
||
| 954 | */ |
||
| 955 | protected function prepareUpdate(array $updates, $oneToOneRelation = false): array |
||
| 987 | |||
| 988 | /** |
||
| 989 | * @param string|array $columns |
||
| 990 | * @param bool $containsDot |
||
| 991 | * |
||
| 992 | * @return bool |
||
| 993 | */ |
||
| 994 | protected function isInvalidColumn($columns, $containsDot = false): bool |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Prepare input data for insert. |
||
| 1008 | * |
||
| 1009 | * @param $inserts |
||
| 1010 | * |
||
| 1011 | * @return array |
||
| 1012 | */ |
||
| 1013 | protected function prepareInsert($inserts): array |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Is input data is has-one relation. |
||
| 1039 | * |
||
| 1040 | * @param array $inserts |
||
| 1041 | * |
||
| 1042 | * @return bool |
||
| 1043 | */ |
||
| 1044 | protected function isHasOneRelation($inserts): bool |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Ignore fields to save. |
||
| 1061 | * |
||
| 1062 | * @param string|array $fields |
||
| 1063 | * |
||
| 1064 | * @return $this |
||
| 1065 | */ |
||
| 1066 | public function ignore($fields): self |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * @param array $data |
||
| 1075 | * @param string|array $columns |
||
| 1076 | * |
||
| 1077 | * @return array|mixed |
||
| 1078 | */ |
||
| 1079 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Find field object by column. |
||
| 1100 | * |
||
| 1101 | * @param $column |
||
| 1102 | * |
||
| 1103 | * @return mixed |
||
| 1104 | */ |
||
| 1105 | protected function getFieldByColumn($column) |
||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Set original data for each field. |
||
| 1120 | * |
||
| 1121 | * @return void |
||
| 1122 | */ |
||
| 1123 | protected function setFieldOriginalValue() |
||
| 1124 | { |
||
| 1125 | $values = $this->model->toArray(); |
||
| 1126 | |||
| 1127 | $this->builder->fields()->each(function (Field $field) use ($values) { |
||
| 1128 | $field->setOriginal($values); |
||
| 1129 | }); |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Set all fields value in form. |
||
| 1134 | * |
||
| 1135 | * @param $id |
||
| 1136 | * |
||
| 1137 | * @return void |
||
| 1138 | */ |
||
| 1139 | protected function setFieldValue($id) |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Add a fieldset to form. |
||
| 1164 | * |
||
| 1165 | * @param string $title |
||
| 1166 | * @param Closure $setCallback |
||
| 1167 | * |
||
| 1168 | * @return Field\Fieldset |
||
| 1169 | */ |
||
| 1170 | View Code Duplication | public function fieldset(string $title, Closure $setCallback) |
|
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Get validation messages. |
||
| 1185 | * |
||
| 1186 | * @param array $input |
||
| 1187 | * |
||
| 1188 | * @return MessageBag|bool |
||
| 1189 | */ |
||
| 1190 | public function validationMessages($input) |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Merge validation messages from input validators. |
||
| 1212 | * |
||
| 1213 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 1214 | * |
||
| 1215 | * @return MessageBag |
||
| 1216 | */ |
||
| 1217 | protected function mergeValidationMessages($validators): MessageBag |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Get all relations of model from callable. |
||
| 1230 | * |
||
| 1231 | * @return array |
||
| 1232 | */ |
||
| 1233 | public function getRelations(): array |
||
| 1260 | |||
| 1261 | /** |
||
| 1262 | * Set action for form. |
||
| 1263 | * |
||
| 1264 | * @param string $action |
||
| 1265 | * |
||
| 1266 | * @return $this |
||
| 1267 | */ |
||
| 1268 | public function setAction($action): self |
||
| 1274 | |||
| 1275 | /** |
||
| 1276 | * Set field and label width in current form. |
||
| 1277 | * |
||
| 1278 | * @param int $fieldWidth |
||
| 1279 | * @param int $labelWidth |
||
| 1280 | * |
||
| 1281 | * @return $this |
||
| 1282 | */ |
||
| 1283 | public function setWidth($fieldWidth = 8, $labelWidth = 2): self |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Set view for form. |
||
| 1297 | * |
||
| 1298 | * @param string $view |
||
| 1299 | * |
||
| 1300 | * @return $this |
||
| 1301 | */ |
||
| 1302 | public function setView($view): self |
||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * Set title for form. |
||
| 1311 | * |
||
| 1312 | * @param string $title |
||
| 1313 | * |
||
| 1314 | * @return $this |
||
| 1315 | */ |
||
| 1316 | public function setTitle($title = ''): self |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * Add a row in form. |
||
| 1325 | * |
||
| 1326 | * @param Closure $callback |
||
| 1327 | * |
||
| 1328 | * @return $this |
||
| 1329 | */ |
||
| 1330 | public function row(Closure $callback): self |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * Tools setting for form. |
||
| 1339 | * |
||
| 1340 | * @param Closure $callback |
||
| 1341 | */ |
||
| 1342 | public function tools(Closure $callback) |
||
| 1346 | |||
| 1347 | /** |
||
| 1348 | * @param Closure|null $callback |
||
| 1349 | * |
||
| 1350 | * @return Form\Tools |
||
| 1351 | */ |
||
| 1352 | View Code Duplication | public function header(Closure $callback = null) |
|
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Indicates if current form page is creating. |
||
| 1363 | * |
||
| 1364 | * @return bool |
||
| 1365 | */ |
||
| 1366 | public function isCreating(): bool |
||
| 1370 | |||
| 1371 | /** |
||
| 1372 | * Indicates if current form page is editing. |
||
| 1373 | * |
||
| 1374 | * @return bool |
||
| 1375 | */ |
||
| 1376 | public function isEditing(): bool |
||
| 1380 | |||
| 1381 | /** |
||
| 1382 | * Disable form submit. |
||
| 1383 | * |
||
| 1384 | * @param bool $disable |
||
| 1385 | * |
||
| 1386 | * @return $this |
||
| 1387 | * |
||
| 1388 | * @deprecated |
||
| 1389 | */ |
||
| 1390 | public function disableSubmit(bool $disable = true): self |
||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * Disable form reset. |
||
| 1399 | * |
||
| 1400 | * @param bool $disable |
||
| 1401 | * |
||
| 1402 | * @return $this |
||
| 1403 | * |
||
| 1404 | * @deprecated |
||
| 1405 | */ |
||
| 1406 | public function disableReset(bool $disable = true): self |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Disable View Checkbox on footer. |
||
| 1415 | * |
||
| 1416 | * @param bool $disable |
||
| 1417 | * |
||
| 1418 | * @return $this |
||
| 1419 | */ |
||
| 1420 | public function disableViewCheck(bool $disable = true): self |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * Disable Editing Checkbox on footer. |
||
| 1429 | * |
||
| 1430 | * @param bool $disable |
||
| 1431 | * |
||
| 1432 | * @return $this |
||
| 1433 | */ |
||
| 1434 | public function disableEditingCheck(bool $disable = true): self |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Disable Creating Checkbox on footer. |
||
| 1443 | * |
||
| 1444 | * @param bool $disable |
||
| 1445 | * |
||
| 1446 | * @return $this |
||
| 1447 | */ |
||
| 1448 | public function disableCreatingCheck(bool $disable = true): self |
||
| 1454 | |||
| 1455 | /** |
||
| 1456 | * Footer setting for form. |
||
| 1457 | * |
||
| 1458 | * @param Closure $callback |
||
| 1459 | * |
||
| 1460 | * @return \Encore\Admin\Form\Footer |
||
| 1461 | */ |
||
| 1462 | View Code Duplication | public function footer(Closure $callback = null) |
|
| 1470 | |||
| 1471 | /** |
||
| 1472 | * Get current resource route url. |
||
| 1473 | * |
||
| 1474 | * @param int $slice |
||
| 1475 | * |
||
| 1476 | * @return string |
||
| 1477 | */ |
||
| 1478 | public function resource($slice = -2): string |
||
| 1488 | |||
| 1489 | /** |
||
| 1490 | * Render the form contents. |
||
| 1491 | * |
||
| 1492 | * @return string |
||
| 1493 | */ |
||
| 1494 | public function render() |
||
| 1502 | |||
| 1503 | /** |
||
| 1504 | * Get or set input data. |
||
| 1505 | * |
||
| 1506 | * @param string $key |
||
| 1507 | * @param null $value |
||
| 1508 | * |
||
| 1509 | * @return array|mixed |
||
| 1510 | */ |
||
| 1511 | public function input($key, $value = null) |
||
| 1519 | |||
| 1520 | /** |
||
| 1521 | * Register custom field. |
||
| 1522 | * |
||
| 1523 | * @param string $abstract |
||
| 1524 | * @param string $class |
||
| 1525 | * |
||
| 1526 | * @return void |
||
| 1527 | */ |
||
| 1528 | public static function extend($abstract, $class) |
||
| 1532 | |||
| 1533 | /** |
||
| 1534 | * Set form field alias. |
||
| 1535 | * |
||
| 1536 | * @param string $field |
||
| 1537 | * @param string $alias |
||
| 1538 | * |
||
| 1539 | * @return void |
||
| 1540 | */ |
||
| 1541 | public static function alias($field, $alias) |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Remove registered field. |
||
| 1548 | * |
||
| 1549 | * @param array|string $abstract |
||
| 1550 | */ |
||
| 1551 | public static function forget($abstract) |
||
| 1555 | |||
| 1556 | /** |
||
| 1557 | * Find field class. |
||
| 1558 | * |
||
| 1559 | * @param string $method |
||
| 1560 | * |
||
| 1561 | * @return bool|mixed |
||
| 1562 | */ |
||
| 1563 | public static function findFieldClass($method) |
||
| 1578 | |||
| 1579 | /** |
||
| 1580 | * Collect assets required by registered field. |
||
| 1581 | * |
||
| 1582 | * @return array |
||
| 1583 | */ |
||
| 1584 | public static function collectFieldAssets(): array |
||
| 1609 | |||
| 1610 | /** |
||
| 1611 | * Add a new layout column. |
||
| 1612 | * |
||
| 1613 | * @param int $width |
||
| 1614 | * @param \Closure $closure |
||
| 1615 | * |
||
| 1616 | * @return $this |
||
| 1617 | */ |
||
| 1618 | View Code Duplication | public function column($width, \Closure $closure): self |
|
| 1626 | |||
| 1627 | /** |
||
| 1628 | * Initialize filter layout. |
||
| 1629 | */ |
||
| 1630 | protected function initLayout() |
||
| 1634 | |||
| 1635 | /** |
||
| 1636 | * Getter. |
||
| 1637 | * |
||
| 1638 | * @param string $name |
||
| 1639 | * |
||
| 1640 | * @return array|mixed |
||
| 1641 | */ |
||
| 1642 | public function __get($name) |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * Setter. |
||
| 1649 | * |
||
| 1650 | * @param string $name |
||
| 1651 | * @param mixed $value |
||
| 1652 | * |
||
| 1653 | * @return array |
||
| 1654 | */ |
||
| 1655 | public function __set($name, $value) |
||
| 1659 | |||
| 1660 | /** |
||
| 1661 | * Generate a Field object and add to form builder if Field exists. |
||
| 1662 | * |
||
| 1663 | * @param string $method |
||
| 1664 | * @param array $arguments |
||
| 1665 | * |
||
| 1666 | * @return Field |
||
| 1667 | */ |
||
| 1668 | public function __call($method, $arguments) |
||
| 1684 | |||
| 1685 | /** |
||
| 1686 | * @return Layout |
||
| 1687 | */ |
||
| 1688 | public function getLayout(): Layout |
||
| 1692 | } |
||
| 1693 |
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: