Completed
Push — master ( d1a379...3c62fa )
by Costin
01:22
created

Checkbox::setDefaultClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
    /** @var array */
17
    public $attributesList = [
18
        'id', 'name', 'type', 'value', 'class', 'checked', 'required', 'disabled', 'readonly', 'autocomplete',
19
    ];
20
21
    protected function setSpecificAttributes()
22
    {
23
        $this->setChecked();
24
        $this->overwriteValue();
25
        $this->addClasses();
26
    }
27
28
    protected function setChecked()
29
    {
30
        if ($this->value) {
31
            $this->checked = true;
32
        }
33
    }
34
35
    protected function overwriteValue()
36
    {
37
        // Overwrite the value to 1
38
        $this->value = 1;
39
    }
40
41
    protected function setDefaultClass()
42
    {
43
        // Specific to Bootstrap, will need to abstract it
44
        $this->class[] = 'custom-control-input';
45
        $this->labelClass[] = 'custom-control-label';
46
    }
47
48
    protected function addClasses()
49
    {
50
        // Attach the error class if an error is displayed against this field
51
        $errors = session()->get('errors', app(ViewErrorBag::class));
52
        if ($errors->has($this->name)) {
53
            $this->labelClass[] = config('blade-form-components.styles.field.error');
54
        }
55
    }
56
}
57