Completed
Pull Request — master (#526)
by Kristijan
01:39
created

FormField::disable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace Kris\LaravelFormBuilder\Fields;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Kris\LaravelFormBuilder\Filters\Exception\FilterAlreadyBindedException;
8
use Kris\LaravelFormBuilder\Filters\FilterInterface;
9
use Kris\LaravelFormBuilder\Filters\FilterResolver;
10
use Kris\LaravelFormBuilder\Form;
11
use Kris\LaravelFormBuilder\FormHelper;
12
use Kris\LaravelFormBuilder\Rules;
13
14
/**
15
 * Class FormField
16
 *
17
 * @package Kris\LaravelFormBuilder\Fields
18
 */
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 = [])
118
    {
119 101
        $this->name = $name;
120 101
        $this->type = $type;
121 101
        $this->parent = $parent;
122 101
        $this->formHelper = $this->parent->getFormHelper();
123 101
        $this->setTemplate();
124 101
        $this->setDefaultOptions($options);
125 101
        $this->setupValue();
126 96
        $this->initFilters();
127 96
    }
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()
162
    {
163 35
        return $this->parent->getTemplatePrefix() . $this->getOption('template', $this->template);
164
    }
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() {
222 35
        return [];
223
    }
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)
233
    {
234 92
        return $this->formHelper->getModelValue($model, $name);
235
    }
236
237
    /**
238
     * Transform array like syntax to dot syntax.
239
     *
240
     * @param string $key
241
     * @return mixed
242
     */
243 54
    protected function transformKey($key)
244
    {
245 54
        return $this->formHelper->transformToDotSyntax($key);
246
    }
247
248
    /**
249
     * Prepare options for rendering.
250
     *
251
     * @param array $options
252
     * @return array
253
     */
254 101
    protected function prepareOptions(array $options = [])
255
    {
256 101
        $helper = $this->formHelper;
257 101
        $rulesParser = $helper->createRulesParser($this);
258 101
        $rules = $this->getOption('rules');
259 101
        $parsedRules = $rules ? $rulesParser->parse($rules) : [];
260
261 101
        $this->options = $helper->mergeOptions($this->options, $options);
262
263 101
        foreach (['attr', 'label_attr', 'wrapper'] as $appendable) {
264
            // Append values to the 'class' attribute
265 101
            if ($this->getOption("{$appendable}.class_append")) {
266
                // Combine the current class attribute with the appends
267 3
                $append = $this->getOption("{$appendable}.class_append");
268 3
                $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append;
269 3
                $this->setOption("{$appendable}.class", $classAttribute);
270
271
                // Then remove the class_append option to prevent it from showing up as an attribute in the HTML
272 3
                $this->setOption("{$appendable}.class_append", null);
273 3
            }
274 101
        }
275
276 101
        if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) {
277 2
            $this->name = $this->name.'[]';
278 2
            $this->setOption('tmp.multipleBracesSet', true);
279 2
        }
280
281 101
        if ($this->parent->haveErrorsEnabled()) {
282 101
            $this->addErrorClass();
283 101
        }
284
285 101
        if ($this->getOption('required') === true || isset($parsedRules['required'])) {
286 4
            $lblClass = $this->getOption('label_attr.class', '');
287 4
            $requiredClass = $this->getConfig('defaults.required_class', 'required');
288
289 4
            if (! Str::contains($lblClass, $requiredClass)) {
290 4
                $lblClass .= ' '.$requiredClass;
291 4
                $this->setOption('label_attr.class', $lblClass);
292 4
            }
293
294 4
            if ($this->parent->clientValidationEnabled()) {
295 3
                $this->setOption('attr.required', 'required');
296 3
            }
297 4
        }
298
299 101
        if ($this->parent->clientValidationEnabled() && $parsedRules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsedRules of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
300 1
            $attrs = $this->getOption('attr') + $parsedRules;
301 1
            $this->setOption('attr', $attrs);
302 1
        }
303
304 101
        $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper')));
305 101
        $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors')));
306
307 101
        if ($this->getOption('help_block.text')) {
308 1
            $this->setOption(
309 1
                'help_block.helpBlockAttrs',
310 1
                $helper->prepareAttributes($this->getOption('help_block.attr'))
311 1
            );
312 1
        }
313
314 101
        return $this->options;
315
    }
316
317
    /**
318
     * Get name of the field.
319
     *
320
     * @return string
321
     */
