Select   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 12
eloc 23
c 4
b 1
f 1
dl 0
loc 61
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A attributesList() 0 4 1
A setNulloption() 0 3 1
A setStyles() 0 4 1
A setSpecificAttributes() 0 5 1
B setOptions() 0 13 7
A setMultiple() 0 3 1
1
<?php
2
3
namespace SoareCostin\BladeFormComponents\FormElements;
4
5
use SoareCostin\BladeFormComponents\FormElement;
6
7
class Select extends FormElement
8
{
9
    /** @var array */
10
    public $options = [];
11
12
    /** @var bool */
13
    public $multiple = false;
14
15
    /** @var string */
16
    public $nulloption;
17
18
    /** @var array */
19
    public $selected = [];
20
21
    protected function attributesList()
22
    {
23
        return [
24
            'id', 'name', 'class', 'multiple', 'required', 'disabled', 'readonly', 'autocomplete',
25
        ];
26
    }
27
28
    protected function setSpecificAttributes()
29
    {
30
        $this->setOptions();
31
        $this->setNulloption();
32
        $this->setMultiple();
33
    }
34
35
    protected function setOptions()
36
    {
37
        if (isset($this->params['options']) && ! empty($this->params['options'])) {
38
            $this->options = $this->params['options'];
39
40
            foreach ($this->options as $value => $label) {
41
                if (is_array($this->value)) {
42
                    if (in_array($value, $this->value)) {
43
                        $this->selected[] = $value;
44
                    }
45
                } else {
46
                    if ($value == $this->value) {
47
                        $this->selected[] = $value;
48
                    }
49
                }
50
            }
51
        }
52
    }
53
54
    protected function setNulloption()
55
    {
56
        $this->nulloption = $this->params->get('nulloption');
57
    }
58
59
    protected function setMultiple()
60
    {
61
        $this->multiple = $this->params->get('multiple');
62
    }
63
64
    protected function setStyles()
65
    {
66
        $this->class[] = config('blade-form-components.themes.'.$this->getTheme().'.fields.select') ?? config('blade-form-components.themes.'.$this->getTheme().'.fields.default');
67
        $this->labelClass[] = config('blade-form-components.themes.'.$this->getTheme().'.labels.default');
68
    }
69
}
70