Completed
Push — master ( 839e9e...4e091b )
by Costin
03:53
created

Checkbox::setSpecificAttributes()   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 View Code Duplication
    protected function addClasses()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        // Specific to Bootstrap, will need to abstract it
44
        $this->class[] = 'custom-control-input';
45
        $this->labelClass[] = 'custom-control-label';
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.styles.field.error');
51
        }
52
53
    }
54
}