322 39
    public function getName()
323
    {
324 39
        return $this->name;
325
    }
326
327
    /**
328
     * Set name of the field.
329
     *
330
     * @param string $name
331
     * @return $this
332
     */
333 12
    public function setName($name)
334
    {
335 12
        $this->name = $name;
336
337 12
        return $this;
338
    }
339
340
    /**
341
     * Get dot notation key for fields.
342
     *
343
     * @return string
344
     **/
345 54
    public function getNameKey()
346
    {
347 54
        return $this->transformKey($this->name);
348
    }
349
350
    /**
351
     * Get field options.
352
     *
353
     * @return array
354
     */
355 12
    public function getOptions()
356
    {
357 12
        return $this->options;
358
    }
359
360
    /**
361
     * Get single option from options array. Can be used with dot notation ('attr.class').
362
     *
363
     * @param string $option
364
     * @param mixed|null $default
365
     * @return mixed
366
     */
367 101
    public function getOption($option, $default = null)
368
    {
369 101
        return Arr::get($this->options, $option, $default);
370
    }
371
372
    /**
373
     * Set field options.
374
     *
375
     * @param array $options
376
     * @return $this
377
     */
378 12
    public function setOptions($options)
379
    {
380 12
        $this->options = $this->prepareOptions($options);
381
382 12
        return $this;
383
    }
384
385
    /**
386
     * Set single option on the field.
387
     *
388
     * @param string $name
389
     * @param mixed $value
390
     * @return $this
391
     */
392 101
    public function setOption($name, $value)
393
    {
394 101
        Arr::set($this->options, $name, $value);
395
396 101
        return $this;
397
    }
398
399
    /**
400
     * Get the type of the field.
401
     *
402
     * @return string
403
     */
404 65
    public function getType()
405
    {
406 65
        return $this->type;
407
    }
408
409
    /**
410
     * Set type of the field.
411
     *
412
     * @param mixed $type
413
     * @return $this
414
     */
415 1
    public function setType($type)
416
    {
417 1
        if ($this->formHelper->getFieldType($type)) {
418 1
            $this->type = $type;
419 1
        }
420
421 1
        return $this;
422
    }
423
424
    /**
425
     * @return Form
426
     */
427 101
    public function getParent()
428
    {
429 101
        return $this->parent;
430
    }
431
432
    /**
433
     * Check if the field is rendered.
434
     *
435
     * @return bool
436
     */
437 4
    public function isRendered()
438
    {
439 4
        return $this->rendered;
440
    }
441
442
    /**
443
     * Default options for field.
444
     *
445
     * @return array
446
     */
447 76
    protected function getDefaults()
448
    {
449 76
        return [];
450
    }
451
452
    /**
453
     * Defaults used across all fields.
454
     *
455
     * @return array
456
     */
457 101
    private function allDefaults()
458
    {
459
        return [
460 101
            'wrapper' => ['class' => $this->getConfig('defaults.wrapper_class')],
461 101
            'attr' => ['class' => $this->getConfig('defaults.field_class')],
462 101
            'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [
463 101
                'class' => $this->getConfig('defaults.help_block_class')
464 101
            ]],
465 101
            'value' => null,
466 101
            'default_value' => null,
467 101
            'label' => null,
468 101
            'label_show' => true,
469 101
            'is_child' => false,
470 101
            'label_attr' => ['class' => $this->getConfig('defaults.label_class')],
471 101
            'errors' => ['class' => $this->getConfig('defaults.error_class')],
472 101
            'rules' => [],
473 101
            'error_messages' => []
474 101
        ];
475
    }
476
477
    /**
478
     * Get real name of the field without form namespace.
479
     *
480
     * @return string
481
     */
482 100
    public function getRealName()
483
    {
484 100
        return $this->getOption('real_name', $this->name);
485
    }
486
487
    /**
488
     * @param $value
489
     * @return $this
490
     */
491 94
    public function setValue($value)
492
    {
493 94
        if ($this->hasDefault) {
494 1
            return $this;
495
        }
496
497 94
        $closure = $this->valueClosure;
498
499 94
        if ($closure instanceof \Closure) {
500
            $value = $closure($value ?: null);
501
        }
502
503 94
        if (!$this->isValidValue($value)) {
504 91
            $value = $this->getOption($this->defaultValueProperty);
505 91
        }
506
507 94
        $this->options[$this->valueProperty] = $value;
508
509 94
        return $this;
510
    }
