Completed
Push — master ( ed22ad...b8c58a )
by Rudie
12s queued 10s
created

FormField::allDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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