Passed
Push — master ( 567445...4ac224 )
by Costin
02:45
created

Checkbox::setStyles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace SoareCostin\BladeFormComponents\FormElements;
4
5
use Illuminate\Support\ViewErrorBag;
6
use SoareCostin\BladeFormComponents\FormElement;
7
8
class Checkbox extends FormElement
9
{
10
    /** @var string */
11
    public $type = 'checkbox';
12
13
    /** @var bool */
14
    public $checked = false;
15
16
    protected function attributesList()
17
    {
18
        return [
19
            'id', 'name', 'type', 'value', 'class', 'checked', 'required', 'disabled', 'readonly', 'autocomplete',
20
        ];
21
    }
22
23
    protected function setSpecificAttributes()
24
    {
25
        $this->setChecked();
26
        $this->overwriteValue();
27
    }
28
29
    protected function setChecked()
30
    {
31
        if ($this->value) {
32
            $this->checked = true;
33
        }
34
    }
35
36
    protected function overwriteValue()
37
    {
38
        // Overwrite the value to 1
39
        $this->value = 1;
40
    }
41
42
    protected function setStyles()
43
    {
44
        $this->class[] = config('blade-form-components.themes.'.$this->getTheme().'.fields.checkbox') ?? config('blade-form-components.themes.'.$this->getTheme().'.fields.default');
45
        $this->labelClass[] = config('blade-form-components.themes.'.$this->getTheme().'.labels.checkbox');
46
47
        // Attach the error class if an error is displayed against this field
48
        $errors = session()->get('errors', app(ViewErrorBag::class));
49
        if ($errors->has($this->name)) {
50
            $this->labelClass[] = config('blade-form-components.themes.'.$this->getTheme().'.fields.is-error');
51
        }
52
    }
53
}
54