511
512
    /**
513
     * Set the template property on the object.
514
     *
515
     * @return void
516
     */
517 101
    private function setTemplate()
518
    {
519 101
        $this->template = $this->getConfig($this->getTemplate(), $this->getTemplate());
520 101
    }
521
522
    /**
523
     * Add error class to wrapper if validation errors exist.
524
     *
525
     * @return void
526
     */
527 101
    protected function addErrorClass()
528
    {
529 101
        $errors = $this->parent->getRequest()->session()->get('errors');
530 101
        $errorBag = $this->parent->getErrorBag();
531
532 101
        if ($errors && $errors->hasBag($errorBag) && $errors->getBag($errorBag)->has($this->getNameKey())) {
533
            $fieldErrorClass = $this->getConfig('defaults.field_error_class');
534
            $fieldClass = $this->getOption('attr.class');
535
536
            if ($fieldErrorClass && !str_contains($fieldClass, $fieldErrorClass)) {
537
                $fieldClass .= ' ' . $fieldErrorClass;
538
                $this->setOption('attr.class', $fieldClass);
539
            }
540
541
            $wrapperErrorClass = $this->getConfig('defaults.wrapper_error_class');
542
            $wrapperClass = $this->getOption('wrapper.class');
543
544
            if ($wrapperErrorClass && $this->getOption('wrapper') && !str_contains($wrapperClass, $wrapperErrorClass)) {
545
                $wrapperClass .= ' ' . $wrapperErrorClass;
546
                $this->setOption('wrapper.class', $wrapperClass);
547
            }
548
        }
549 101
    }
550
551
    /**
552
     * Merge all defaults with field specific defaults and set template if passed.
553
     *
554
     * @param array $options
555
     */
556 101
    protected function setDefaultOptions(array $options = [])
557
    {
558 101
        $this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults());
559 101
        $this->options = $this->prepareOptions($options);
560
561 101
        $defaults = $this->setDefaultClasses($options);
562 101
        $this->options = $this->formHelper->mergeOptions($this->options, $defaults);
563
564 101
        $this->setupLabel();
565 101
    }
566
567
    /**
568
     * Creates default wrapper classes for the form element.
569
     *
570
     * @param array $options
571
     * @return array
572
     */
573 101
    protected function setDefaultClasses(array $options = [])
574
    {
575 101
        $wrapper_class = $this->getConfig('defaults.' . $this->type . '.wrapper_class', '');
576 101
        $label_class = $this->getConfig('defaults.' . $this->type . '.label_class', '');
577 101
        $field_class = $this->getConfig('defaults.' . $this->type . '.field_class', '');
578
579 101
        $defaults = [];
580 101
        if ($wrapper_class && !Arr::get($options, 'wrapper.class')) {
581
            $defaults['wrapper']['class'] = $wrapper_class;
582
        }
583 101
        if ($label_class && !Arr::get($options, 'label_attr.class')) {
584
            $defaults['label_attr']['class'] = $label_class;
585
        }
586 101
        if ($field_class && !Arr::get($options, 'attr.class')) {
587 1
            $defaults['attr']['class'] = $field_class;
588 1
        }
589 101
        return $defaults;
590
    }
591
592
    /**
593
     * Setup the label for the form field.
594
     *
595
     * @return void
596
     */
597 101
    protected function setupLabel()
598
    {
599 101
        if ($this->getOption('label') !== null) {
600 24
            return;
601
        }
602
603 99
        if ($template = $this->parent->getTranslationTemplate()) {
604 3
            $label = str_replace(
605 3
                ['{name}', '{type}'],
606 3
                [$this->getRealName(), 'label'],
607
                $template
608 3
            );
609 99
        } elseif ($langName = $this->parent->getLanguageName()) {
610 4
            $label = sprintf('%s.%s', $langName, $this->getRealName());
611 4
        } else {
612 93
            $label = $this->getRealName();
613
        }
614
615 99
        $this->setOption('label', $this->formHelper->formatLabel($label));
616 99
    }
617
618
    /**
619
     * Check if fields needs label.
620
     *
621
     * @return bool
622
     */
623 35
    protected function needsLabel()
624
    {
625
        // If field is <select> and child of choice, we don't need label for it
626 35
        $isChildSelect = $this->type == 'select' && $this->getOption('is_child') === true;
627
628 35
        if ($this->type == 'hidden' || $isChildSelect) {
629 11
            return false;
630
        }
631
632 31
        return true;
633
    }
