Completed
Push — master ( 936d32...358572 )
by Kristijan
19s
created

FormField::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
                'errorBag'  => $this->parent->getErrorBag(),
208 35
                'translationTemplate' => $this->parent->getTranslationTemplate(),
209
            ]
210 35
        )->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 35
    protected function getRenderData() {
219 35
        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 92
    protected function getModelValueAttribute($model, $name)
230
    {
231 92
        $transformedName = $this->transformKey($name);
232 92
        if (is_string($model)) {
233
            return $model;
234 92
        } elseif (is_object($model)) {
235 3
            return object_get($model, $transformedName);
236 92
        } elseif (is_array($model)) {
237 91
            return array_get($model, $transformedName);
238
        }
239 5
    }
240
241
    /**
242
     * Transform array like syntax to dot syntax.
243
     *
244
     * @param string $key
245
     * @return mixed
246
     */
247 101
    protected function transformKey($key)
248
    {
249 101
        return $this->formHelper->transformToDotSyntax($key);
250
    }
251
252
    /**
253
     * Prepare options for rendering.
254
     *
255
     * @param array $options
256
     * @return array
257
     */
258 101
    protected function prepareOptions(array $options = [])
259
    {
260 101
        $helper = $this->formHelper;
261 101
        $rulesParser = $helper->createRulesParser($this);
262 101
        $rules = $this->getOption('rules');
263 101
        $parsedRules = $rules ? $rulesParser->parse($rules) : [];
264
265 101
        $this->options = $helper->mergeOptions($this->options, $options);
266
267 101
        foreach (['attr', 'label_attr', 'wrapper'] as $appendable) {
268
            // Append values to the 'class' attribute
269 101
            if ($this->getOption("{$appendable}.class_append")) {
270
                // Combine the current class attribute with the appends
271 3
                $append = $this->getOption("{$appendable}.class_append");
272 3
                $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append;
273 3
                $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 101
                $this->setOption("{$appendable}.class_append", null);
277
            }
278
        }
279
280 101
        if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) {
281 2
            $this->name = $this->name.'[]';
282 2
            $this->setOption('tmp.multipleBracesSet', true);
283
        }
284
285 101
        if ($this->parent->haveErrorsEnabled()) {
286 101
            $this->addErrorClass();
287
        }
288
289 101
        if ($this->getOption('required') === true || isset($parsedRules['required'])) {
290 4
            $lblClass = $this->getOption('label_attr.class', '');
291 4
            $requiredClass = $this->getConfig('defaults.required_class', 'required');
292
293 4
            if (! str_contains($lblClass, $requiredClass)) {
294 4
                $lblClass .= ' '.$requiredClass;
295 4
                $this->setOption('label_attr.class', $lblClass);
296
            }
297
298 4
            if ($this->parent->clientValidationEnabled()) {
299 3
                $this->setOption('attr.required', 'required');
300
            }
301
        }
302
303 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...
304 1
            $attrs = $this->getOption('attr') + $parsedRules;
305 1
            $this->setOption('attr', $attrs);
306
        }
307
308 101
        $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper')));
309 101
        $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors')));
310
311 101
        if ($this->getOption('help_block.text')) {
312 1
            $this->setOption(
313 1
                'help_block.helpBlockAttrs',
314 1
                $helper->prepareAttributes($this->getOption('help_block.attr'))
315
            );
316
        }
317
318 101
        return $this->options;
319
    }
320
321
    /**
322
     * Get name of the field.
323
     *
324
     * @return string
325
     */
326 39
    public function getName()
327
    {
328 39
        return $this->name;
329
    }
330
331
    /**
332
     * Set name of the field.
333
     *
334
     * @param string $name
335
     * @return $this
336
     */
337 12
    public function setName($name)
338
    {
339 12
        $this->name = $name;
340
341 12
        return $this;
342
    }
343
344
    /**
345
     * Get dot notation key for fields.
346
     *
347
     * @return string
348
     **/
