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