634
635
    /**
636
     * Disable field.
637
     *
638
     * @return $this
639
     */
640 1
    public function disable()
641
    {
642 1
        $this->setOption('attr.disabled', 'disabled');
643
644 1
        return $this;
645
    }
646
647
    /**
648
     * Enable field.
649
     *
650
     * @return $this
651
     */
652 1
    public function enable()
653
    {
654 1
        Arr::forget($this->options, 'attr.disabled');
655
656 1
        return $this;
657
    }
658
659
    /**
660
     * Get validation rules for a field if any with label for attributes.
661
     *
662
     * @return array|null
663
     */
664 9
    public function getValidationRules()
665
    {
666 9
        $rules = $this->getOption('rules', []);
667 9
        $name = $this->getNameKey();
668 9
        $messages = $this->getOption('error_messages', []);
669 9
        $formName = $this->formHelper->transformToDotSyntax($this->parent->getName());
670
671 9
        if ($messages && $formName) {
672 1
            $newMessages = [];
673 1
            foreach ($messages as $messageKey => $message) {
674 1
                $messageKey = sprintf('%s.%s', $formName, $messageKey);
675 1
                $newMessages[$messageKey] = $message;
676 1
            }
677 1
            $messages = $newMessages;
678 1
        }
679
680 9
        if (!$rules) {
681 2
            return (new Rules([]))->setFieldName($this->getNameKey());
682
        }
683
684 8
        if (is_array($rules)) {
685 2
            $rules = array_map(function ($rule) use ($name) {
686 2
                if ($rule instanceof \Closure) {
687
                    return $rule($name);
688
                }
689
690 2
                return $rule;
691 2
            }, $rules);
692 2
        }
693
694 8
        return (new Rules(
695 8
            [$name => $rules],
696 8
            [$name => $this->getOption('label')],
697
            $messages
698 8
        ))->setFieldName($this->getNameKey());
699
    }
700
701
    /**
702
     * Get this field's attributes, probably just one.
703
     *
704
     * @return array
705
     */
706 3
    public function getAllAttributes()
707
    {
708 3
        return [$this->getNameKey()];
709
    }
710
711
    /**
712
     * Get value property.
713
     *
714
     * @param mixed|null $default
715
     * @return mixed
716
     */
717 38
    public function getValue($default = null)
718
    {
719 38
        return $this->getOption($this->valueProperty, $default);
720
    }
721
722
    /**
723
     * Get default value property.
724
     *
725
     * @param mixed|null $default
726
     * @return mixed
727
     */
728 35
    public function getDefaultValue($default = null)
729
    {
730 35
        return $this->getOption($this->defaultValueProperty, $default);
731
    }
732
733
    /**
734
     * Check if provided value is valid for this type.
735
     *
736
     * @return bool
737
     */
738 94
    protected function isValidValue($value)
739
    {
740 94
        return $value !== null;
741
    }
742
743
    /**
744
     * Method initFilters used to initialize filters
745
     * from field options and bind it to the same.
746
     *
747
     * @return $this
748
     */
749 96
    protected function initFilters()
750
    {
751
        // If override status is set in field options to true
752
        // we will change filtersOverride property value to true
753
        // so we can override existing filters with registered
754
        // alias/name in addFilter method.
755 96
        $overrideStatus = $this->getOption('filters_override', false);
756 96
        if ($overrideStatus) {
757 2
            $this->setFiltersOverride(true);
758 2
        }
759
760
        // Get filters and bind it to field.
761 96
        $filters = $this->getOption('filters', []);
762 96
        foreach ($filters as $filter) {
763 8
            $this->addFilter($filter);
764 96
        }
765
766 96
        return $this;
767
    }
768
769
    /**
770
     * Method setFilters used to set filters to current filters property.
771
     *
772
     * @param  array $filters
773
     *
774
     * @return \Kris\LaravelFormBuilder\Fields\FormField
775
     */
776
    public function setFilters(array $filters)
777
    {
778
        $this->clearFilters();
779
        foreach ($filters as $filter) {
780
            $this->addFilter($filter);
781
        }
782
783
        return $this;
784
    }
785
786
    /**
787
     * Method getFilters returns array of binded filters
788
     * if there are any binded. Otherwise empty array.
789
     *
790
     * @return array
791
     */
