Complex classes like FormField 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 FormField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class FormField |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Name of the field. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Type of the field. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $type; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * All options for the field. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $options = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Is field rendered. |
||
| 44 | * |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $rendered = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Form |
||
| 51 | */ |
||
| 52 | protected $parent; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $template; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var FormHelper |
||
| 61 | */ |
||
| 62 | protected $formHelper; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Name of the property for value setting. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $valueProperty = 'value'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Name of the property for default value. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $defaultValueProperty = 'default_value'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Is default value set? |
||
| 80 | * |
||
| 81 | * @var bool|false |
||
| 82 | */ |
||
| 83 | protected $hasDefault = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \Closure|null |
||
| 87 | */ |
||
| 88 | protected $valueClosure = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Array of filters key(alias/name) => objects. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $filters = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Raw/unfiltered field value. |
||
| 99 | * |
||
| 100 | * @var mixed $rawValues |
||
| 101 | */ |
||
| 102 | protected $rawValue; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Override filters with same alias/name for field. |
||
| 106 | * |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | protected $filtersOverride = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $name |
||
| 113 | * @param string $type |
||
| 114 | * @param Form $parent |
||
| 115 | * @param array $options |
||
| 116 | */ |
||
| 117 | 101 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Setup the value of the form field. |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | 101 | protected function setupValue() |
|
| 136 | { |
||
| 137 | 101 | $value = $this->getOption($this->valueProperty); |
|
| 138 | 101 | $isChild = $this->getOption('is_child'); |
|
| 139 | |||
| 140 | 101 | if ($value instanceof \Closure) { |
|
| 141 | $this->valueClosure = $value; |
||
| 142 | } |
||
| 143 | |||
| 144 | 101 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
| 145 | 90 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
| 146 | 96 | } elseif (!$isChild) { |
|
| 147 | 14 | $this->hasDefault = true; |
|
| 148 | 14 | } |
|
| 149 | 96 | } |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get the template, can be config variable or view path. |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | abstract protected function getTemplate(); |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 35 | protected function getViewTemplate() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Render the field. |
||
| 168 | * |
||
| 169 | * @param array $options |
||
| 170 | * @param bool $showLabel |
||
| 171 | * @param bool $showField |
||
| 172 | * @param bool $showError |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 35 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 176 | { |
||
| 177 | 35 | $this->prepareOptions($options); |
|
| 178 | 35 | $value = $this->getValue(); |
|
| 179 | 35 | $defaultValue = $this->getDefaultValue(); |
|
| 180 | |||
| 181 | 35 | if ($showField) { |
|
| 182 | 35 | $this->rendered = true; |
|
| 183 | 35 | } |
|
| 184 | |||
| 185 | // Override default value with value |
||
| 186 | 35 | if (!$this->isValidValue($value) && $this->isValidValue($defaultValue)) { |
|
| 187 | $this->setOption($this->valueProperty, $defaultValue); |
||
| 188 | } |
||
| 189 | |||
| 190 | 35 | if (!$this->needsLabel()) { |
|
| 191 | 11 | $showLabel = false; |
|
| 192 | 11 | } |
|
| 193 | |||
| 194 | 35 | if ($showError) { |
|
| 195 | 34 | $showError = $this->parent->haveErrorsEnabled(); |
|
| 196 | 34 | } |
|
| 197 | |||
| 198 | 35 | $data = $this->getRenderData(); |
|
| 199 | |||
| 200 | 35 | return $this->formHelper->getView()->make( |
|
| 201 | 35 | $this->getViewTemplate(), |
|
| 202 | $data + [ |
||
| 203 | 35 | 'name' => $this->name, |
|
| 204 | 35 | 'nameKey' => $this->getNameKey(), |
|
| 205 | 35 | 'type' => $this->type, |
|
| 206 | 35 | 'options' => $this->options, |
|
| 207 | 35 | 'showLabel' => $showLabel, |
|
| 208 | 35 | 'showField' => $showField, |
|
| 209 | 35 | 'showError' => $showError, |
|
| 210 | 35 | 'errorBag' => $this->parent->getErrorBag(), |
|
| 211 | 35 | 'translationTemplate' => $this->parent->getTranslationTemplate(), |
|
| 212 | ] |
||
| 213 | 35 | )->render(); |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | 35 | protected function getRenderData() { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get the attribute value from the model by name. |
||
| 227 | * |
||
| 228 | * @param mixed $model |
||
| 229 | * @param string $name |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | 92 | protected function getModelValueAttribute($model, $name) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Transform array like syntax to dot syntax. |
||
| 246 | * |
||
| 247 | * @param string $key |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | 101 | protected function transformKey($key) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Prepare options for rendering. |
||
| 257 | * |
||
| 258 | * @param array $options |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 101 | protected function prepareOptions(array $options = []) |
|
| 262 | { |
||
| 263 | 101 | $helper = $this->formHelper; |
|
| 264 | 101 | $rulesParser = $helper->createRulesParser($this); |
|
| 265 | 101 | $rules = $this->getOption('rules'); |
|
| 266 | 101 | $parsedRules = $rules ? $rulesParser->parse($rules) : []; |
|
| 267 | |||
| 268 | 101 | $this->options = $helper->mergeOptions($this->options, $options); |
|
| 269 | |||
| 270 | 101 | foreach (['attr', 'label_attr', 'wrapper'] as $appendable) { |
|
| 271 | // Append values to the 'class' attribute |
||
| 272 | 101 | if ($this->getOption("{$appendable}.class_append")) { |
|
| 273 | // Combine the current class attribute with the appends |
||
| 274 | 3 | $append = $this->getOption("{$appendable}.class_append"); |
|
| 275 | 3 | $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append; |
|
| 276 | 3 | $this->setOption("{$appendable}.class", $classAttribute); |
|
| 277 | |||
| 278 | // Then remove the class_append option to prevent it from showing up as an attribute in the HTML |
||
| 279 | 3 | $this->setOption("{$appendable}.class_append", null); |
|
| 280 | 3 | } |
|
| 281 | 101 | } |
|
| 282 | |||
| 283 | 101 | if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) { |
|
| 284 | 2 | $this->name = $this->name.'[]'; |
|
| 285 | 2 | $this->setOption('tmp.multipleBracesSet', true); |
|
| 286 | 2 | } |
|
| 287 | |||
| 288 | 101 | if ($this->parent->haveErrorsEnabled()) { |
|
| 289 | 101 | $this->addErrorClass(); |
|
| 290 | 101 | } |
|
| 291 | |||
| 292 | 101 | if ($this->getOption('required') === true || isset($parsedRules['required'])) { |
|
| 293 | 4 | $lblClass = $this->getOption('label_attr.class', ''); |
|
| 294 | 4 | $requiredClass = $this->getConfig('defaults.required_class', 'required'); |
|
| 295 | |||
| 296 | 4 | if (! Str::contains($lblClass, $requiredClass)) { |
|
| 297 | 4 | $lblClass .= ' '.$requiredClass; |
|
| 298 | 4 | $this->setOption('label_attr.class', $lblClass); |
|
| 299 | 4 | } |
|
| 300 | |||
| 301 | 4 | if ($this->parent->clientValidationEnabled()) { |
|
| 302 | 3 | $this->setOption('attr.required', 'required'); |
|
| 303 | 3 | } |
|
| 304 | 4 | } |
|
| 305 | |||
| 306 | 101 | if ($this->parent->clientValidationEnabled() && $parsedRules) { |
|
|
|
|||
| 307 | 1 | $attrs = $this->getOption('attr') + $parsedRules; |
|
| 308 | 1 | $this->setOption('attr', $attrs); |
|
| 309 | 1 | } |
|
| 310 | |||
| 311 | 101 | $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper'))); |
|
| 312 | 101 | $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors'))); |
|
| 313 | |||
| 314 | 101 | if ($this->getOption('help_block.text')) { |
|
| 315 | 1 | $this->setOption( |
|
| 316 | 1 | 'help_block.helpBlockAttrs', |
|
| 317 | 1 | $helper->prepareAttributes($this->getOption('help_block.attr')) |
|
| 318 | 1 | ); |
|
| 319 | 1 | } |
|
| 320 | |||
| 321 | 101 | return $this->options; |
|
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get name of the field. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 39 | public function getName() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set name of the field. |
||
| 336 | * |
||
| 337 | * @param string $name |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | 12 | public function setName($name) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get dot notation key for fields. |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | **/ |
||
| 352 | 54 | public function getNameKey() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get field options. |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | 12 | public function getOptions() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 369 | * |
||
| 370 | * @param string $option |
||
| 371 | * @param mixed|null $default |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | 101 | public function getOption($option, $default = null) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Set field options. |
||
| 381 | * |
||
| 382 | * @param array $options |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | 12 | public function setOptions($options) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Set single option on the field. |
||
| 394 | * |
||
| 395 | * @param string $name |
||
| 396 | * @param mixed $value |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | 101 | public function setOption($name, $value) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Get the type of the field. |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 65 | public function getType() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Set type of the field. |
||
| 418 | * |
||
| 419 | * @param mixed $type |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 1 | public function setType($type) |
|
| 423 | { |
||
| 424 | 1 | if ($this->formHelper->getFieldType($type)) { |
|
| 425 | 1 | $this->type = $type; |
|
| 426 | 1 | } |
|
| 427 | |||
| 428 | 1 | return $this; |
|
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return Form |
||
| 433 | */ |
||
| 434 | 101 | public function getParent() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Check if the field is rendered. |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | 4 | public function isRendered() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Default options for field. |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | 76 | protected function getDefaults() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Defaults used across all fields. |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | 101 | private function allDefaults() |
|
| 465 | { |
||
| 466 | return [ |
||
| 467 | 101 | 'wrapper' => ['class' => $this->getConfig('defaults.wrapper_class')], |
|
| 468 | 101 | 'attr' => ['class' => $this->getConfig('defaults.field_class')], |
|
| 469 | 101 | 'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [ |
|
| 470 | 101 | 'class' => $this->getConfig('defaults.help_block_class') |
|
| 471 | 101 | ]], |
|
| 472 | 101 | 'value' => null, |
|
| 473 | 101 | 'default_value' => null, |
|
| 474 | 101 | 'label' => null, |
|
| 475 | 101 | 'label_show' => true, |
|
| 476 | 101 | 'is_child' => false, |
|
| 477 | 101 | 'label_attr' => ['class' => $this->getConfig('defaults.label_class')], |
|
| 478 | 101 | 'errors' => ['class' => $this->getConfig('defaults.error_class')], |
|
| 479 | 101 | 'rules' => [], |
|
| 480 | 101 | 'error_messages' => [] |
|
| 481 | 101 | ]; |
|
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get real name of the field without form namespace. |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | 100 | public function getRealName() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @param $value |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | 94 | public function setValue($value) |
|
| 499 | { |
||
| 500 | 94 | if ($this->hasDefault) { |
|
| 501 | 1 | return $this; |
|
| 502 | } |
||
| 503 | |||
| 504 | 94 | $closure = $this->valueClosure; |
|
| 505 | |||
| 506 | 94 | if ($closure instanceof \Closure) { |
|
| 507 | $value = $closure($value ?: null); |
||
| 508 | } |
||
| 509 | |||
| 510 | 94 | if (!$this->isValidValue($value)) { |
|
| 511 | 91 | $value = $this->getOption($this->defaultValueProperty); |
|
| 512 | 91 | } |
|
| 513 | |||
| 514 | 94 | $this->options[$this->valueProperty] = $value; |
|
| 515 | |||
| 516 | 94 | return $this; |
|
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Set the template property on the object. |
||
| 521 | * |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | 101 | private function setTemplate() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Add error class to wrapper if validation errors exist. |
||
| 531 | * |
||
| 532 | * @return void |
||
| 533 | */ |
||
| 534 | 101 | protected function addErrorClass() |
|
| 535 | { |
||
| 536 | 101 | $errors = $this->parent->getRequest()->session()->get('errors'); |
|
| 537 | 101 | $errorBag = $this->parent->getErrorBag(); |
|
| 538 | |||
| 539 | 101 | if ($errors && $errors->hasBag($errorBag) && $errors->getBag($errorBag)->has($this->getNameKey())) { |
|
| 540 | $fieldErrorClass = $this->getConfig('defaults.field_error_class'); |
||
| 541 | $fieldClass = $this->getOption('attr.class'); |
||
| 542 | |||
| 543 | if ($fieldErrorClass && !str_contains($fieldClass, $fieldErrorClass)) { |
||
| 544 | $fieldClass .= ' ' . $fieldErrorClass; |
||
| 545 | $this->setOption('attr.class', $fieldClass); |
||
| 546 | } |
||
| 547 | |||
| 548 | $wrapperErrorClass = $this->getConfig('defaults.wrapper_error_class'); |
||
| 549 | $wrapperClass = $this->getOption('wrapper.class'); |
||
| 550 | |||
| 551 | if ($wrapperErrorClass && $this->getOption('wrapper') && !str_contains($wrapperClass, $wrapperErrorClass)) { |
||
| 552 | $wrapperClass .= ' ' . $wrapperErrorClass; |
||
| 553 | $this->setOption('wrapper.class', $wrapperClass); |
||
| 554 | } |
||
| 555 | } |
||
| 556 | 101 | } |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 560 | * |
||
| 561 | * @param array $options |
||
| 562 | */ |
||
| 563 | 101 | protected function setDefaultOptions(array $options = []) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Creates default wrapper classes for the form element. |
||
| 576 | * |
||
| 577 | * @param array $options |
||
| 578 | * @return array |
||
| 579 | */ |
||
| 580 | 101 | protected function setDefaultClasses(array $options = []) |
|
| 581 | { |
||
| 582 | 101 | $wrapper_class = $this->getConfig('defaults.' . $this->type . '.wrapper_class', ''); |
|
| 583 | 101 | $label_class = $this->getConfig('defaults.' . $this->type . '.label_class', ''); |
|
| 584 | 101 | $field_class = $this->getConfig('defaults.' . $this->type . '.field_class', ''); |
|
| 585 | |||
| 586 | 101 | $defaults = []; |
|
| 587 | 101 | if ($wrapper_class && !Arr::get($options, 'wrapper.class')) { |
|
| 588 | $defaults['wrapper']['class'] = $wrapper_class; |
||
| 589 | } |
||
| 590 | 101 | if ($label_class && !Arr::get($options, 'label_attr.class')) { |
|
| 591 | $defaults['label_attr']['class'] = $label_class; |
||
| 592 | } |
||
| 593 | 101 | if ($field_class && !Arr::get($options, 'attr.class')) { |
|
| 594 | 1 | $defaults['attr']['class'] = $field_class; |
|
| 595 | 1 | } |
|
| 596 | 101 | return $defaults; |
|
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Setup the label for the form field. |
||
| 601 | * |
||
| 602 | * @return void |
||
| 603 | */ |
||
| 604 | 101 | protected function setupLabel() |
|
| 605 | { |
||
| 606 | 101 | if ($this->getOption('label') !== null) { |
|
| 607 | 24 | return; |
|
| 608 | } |
||
| 609 | |||
| 610 | 99 | if ($template = $this->parent->getTranslationTemplate()) { |
|
| 611 | 3 | $label = str_replace( |
|
| 612 | 3 | ['{name}', '{type}'], |
|
| 613 | 3 | [$this->getRealName(), 'label'], |
|
| 614 | $template |
||
| 615 | 3 | ); |
|
| 616 | 99 | } elseif ($langName = $this->parent->getLanguageName()) { |
|
| 617 | 4 | $label = sprintf('%s.%s', $langName, $this->getRealName()); |
|
| 618 | 4 | } else { |
|
| 619 | 93 | $label = $this->getRealName(); |
|
| 620 | } |
||
| 621 | |||
| 622 | 99 | $this->setOption('label', $this->formHelper->formatLabel($label)); |
|
| 623 | 99 | } |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Check if fields needs label. |
||
| 627 | * |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | 35 | protected function needsLabel() |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Disable field. |
||
| 644 | * |
||
| 645 | * @return $this |
||
| 646 | */ |
||
| 647 | 1 | public function disable() |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Enable field. |
||
| 656 | * |
||
| 657 | * @return $this |
||
| 658 | */ |
||
| 659 | 1 | public function enable() |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Get validation rules for a field if any with label for attributes. |
||
| 668 | * |
||
| 669 | * @return array|null |
||
| 670 | */ |
||
| 671 | 9 | public function getValidationRules() |
|
| 672 | { |
||
| 673 | 9 | $rules = $this->getOption('rules', []); |
|
| 674 | 9 | $name = $this->getNameKey(); |
|
| 675 | 9 | $messages = $this->getOption('error_messages', []); |
|
| 676 | 9 | $formName = $this->formHelper->transformToDotSyntax($this->parent->getName()); |
|
| 677 | |||
| 678 | 9 | if ($messages && $formName) { |
|
| 679 | 1 | $newMessages = []; |
|
| 680 | 1 | foreach ($messages as $messageKey => $message) { |
|
| 681 | 1 | $messageKey = sprintf('%s.%s', $formName, $messageKey); |
|
| 682 | 1 | $newMessages[$messageKey] = $message; |
|
| 683 | 1 | } |
|
| 684 | 1 | $messages = $newMessages; |
|
| 685 | 1 | } |
|
| 686 | |||
| 687 | 9 | if (!$rules) { |
|
| 688 | 2 | return new Rules([]); |
|
| 689 | } |
||
| 690 | |||
| 691 | 8 | if (is_array($rules)) { |
|
| 692 | 2 | $rules = array_map(function ($rule) use ($name) { |
|
| 693 | 2 | if ($rule instanceof \Closure) { |
|
| 694 | return $rule($name); |
||
| 695 | } |
||
| 696 | |||
| 697 | 2 | return $rule; |
|
| 698 | 2 | }, $rules); |
|
| 699 | 2 | } |
|
| 700 | |||
| 701 | 8 | return new Rules( |
|
| 702 | 8 | [$name => $rules], |
|
| 703 | 8 | [$name => $this->getOption('label')], |
|
| 704 | $messages |
||
| 705 | 8 | ); |
|
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Get this field's attributes, probably just one. |
||
| 710 | * |
||
| 711 | * @return array |
||
| 712 | */ |
||
| 713 | 3 | public function getAllAttributes() |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Get value property. |
||
| 720 | * |
||
| 721 | * @param mixed|null $default |
||
| 722 | * @return mixed |
||
| 723 | */ |
||
| 724 | 38 | public function getValue($default = null) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * Get default value property. |
||
| 731 | * |
||
| 732 | * @param mixed|null $default |
||
| 733 | * @return mixed |
||
| 734 | */ |
||
| 735 | 35 | public function getDefaultValue($default = null) |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Check if provided value is valid for this type. |
||
| 742 | * |
||
| 743 | * @return bool |
||
| 744 | */ |
||
| 745 | 94 | protected function isValidValue($value) |
|
| 749 | |||
| 750 | /** |
||
| 751 | * Method initFilters used to initialize filters |
||
| 752 | * from field options and bind it to the same. |
||
| 753 | * |
||
| 754 | * @return $this |
||
| 755 | */ |
||
| 756 | 96 | protected function initFilters() |
|
| 757 | { |
||
| 758 | // If override status is set in field options to true |
||
| 759 | // we will change filtersOverride property value to true |
||
| 760 | // so we can override existing filters with registered |
||
| 761 | // alias/name in addFilter method. |
||
| 762 | 96 | $overrideStatus = $this->getOption('filters_override', false); |
|
| 763 | 96 | if ($overrideStatus) { |
|
| 764 | 2 | $this->setFiltersOverride(true); |
|
| 765 | 2 | } |
|
| 766 | |||
| 767 | // Get filters and bind it to field. |
||
| 768 | 96 | $filters = $this->getOption('filters', []); |
|
| 769 | 96 | foreach ($filters as $filter) { |
|
| 770 | 8 | $this->addFilter($filter); |
|
| 771 | 96 | } |
|
| 772 | |||
| 773 | 96 | return $this; |
|
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Method setFilters used to set filters to current filters property. |
||
| 778 | * |
||
| 779 | * @param array $filters |
||
| 780 | * |
||
| 781 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 782 | */ |
||
| 783 | public function setFilters(array $filters) |
||
| 784 | { |
||
| 785 | $this->clearFilters(); |
||
| 786 | foreach ($filters as $filter) { |
||
| 787 | $this->addFilter($filter); |
||
| 788 | } |
||
| 789 | |||
| 790 | return $this; |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Method getFilters returns array of binded filters |
||
| 795 | * if there are any binded. Otherwise empty array. |
||
| 796 | * |
||
| 797 | * @return array |
||
| 798 | */ |
||
| 799 | 23 | public function getFilters() |
|
| 803 | |||
| 804 | /** |
||
| 805 | * @param string|FilterInterface $filter |
||
| 806 | * |
||
| 807 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 808 | * |
||
| 809 | * @throws FilterAlreadyBindedException |
||
| 810 | */ |
||
| 811 | 8 | public function addFilter($filter) |
|
| 812 | { |
||
| 813 | // Resolve filter object from string/object or throw Ex. |
||
| 814 | 8 | $filterObj = FilterResolver::instance($filter); |
|
| 815 | |||
| 816 | // If filtersOverride is allowed we will override filter |
||
| 817 | // with same alias/name if there is one with new resolved filter. |
||
| 818 | 8 | if ($this->getFiltersOverride()) { |
|
| 819 | 1 | if ($key = array_search($filterObj->getName(), $this->getFilters())) { |
|
| 820 | $this->filters[$key] = $filterObj; |
||
| 821 | } else { |
||
| 822 | 1 | $this->filters[$filterObj->getName()] = $filterObj; |
|
| 823 | } |
||
| 824 | 1 | } else { |
|
| 825 | // If filtersOverride is disabled and we found |
||
| 826 | // equal alias defined we will throw Ex. |
||
| 827 | 7 | if (array_key_exists($filterObj->getName(), $this->getFilters())) { |
|
| 828 | 1 | $ex = new FilterAlreadyBindedException($filterObj->getName(), $this->getName()); |
|
| 829 | 1 | throw $ex; |
|
| 830 | } |
||
| 831 | |||
| 832 | // Filter with resolvedFilter alias/name doesn't exist |
||
| 833 | // so we will bind it as new one to field. |
||
| 834 | 7 | $this->filters[$filterObj->getName()] = $filterObj; |
|
| 835 | } |
||
| 836 | |||
| 837 | 8 | return $this; |
|
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 842 | * |
||
| 843 | * @param string $name |
||
| 844 | * |
||
| 845 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 846 | */ |
||
| 847 | 1 | public function removeFilter($name) |
|
| 848 | { |
||
| 849 | 1 | $filters = $this->getFilters(); |
|
| 850 | 1 | if (array_key_exists($name, $filters)) { |
|
| 851 | 1 | unset($filters[$name]); |
|
| 852 | 1 | $this->filters = $filters; |
|
| 853 | 1 | } |
|
| 854 | |||
| 855 | 1 | return $this; |
|
| 856 | } |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 860 | * |
||
| 861 | * @param array $filterNames |
||
| 862 | * |
||
| 863 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 864 | */ |
||
| 865 | 1 | public function removeFilters(array $filterNames) |
|
| 866 | { |
||
| 867 | 1 | $filters = $this->getFilters(); |
|
| 868 | 1 | foreach ($filterNames as $filterName) { |
|
| 869 | 1 | if (array_key_exists($filterName, $filters)) { |
|
| 870 | 1 | unset($filters[$filterName]); |
|
| 871 | 1 | $this->filters = $filters; |
|
| 872 | 1 | } |
|
| 873 | 1 | } |
|
| 874 | |||
| 875 | 1 | return $this; |
|
| 876 | } |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Method clearFilters used to empty current filters property. |
||
| 880 | * |
||
| 881 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 882 | */ |
||
| 883 | 1 | public function clearFilters() |
|
| 888 | |||
| 889 | /** |
||
| 890 | * Method used to set FiltersOverride status to provided value. |
||
| 891 | * |
||
| 892 | * @param $status |
||
| 893 | * |
||
| 894 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 895 | */ |
||
| 896 | 2 | public function setFiltersOverride($status) |
|
| 901 | |||
| 902 | /** |
||
| 903 | * @return bool |
||
| 904 | */ |
||
| 905 | 9 | public function getFiltersOverride() |
|
| 909 | |||
| 910 | /** |
||
| 911 | * Method used to set Unfiltered/Unmutated field value. |
||
| 912 | * Method is called before field value mutating starts - request value filtering. |
||
| 913 | * |
||
| 914 | * @param mixed $value |
||
| 915 | * |
||
| 916 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 917 | */ |
||
| 918 | 1 | public function setRawValue($value) |
|
| 923 | |||
| 924 | /** |
||
| 925 | * Returns unfiltered raw value of field. |
||
| 926 | * |
||
| 927 | * @return mixed |
||
| 928 | */ |
||
| 929 | public function getRawValue() |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Get config from the form. |
||
| 936 | * |
||
| 937 | * @return mixed |
||
| 938 | */ |
||
| 939 | 101 | private function getConfig($key = null, $default = null) |
|
| 943 | } |
||
| 944 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.