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

Checkbox   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 49
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setSpecificAttributes() 0 6 1
A setChecked() 0 6 2
A overwriteValue() 0 5 1
A setDefaultClass() 0 6 1
A addClasses() 0 8 2
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