792 23
    public function getFilters()
793
    {
794 23
        return $this->filters;
795
    }
796
797
    /**
798
     * @param  string|FilterInterface $filter
799
     *
800
     * @return \Kris\LaravelFormBuilder\Fields\FormField
801
     *
802
     * @throws FilterAlreadyBindedException
803
     */
804 8
    public function addFilter($filter)
805
    {
806
        // Resolve filter object from string/object or throw Ex.
807 8
        $filterObj = FilterResolver::instance($filter);
808
809
        // If filtersOverride is allowed we will override filter
810
        // with same alias/name if there is one with new resolved filter.
811 8
        if ($this->getFiltersOverride()) {
812 1
            if ($key = array_search($filterObj->getName(), $this->getFilters())) {
813
                $this->filters[$key] = $filterObj;
814
            } else {
815 1
                $this->filters[$filterObj->getName()] = $filterObj;
816
            }
817 1
        } else {
818
            // If filtersOverride is disabled and we found
819
            // equal alias defined we will throw Ex.
820 7
            if (array_key_exists($filterObj->getName(), $this->getFilters())) {
821 1
                $ex = new FilterAlreadyBindedException($filterObj->getName(), $this->getName());
822 1
                throw $ex;
823
            }
824
825
            // Filter with resolvedFilter alias/name doesn't exist
826
            // so we will bind it as new one to field.
827 7
            $this->filters[$filterObj->getName()] = $filterObj;
828
        }
829
830 8
        return $this;
831
    }
832
833
    /**
834
     * Method removeFilter used to remove filter by provided alias/name.
835
     *
836
     * @param  string $name
837
     *
838
     * @return \Kris\LaravelFormBuilder\Fields\FormField
839
     */
840 1
    public function removeFilter($name)
841
    {
842 1
        $filters = $this->getFilters();
843 1
        if (array_key_exists($name, $filters)) {
844 1
            unset($filters[$name]);
845 1
            $this->filters = $filters;
846 1
        }
847
848 1
        return $this;
849
    }
850
851
    /**
852
     * Method removeFilters used to remove filters by provided aliases/names.
853
     *
854
     * @param  array $filterNames
855
     *
856
     * @return \Kris\LaravelFormBuilder\Fields\FormField
857
     */
858 1
    public function removeFilters(array $filterNames)
859
    {
860 1
        $filters = $this->getFilters();
861 1
        foreach ($filterNames as $filterName) {
862 1
            if (array_key_exists($filterName, $filters)) {
863 1
                unset($filters[$filterName]);
864 1
                $this->filters = $filters;
865 1
            }
866 1
        }
867
868 1
        return $this;
869
    }
870
871
    /**
872
     * Method clearFilters used to empty current filters property.
873
     *
874
     * @return \Kris\LaravelFormBuilder\Fields\FormField
875
     */
876 1
    public function clearFilters()
877
    {
878 1
        $this->filters = [];
879 1
        return $this;
880
    }
881
882
    /**
883
     * Method used to set FiltersOverride status to provided value.
884
     *
885
     * @param $status
886
     *
887
     * @return \Kris\LaravelFormBuilder\Fields\FormField
888
     */
889 2
    public function setFiltersOverride($status)
890
    {
891 2
        $this->filtersOverride = $status;
892 2
        return $this;
893
    }
894
895
    /**
896
     * @return bool
897
     */
898 9
    public function getFiltersOverride()
899
    {
900 9
        return $this->filtersOverride;
901
    }
902
903
    /**
904
     * Method used to set Unfiltered/Unmutated field value.
905
     * Method is called before field value mutating starts - request value filtering.
906
     *
907
     * @param mixed $value
908
     *
909
     * @return \Kris\LaravelFormBuilder\Fields\FormField
910
     */
911 1
    public function setRawValue($value)
912
    {
913 1
        $this->rawValue = $value;
914 1
        return $this;
915
    }
916
917
    /**
918
     * Returns unfiltered raw value of field.
919
     *
920
     * @return mixed
921
     */
922
    public function getRawValue()
923
    {
924
        return $this->rawValue;
925
    }
926
927
    /**
928
     * Get config from the form.
929
     *
930
     * @return mixed
931
     */
932 101
    private function getConfig($key = null, $default = null)
933
    {
934 101
        return $this->parent->getConfig($key, $default);
935
    }
936
}
937