Passed
Push — master ( a90def...5de986 )
by Chris
04:49
created

Select::getGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebTheory\Saveyour\Fields;
4
5
use WebTheory\Html\AbstractHtmlElement;
6
use WebTheory\Saveyour\Concerns\MultiValueSelectionTrait;
7
use WebTheory\Saveyour\Concerns\RendersOptionsTrait;
8
use WebTheory\Saveyour\Contracts\FormFieldInterface;
9
use WebTheory\Saveyour\Elements\OptGroup;
10
use WebTheory\Saveyour\Elements\Option;
11
12
class Select extends AbstractStandardFormControl implements FormFieldInterface
13
{
14
    use MultiValueSelectionTrait;
15
    use RendersOptionsTrait {
16
        renderSelection as renderSelectionFromProvider;
17
    }
18
19
    /**
20
     *
21
     */
22
    protected $value = [];
23
24
    /**
25
     * @var OptGroup[]
26
     */
27
    protected $groups = [];
28
29
    /**
30
     * @var bool
31
     */
32
    protected $multiple = false;
33
34
    /**
35
     * @var int
36
     */
37
    protected $size;
38
39
    /**
40
     *
41
     */
42
    public function getGroups()
43
    {
44
        return $this->groups;
45
    }
46
47
    /**
48
     *
49
     */
50
    public function setGroups(OptGroup ...$optGroups)
51
    {
52
        $this->groups = $optGroups;
53
    }
54
55
    /**
56
     *
57
     */
58
    public function addGroup(OptGroup $optGroup)
59
    {
60
        $this->groups[] = $optGroup;
61
    }
62
63
    /**
64
     * Get the value of multiple
65
     *
66
     * @return bool
67
     */
68
    public function isMultiple(): bool
69
    {
70
        return $this->multiple;
71
    }
72
73
    /**
74
     * Set the value of multiple
75
     *
76
     * @param bool $multiple
77
     *
78
     * @return self
79
     */
80
    public function setMultiple(bool $multiple)
81
    {
82
        $this->multiple = $multiple;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Get the value of size
89
     *
90
     * @return int
91
     */
92
    public function getSize(): int
93
    {
94
        return $this->size;
95
    }
96
97
    /**
98
     * Set the value of size
99
     *
100
     * @param int $size
101
     *
102
     * @return self
103
     */
104
    public function setSize(int $size)
105
    {
106
        $this->size = $size;
107
108
        return $this;
109
    }
110
111
    /**
112
     *
113
     */
114
    protected function resolveAttributes(): AbstractHtmlElement
115
    {
116
        return parent::resolveAttributes()
117
            ->addAttribute('name', $this->name . ($this->multiple ? '[]' : ''))
0 ignored issues
show
Bug introduced by
'name' 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

117
            ->addAttribute(/** @scrutinizer ignore-type */ 'name', $this->name . ($this->multiple ? '[]' : ''))
Loading history...
118
            ->addAttribute('multiple', $this->multiple)
119
            ->addAttribute('size', $this->size);
120
    }
121
122
    /**
123
     *
124
     */
125
    protected function renderHtmlMarkup(): string
126
    {
127
        return $this->tag('select', $this->renderOptions(), $this->attributes);
128
    }
129
130
    /**
131
     *
132
     */
133
    protected function renderOptions()
134
    {
135
        $html = '';
136
137
        if (!empty($this->placeholder)) {
138
            $html .= $this->createPlaceholder();
139
        }
140
141
        $html .= $this->renderSelection();
142
143
        return $html;
144
    }
145
146
    /**
147
     *
148
     */
149
    protected function createPlaceholder(): Option
150
    {
151
        return new Option($this->placeholder, '');
152
    }
153
154
    /**
155
     *
156
     */
157
    protected function renderSelection(): string
158
    {
159
        return empty($this->groups)
160
            ? $this->renderSelectionFromProvider()
161
            : $this->renderSelectionFromOptGroups();
162
    }
163
164
    /**
165
     *
166
     */
167
    protected function renderSelectionFromOptGroups(): string
168
    {
169
        $html = '';
170
171
        foreach ($this->groups as $optgroup) {
172
            $html .= $optgroup->setValue($this->value);
173
        }
174
175
        return $html;
176
    }
177
}
178