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
|
|
|
* Value for hidden input that facilitates removing all values on the server |
41
|
|
|
* if no values are selected in the form. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $clearControl = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
public function getGroups() |
51
|
|
|
{ |
52
|
|
|
return $this->groups; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
public function setGroups(OptGroup ...$optGroups) |
59
|
|
|
{ |
60
|
|
|
$this->groups = $optGroups; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
public function addGroup(OptGroup $optGroup) |
67
|
|
|
{ |
68
|
|
|
$this->groups[] = $optGroup; |
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
|
|
|
* Get the value of size |
97
|
|
|
* |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
public function getSize(): int |
101
|
|
|
{ |
102
|
|
|
return $this->size; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the value of size |
107
|
|
|
* |
108
|
|
|
* @param int $size |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
|
|
public function setSize(int $size) |
113
|
|
|
{ |
114
|
|
|
$this->size = $size; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
|
|
protected function resolveAttributes(): AbstractHtmlElement |
123
|
|
|
{ |
124
|
|
|
return parent::resolveAttributes() |
125
|
|
|
->addAttribute('name', $this->resolveNameAttribute()) |
|
|
|
|
126
|
|
|
->addAttribute('multiple', $this->multiple) |
127
|
|
|
->addAttribute('size', $this->size); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
*/ |
133
|
|
|
protected function resolveNameAttribute() |
134
|
|
|
{ |
135
|
|
|
return $this->name . ($this->multiple ? '[]' : ''); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
*/ |
141
|
|
|
protected function renderHtmlMarkup(): string |
142
|
|
|
{ |
143
|
|
|
return $this->tag('select', $this->attributes, $this->renderOptions()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* |
148
|
|
|
*/ |
149
|
|
|
protected function renderOptions() |
150
|
|
|
{ |
151
|
|
|
$html = ''; |
152
|
|
|
|
153
|
|
|
if ($this->multiple) { |
154
|
|
|
$html .= (new Option('', $this->clearControl)) |
155
|
|
|
->setSelected(true) |
156
|
|
|
->addAttribute('hidden', true); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (!empty($this->placeholder)) { |
160
|
|
|
$html .= $this->createPlaceholder(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$html .= $this->renderSelection(); |
164
|
|
|
|
165
|
|
|
return $html; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* |
170
|
|
|
*/ |
171
|
|
|
protected function createPlaceholder(): Option |
172
|
|
|
{ |
173
|
|
|
return new Option($this->placeholder, ''); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* |
178
|
|
|
*/ |
179
|
|
|
protected function renderSelection(): string |
180
|
|
|
{ |
181
|
|
|
return empty($this->groups) |
182
|
|
|
? $this->renderSelectionFromProvider() |
183
|
|
|
: $this->renderSelectionFromOptGroups(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
*/ |
189
|
|
|
protected function renderSelectionFromOptGroups(): string |
190
|
|
|
{ |
191
|
|
|
$html = ''; |
192
|
|
|
|
193
|
|
|
foreach ($this->groups as $optgroup) { |
194
|
|
|
$html .= $optgroup->setValue($this->value); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $html; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|