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

Select   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 10
lcom 3
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 20 6
A afterElementPrepareToRender() 0 4 1
A selected() 0 4 1
A tag() 0 4 1
A getTagHtml() 0 4 1
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
}