Completed
Push — master ( 6a33ec...f7065a )
by Denis
01:24
created

Select::getTagHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ngtfkx\Laradeck\FormBuilder\Elements;
4
5
6
class Select extends AbstractElement
7
{
8
    protected $selected;
9
10
    public function __construct(?string $name = null, ?string $value = null, ?iterable $options = null)
11
    {
12
        parent::__construct();
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
31
    public function selected($value = null)
32
    {
33
        $this->selected = $value;
34
    }
35
36
    public function tag(): void
37
    {
38
        $this->tag = 'select';
39
    }
40
41
    public function getTagHtml()
42
    {
43
        return '<' . $this->tag . '**attributes**>' . $this->value . '</' . $this->tag . '>';
44
    }
45
46
}