Completed
Push — master ( b5f959...da0e5f )
by Costin
02:20
created

Checkbox::attributesList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
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
        $this->addClasses();
28
    }
29
30
    protected function setChecked()
31
    {
32
        if ($this->value) {
33
            $this->checked = true;
34
        }
35
    }
36
37
    protected function overwriteValue()
38
    {
39
        // Overwrite the value to 1
40
        $this->value = 1;
41
    }
42
43
    protected function setDefaultClass()
44
    {
45
        // Specific to Bootstrap, will need to abstract it
46
        $this->class[] = 'custom-control-input';
47
        $this->labelClass[] = 'custom-control-label';
48
    }
49
50
    protected function addClasses()
51
    {
52
        // Attach the error class if an error is displayed against this field
53
        $errors = session()->get('errors', app(ViewErrorBag::class));
54
        if ($errors->has($this->name)) {
55
            $this->labelClass[] = config('blade-form-components.styles.field.error');
56
        }
57
    }
58
}
59