Passed
Push — master ( 3c0766...793f0f )
by Chris
05:28
created

Select::renderHtmlMarkup()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 25
ccs 0
cts 14
cp 0
crap 20
rs 9.8333
1
<?php
2
3
namespace WebTheory\Saveyour\Fields;
4
5
use WebTheory\Html\AbstractHtmlElement;
6
use WebTheory\Saveyour\Concerns\IsSelectionFieldTrait;
7
use WebTheory\Saveyour\Contracts\FormFieldInterface;
8
9
class Select extends AbstractStandardFormControl implements FormFieldInterface
10
{
11
    use IsSelectionFieldTrait;
12
13
    /**
14
     *
15
     */
16
    public $options = [];
17
18
    /**
19
     *
20
     */
21
    public $value = [];
22
23
    /**
24
     * @var bool
25
     */
26
    public $multiple = false;
27
28
    /**
29
     * @var int
30
     */
31
    public $size;
32
33
    /**
34
     * Get the value of options
35
     *
36
     * @return mixed
37
     */
38
    public function getOptions()
39
    {
40
        return $this->options;
41
    }
42
43
    /**
44
     * Set the value of options
45
     *
46
     * @param mixed $options
47
     *
48
     * @return self
49
     */
50
    public function setOptions($options)
51
    {
52
        $this->options = $options;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Set the value of value
59
     *
60
     * @param mixed $value
61
     *
62
     * @return self
63
     */
64
    public function setValue($value)
65
    {
66
        $this->value[] = $value;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Get the value of multiple
73
     *
74
     * @return bool
75
     */
76
    public function isMultiple(): bool
77
    {
78
        return $this->multiple;
79
    }
80
81
    /**
82
     * Set the value of multiple
83
     *
84
     * @param bool $multiple
85
     *
86
     * @return self
87
     */
88
    public function setMultiple(bool $multiple)
89
    {
90
        $this->multiple = $multiple;
91
92
        return $this;
93
    }
94
95
    /**
96
     *
97
     */
98
    protected function resolveAttributes(): AbstractHtmlElement
99
    {
100
        return parent::resolveAttributes()
101
            ->addAttribute('multiple', $this->multiple);
0 ignored issues
show
Bug introduced by
'multiple' of type string is incompatible with the type array expected by parameter $attribute of WebTheory\Html\AbstractHtmlElement::addAttribute(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
            ->addAttribute(/** @scrutinizer ignore-type */ 'multiple', $this->multiple);
Loading history...
102
    }
103
104
    /**
105
     *
106
     */
107
    protected function getItemsToRender(): array
108
    {
109
        return $this->selectionProvider
110
            ? $this->selectionProvider->getSelection()
111
            : $this->options;
112
    }
113
114
    /**
115
     *
116
     */
117
    public function renderHtmlMarkup(): string
118
    {
119
        $html = '';
120
121
        $html .= $this->open('select', $this->attributes);
122
123
        if (!empty($this->placeholder)) {
124
            $html .= $this->open('option') . $this->placeholder . $this->close('option');
125
        }
126
127
        foreach ($this->getItemsToRender() as $value => $option) {
128
            $optionAttr = ['value' => $value];
129
130
            if (in_array($value, $this->value)) {
131
                $optionAttr['selected'] = true;
132
            }
133
134
            $html .= $this->open('option', $optionAttr);
135
            $html .= $option;
136
            $html .= $this->close('option');
137
        }
138
139
        $html .= $this->close('select');
140
141
        return $html;
142
    }
143
}
144