Passed
Push — master ( bd59c3...bb2815 )
by Pascal
05:04 queued 03:02
created

FormSelect::isSelected()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 4
nop 1
1
<?php
2
3
namespace ProtoneMedia\LaravelFormComponents\Components;
4
5
class FormSelect extends Component
6
{
7
    use HandlesValidationErrors;
8
    use HandlesBoundValues;
9
10
    public string $name;
11
    public string $label;
12
    public $options;
13
    public $selectedKey;
14
    public bool $multiple;
15
16
    /**
17
     * Create a new component instance.
18
     *
19
     * @return void
20
     */
21
    public function __construct(
22
        string $name,
23
        string $label = '',
24
        $options = [],
25
        $bind = null,
26
        $default = null,
27
        bool $multiple = false,
28
        bool $showErrors = true
29
    ) {
30
        $this->name    = $name;
31
        $this->label   = $label;
32
        $this->options = $options;
33
34
        if ($this->isNotWired()) {
35
            $default = $this->getBoundValue($bind, $name) ?: $default;
36
37
            $this->selectedKey = old($name, $default);
38
        }
39
40
        $this->multiple   = $multiple;
41
        $this->showErrors = $showErrors;
42
    }
43
44
    public function isSelected($key): bool
45
    {
46
        if ($this->isWired()) {
47
            return false;
48
        }
49
50
        if ($this->selectedKey === $key) {
51
            return true;
52
        }
53
54
        if (is_array($this->selectedKey) && in_array($key, $this->selectedKey)) {
55
            return true;
56
        }
57
58
        return false;
59
    }
60
}
61