349 54
    public function getNameKey()
350
    {
351 54
        return $this->transformKey($this->name);
352
    }
353
354
    /**
355
     * Get field options.
356
     *
357
     * @return array
358
     */
359 12
    public function getOptions()
360
    {
361 12
        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 101
    public function getOption($option, $default = null)
372
    {
373 101
        return array_get($this->options, $option, $default);
374
    }
375
376
    /**
377
     * Set field options.
378
     *
379
     * @param array $options
380
     * @return $this
381
     */
382 12
    public function setOptions($options)
383
    {
384 12
        $this->options = $this->prepareOptions($options);
385
386 12
        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 101
    public function setOption($name, $value)
397
    {
398 101
        array_set($this->options, $name, $value);
399
400 101
        return $this;
401
    }
402
403
    /**
404
     * Get the type of the field.
405
     *
406
     * @return string
407
     */
408 65
    public function getType()
409
    {
410 65
        return $this->type;
411
    }
412
413
    /**
414
     * Set type of the field.
415
     *
416
     * @param mixed $type
417
     * @return $this
418
     */
419 1
    public function setType($type)
420
    {
421 1
        if ($this->formHelper->getFieldType($type)) {
422 1
            $this->type = $type;
423
        }
424
425 1
        return $this;
426
    }
427
428
    /**
429
     * @return Form
430
     */
431 101
    public function getParent()
432
    {
433 101
        return $this->parent;
434
    }
435
436
    /**
437
     * Check if the field is rendered.
438
     *
439
     * @return bool
440
     */
441 4
    public function isRendered()
442
    {
443 4
        return $this->rendered;
444
    }
445
446
    /**
447
     * Default options for field.
448
     *
449
     * @return array
450
     */
451 76
    protected function getDefaults()
452
    {
453 76
        return [];
454
    }
455
456
    /**
457
     * Defaults used across all fields.
458
     *
459
     * @return array
460
     */
461 101
    private function allDefaults()
462
    {
463
        return [
464 101
            'wrapper' => ['class' => $this->getConfig('defaults.wrapper_class')],
465 101
            'attr' => ['class' => $this->getConfig('defaults.field_class')],
466 101
            'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [
467 101
                '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 101
            'label_attr' => ['class' => $this->getConfig('defaults.label_class')],
475 101
            '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 100
    public function getRealName()
487
    {
488 100
        return $this->getOption('real_name', $this->name);
489
    }
490
491
    /**
492
     * @param $value
493
     * @return $this
494
     */
495 94
    public function setValue($value)
496
    {
497 94
        if ($this->hasDefault) {
498 1
            return $this;
499
        }
500
501 94
        $closure = $this->valueClosure;
502
503 94
        if ($closure instanceof \Closure) {
504
            $value = $closure($value ?: null);
505
        }
506
507 94
        if (!$this->isValidValue($value)) {
508 91
            $value = $this->getOption($this->defaultValueProperty);
509
        }
510
511 94
        $this->options[$this->valueProperty] = $value;
512
513 94
        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 101
    }
525
526
    /**
527
     * Add error class to wrapper if validation errors exist.
528
     *
529
     * @return void
530
     */
531 101
    protected function addErrorClass()
532
    {
533 101
        $errors = $this->parent->getRequest()->session()->get('errors');
534 101
        $errorBag = $this->parent->getErrorBag();
535
536 101
        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 101
    }
554
555
    /**
556
     * Merge all defaults with field specific defaults and set template if passed.
557
     *
558
     * @param array $options
559
     */
560 101
    protected function setDefaultOptions(array $options = [])
561
    {
562 101
        $this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults());
563 101
        $this->options = $this->prepareOptions($options);
564
565 101
        $defaults = $this->setDefaultClasses($options);
566 101
        $this->options = $this->formHelper->mergeOptions($this->options, $defaults);
567
568 101
        $this->setupLabel();
569 101
    }
570
571
    /**
572
     * Creates default wrapper classes for the form element.
573
     *
574
     * @param array $options
575
     * @return array
576
     */
577 101
    protected function setDefaultClasses(array $options = [])
578
    {
579 101
        $wrapper_class = $this->getConfig('defaults.' . $this->type . '.wrapper_class', '');
580 101
        $label_class = $this->getConfig('defaults.' . $this->type . '.label_class', '');
581 101
        $field_class = $this->getConfig('defaults.' . $this->type . '.field_class', '');
582
583 101
        $defaults = [];
584 101
        if ($wrapper_class && !array_get($options, 'wrapper.class')) {
585
            $defaults['wrapper']['class'] = $wrapper_class;
586
        }
587 101
        if ($label_class && !array_get($options, 'label_attr.class')) {
588
            $defaults['label_attr']['class'] = $label_class;
589
        }
590 101
        if ($field_class && !array_get($options, 'attr.class')) {
591 1
            $defaults['attr']['class'] = $field_class;
592
        }
593 101
        return $defaults;
594
    }
595
596
    /**
597
     * Setup the label for the form field.
598
     *
599
     * @return void
600
     */
601 101
    protected function setupLabel()
602
    {
603 101
        if ($this->getOption('label') !== null) {
604 24
            return;
605
        }
606
607 99
        if ($template = $this->parent->getTranslationTemplate()) {
608 3
            $label = str_replace(
609 3
                ['{name}', '{type}'],
610 3
                [$this->getRealName(), 'label'],
611 3
                $template
612
            );
613 96
        } elseif ($langName = $this->parent->getLanguageName()) {
614 4
            $label = sprintf('%s.%s', $langName, $this->getRealName());
615
        } else {
616 93
            $label = $this->getRealName();
617
        }
618
619 99
        $this->setOption('label', $this->formHelper->formatLabel($label));
620 99
    }
621
622
    /**
623
     * Check if fields needs label.
624
     *
625
     * @return bool
626
     */
627 35
    protected function needsLabel()
628
    {
629
        // If field is <select> and child of choice, we don't need label for it
630 35
        $isChildSelect = $this->type == 'select' && $this->getOption('is_child') === true;
631
632 35
        if ($this->type == 'hidden' || $isChildSelect) {
633 11
            return false;
634
        }
635
636 31
        return true;
637
    }
638
639
    /**
640
     * Disable field.
641
     *
642
     * @return $this
643
     */
644 1
    public function disable()
645
    {
646 1
        $this->setOption('attr.disabled', 'disabled');
647
648 1
        return $this;
649
    }
650
651
    /**
652
     * Enable field.
653
     *
654
     * @return $this
655
     */
656 1
    public function enable()
657
    {
658 1
        array_forget($this->options, 'attr.disabled');
659
660 1
        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 9
    public function getValidationRules()
669
    {
670 9
        $rules = $this->getOption('rules', []);
671 9
        $name = $this->getNameKey();
672 9
        $messages = $this->getOption('error_messages', []);
673 9
        $formName = $this->formHelper->transformToDotSyntax($this->parent->getName());
674
675 9
        if ($messages && $formName) {
676 1
            $newMessages = [];
677 1
            foreach ($messages as $messageKey => $message) {
678 1
                $messageKey = sprintf('%s.%s', $formName, $messageKey);
679 1
                $newMessages[$messageKey] = $message;
680
            }
681 1
            $messages = $newMessages;
682
        }
683
684 9
        if (!$rules) {
685 2
            return [];
686
        }
687
688 8
        if (is_array($rules)) {
689 2
            $rules = array_map(function ($rule) use ($name) {
690 2
                if ($rule instanceof \Closure) {
691
                    return $rule($name);
692
                }
693
694 2
                return $rule;
695 2
            }, $rules);
696
        }
697
698
        return [
699 8
            'rules' => [$name => $rules],
700 8
            'attributes' => [$name => $this->getOption('label')],
701 8
            'error_messages' => $messages
702
        ];
703
    }
704
705
    /**
706
     * Get this field's attributes, probably just one.
707
     *
708
     * @return array
709
     */
710 3
    public function getAllAttributes()
711
    {
712 3
        return [$this->getNameKey()];
713
    }
714
715
    /**
716
     * Get value property.
717
     *
718
     * @param mixed|null $default
719
     * @return mixed
720
     */
721 38
    public function getValue($default = null)
722
    {
723 38
        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 35
    public function getDefaultValue($default = null)
733
    {
734 35
        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 94
    protected function isValidValue($value)
743
    {
744 94
        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 96
    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 96
        $overrideStatus = $this->getOption('filters_override', false);
760 96
        if ($overrideStatus) {
761 2
            $this->setFiltersOverride(true);
762
        }
763
764
        // Get filters and bind it to field.
765 96
        $filters = $this->getOption('filters', []);
766 96
        foreach ($filters as $filter) {
767 8
            $this->addFilter($filter);
768
        }
769
770 96
        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)
781
    {
782
        $this->clearFilters();
783
        foreach ($filters as $filter) {
784
            $this->addFilter($filter);
785
        }
786
787
        return $this;
788
    }
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 23
    public function getFilters()
797
    {
798 23
        return $this->filters;
799
    }
800
801
    /**
802
     * @param  string|FilterInterface $filter
803
     *
804
     * @return \Kris\LaravelFormBuilder\Fields\FormField
805
     *
806
     * @throws FilterAlreadyBindedException
807
     */
808 8
    public function addFilter($filter)
809
    {
810
        // Resolve filter object from string/object or throw Ex.
811 8
        $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 8
        if ($this->getFiltersOverride()) {
816 1
            if ($key = array_search($filterObj->getName(), $this->getFilters())) {
817
                $this->filters[$key] = $filterObj;
818
            } else {
819 1
                $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 7
            if (array_key_exists($filterObj->getName(), $this->getFilters())) {
825 1
                $ex = new FilterAlreadyBindedException($filterObj->getName(), $this->getName());
826 1
                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 7
            $this->filters[$filterObj->getName()] = $filterObj;
832
        }
833
834 8
        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 1
    public function removeFilter($name)
845
    {
846 1
        $filters = $this->getFilters();
847 1
        if (array_key_exists($name, $filters)) {
848 1
            unset($filters[$name]);
849 1
            $this->filters = $filters;
850
        }
851
852 1
        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 1
    public function removeFilters(array $filterNames)
863
    {
864 1
        $filters = $this->getFilters();
865 1
        foreach ($filterNames as $filterName) {
866 1
            if (array_key_exists($filterName, $filters)) {
867 1
                unset($filters[$filterName]);
868 1
                $this->filters = $filters;
869
            }
870
        }
871
872 1
        return $this;
873
    }
874
875
    /**
876
     * Method clearFilters used to empty current filters property.
877
     *
878
     * @return \Kris\LaravelFormBuilder\Fields\FormField
879
     */
880 1
    public function clearFilters()
881
    {
882 1
        $this->filters = [];
883 1
        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 2
    public function setFiltersOverride($status)
894
    {
895 2
        $this->filtersOverride = $status;
896 2
        return $this;
897
    }
898
899
    /**
900
     * @return bool
901
     */
902 9
    public function getFiltersOverride()
903
    {
904 9
        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 1
    public function setRawValue($value)
916
    {
917 1
        $this->rawValue = $value;
918 1
        return $this;
919
    }
920
921
    /**
922
     * Returns unfiltered raw value of field.
923
     *
924
     * @return mixed
925
     */
926
    public function getRawValue()
927
    {
928
        return $this->rawValue;
929
    }
930
931
    /**
932
     * Get config from the form.
933
     *
934
     * @return mixed
935
     */
936 101
    private function getConfig($key = null, $default = null)
937
    {
938 101
        return $this->parent->getConfig($key, $default);
939
    }
940
}
941