Passed
Pull Request — master (#8)
by Pascal
03:05
created

FormCheckbox::__construct()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
rs 9.6111
cc 5
nc 6
nop 6
1
<?php
2
3
namespace ProtoneMedia\LaravelFormComponents\Components;
4
5
class FormCheckbox extends Component
6
{
7
    use HandlesValidationErrors;
8
    use HandlesBoundValues;
9
10
    public string $name;
11
    public string $label;
12
    public $value;
13
    public bool $checked = false;
14
15
    /**
16
     * Create a new component instance.
17
     *
18
     * @return void
19
     */
20
    public function __construct(
21
        string $name,
22
        string $label = '',
23
        $value = 1,
24
        $bind = null,
25
        bool $default = false,
26
        bool $showErrors = true
27
    ) {
28
        $this->name       = $name;
29
        $this->label      = $label;
30
        $this->value      = $value;
31
        $this->showErrors = $showErrors;
32
33
        if (old($name)) {
34
            $this->checked = true;
35
        }
36
37
        if (!session()->hasOldInput() && $this->isNotWired()) {
38
            $boundValue = $this->getBoundValue($bind, $name);
39
40
            $this->checked = is_null($boundValue) ? $default : $boundValue;
41
        }
42
    }
43
}
44