Completed
Push — master ( 60ac2f...695cd7 )
by Denis
01:32
created

Select::afterElementPrepareToRender()   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
    protected $valueAsAttribute = false;
11
12
    public function __construct(?string $name = null, ?string $value = null, ?iterable $options = null)
13
    {
14
        parent::__construct();
15
16
        $this->name($name);
17
18
        $this->selected($value);
19
20
        if(!empty($options)) {
21
            $isIndexed = array_keys($options) === range(0, count($options) - 1);
22
23
            $opt = '';
24
            foreach($options as $key => $val) {
25
                $selected = ($isIndexed ? $val : $key) == $value ? ' selected="selected"' : '';
26
                $opt .= '<option value="' . ($isIndexed ? $val : $key) . '"' . $selected . '>' . $val . '</option>';
27
            }
28
29
            $this->value($opt);
30
        }
31
    }
32
33
    public function afterElementPrepareToRender(): void
34
    {
35
        $this->parts->put('value', null);
36
    }
37
38
    public function selected($value = null)
39
    {
40
        $this->selected = $value;
41
    }
42
43
    public function tag(): void
44
    {
45
        $this->tag = 'select';
46
    }
47
48
    public function getTagHtml()
49
    {
50
        return '<' . $this->tag . '**attributes**>' . $this->value . '</' . $this->tag . '>';
51
    }
52
53
}