Completed
Push — master ( 6dd28e...5c063d )
by Kristijan
04:11 queued 10s
created

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