Completed
Push — master ( 5e1312...160e4d )
by Denis
01:45
created

Select   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 20 6
A selected() 0 4 1
A tag() 0 4 1
1
<?php
2
3
namespace Ngtfkx\Laradeck\FormBuilder\Elements;
4
5
6
class Select extends AbstractElement
7
{
8
    protected $needClose = true;
9
10
    protected $selected;
11
12
    public function __construct(?string $name = null, ?string $value = null, ?iterable $options = null)
13
    {
14
        $this->name($name);
15
16
        $this->selected($value);
17
18
        if(!empty($options)) {
19
            $isIndexed = array_keys($options) === range(0, count($options) - 1);
20
21
            $opt = '';
22
            foreach($options as $key => $val) {
23
                $selected = ($isIndexed ? $val : $key) == $value ? ' selected="selected"' : '';
24
                $opt .= '<option value="' . ($isIndexed ? $val : $key) . '"' . $selected . '>' . $val . '</option>';
25
            }
26
27
            $this->value($opt);
28
        }
29
30
        parent::__construct();
31
    }
32
33
    public function selected($value = null)
34
    {
35
        $this->selected = $value;
36
    }
37
38
    public function tag(): void
39
    {
40
        $this->tag = 'select';
41
    }
42
43
}