Passed
Push — master ( d5d794...b1bf4d )
by Pascal
12:05
created

FormRadio::generateIdByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFormComponents\Components;
4
5
class FormRadio 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
    public function __construct(
16
        string $name,
17
        string $label = '',
18
        $value = 1,
19
        $bind = null,
20
        bool $default = false,
21
        bool $showErrors = false
22
    ) {
23
        $this->name       = $name;
24
        $this->label      = $label;
25
        $this->value      = $value;
26
        $this->showErrors = $showErrors;
27
28
        $inputName = static::convertBracketsToDots($name);
29
30
        if (old($inputName) !== null) {
31
            $this->checked = old($inputName) == $value;
32
        }
33
34
        if (!session()->hasOldInput() && $this->isNotWired()) {
35
            $boundValue = $this->getBoundValue($bind, $name);
36
37
            if (!is_null($boundValue)) {
38
                $this->checked = $boundValue == $this->value;
39
            } else {
40
                $this->checked = $default;
41
            }
42
        }
43
    }
44
45
    /**
46
     * Generates an ID by the name and value attributes.
47
     *
48
     * @return string
49
     */
50
    protected function generateIdByName(): string
51
    {
52
        return "auto_id_" . $this->name . "_" . $this->value;
53
    }
54
}
55