Completed
Pull Request — master (#411)
by Nikita
03:54
created

FormField::getDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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