|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phalcon\Html\Helper\Input; |
|
13
|
|
|
|
|
14
|
|
|
use Phalcon\Html\Helper\AbstractList; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Select |
|
18
|
|
|
* |
|
19
|
|
|
* @property string $elementTag |
|
20
|
|
|
* @property bool $inOptGroup |
|
21
|
|
|
* @property string $selected |
|
22
|
|
|
*/ |
|
23
|
|
|
class Select extends AbstractList |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $elementTag = "option"; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $inOptGroup = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $selected = ""; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Add an element to the list |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $text |
|
44
|
|
|
* @param string|null $value |
|
45
|
|
|
* @param array $attributes |
|
46
|
|
|
* @param bool $raw |
|
47
|
|
|
* |
|
48
|
|
|
* @return Select |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function add( |
|
51
|
|
|
string $text, |
|
52
|
|
|
string $value = null, |
|
53
|
|
|
array $attributes = [], |
|
54
|
|
|
bool $raw = false |
|
55
|
|
|
): Select { |
|
56
|
1 |
|
$attributes = $this->processValue($attributes, $value); |
|
57
|
|
|
|
|
58
|
1 |
|
$this->store[] = [ |
|
59
|
1 |
|
"renderFullElement", |
|
60
|
|
|
[ |
|
61
|
1 |
|
$this->elementTag, |
|
62
|
1 |
|
$text, |
|
63
|
1 |
|
$attributes, |
|
64
|
1 |
|
$raw, |
|
65
|
|
|
], |
|
66
|
1 |
|
$this->indent(), |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add an element to the list |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $text |
|
76
|
|
|
* @param string $value |
|
77
|
|
|
* @param array $attributes |
|
78
|
|
|
* @param bool $raw |
|
79
|
|
|
* |
|
80
|
|
|
* @return Select |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function addPlaceholder( |
|
83
|
|
|
string $text, |
|
84
|
|
|
string $value = null, |
|
85
|
|
|
array $attributes = [], |
|
86
|
|
|
bool $raw = false |
|
87
|
|
|
): Select { |
|
88
|
1 |
|
if (null !== $value) { |
|
89
|
1 |
|
$attributes["value"] = $value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$this->store[] = [ |
|
93
|
1 |
|
"renderFullElement", |
|
94
|
|
|
[ |
|
95
|
1 |
|
$this->elementTag, |
|
96
|
1 |
|
$text, |
|
97
|
1 |
|
$attributes, |
|
98
|
1 |
|
$raw, |
|
99
|
|
|
], |
|
100
|
1 |
|
$this->indent(), |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Creates an option group |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $label |
|
110
|
|
|
* @param array $attributes |
|
111
|
|
|
* |
|
112
|
|
|
* @return Select |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function optGroup(string $label = null, array $attributes = []): Select |
|
115
|
|
|
{ |
|
116
|
1 |
|
if (!$this->inOptGroup) { |
|
117
|
1 |
|
$this->store[] = [ |
|
118
|
1 |
|
"optGroupStart", |
|
119
|
|
|
[ |
|
120
|
1 |
|
$label, |
|
121
|
1 |
|
$attributes, |
|
122
|
|
|
], |
|
123
|
1 |
|
$this->indent(), |
|
124
|
|
|
]; |
|
125
|
1 |
|
$this->indentLevel += 1; |
|
126
|
|
|
} else { |
|
127
|
1 |
|
$this->indentLevel -= 1; |
|
128
|
1 |
|
$this->store[] = [ |
|
129
|
1 |
|
"optGroupEnd", |
|
130
|
|
|
[], |
|
131
|
1 |
|
$this->indent(), |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
$this->inOptGroup = !$this->inOptGroup; |
|
136
|
|
|
|
|
137
|
1 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $selected |
|
142
|
|
|
* |
|
143
|
|
|
* @return Select |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function selected(string $selected): Select |
|
146
|
|
|
{ |
|
147
|
1 |
|
$this->selected = $selected; |
|
148
|
|
|
|
|
149
|
1 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
1 |
|
protected function getTag(): string |
|
156
|
|
|
{ |
|
157
|
1 |
|
return "select"; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
protected function optGroupEnd(): string |
|
161
|
|
|
{ |
|
162
|
1 |
|
return "</optgroup>"; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $label |
|
167
|
|
|
* @param array $attributes |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
1 |
|
protected function optGroupStart(string $label, array $attributes): string |
|
172
|
|
|
{ |
|
173
|
1 |
|
$attributes["label"] = $label; |
|
174
|
|
|
|
|
175
|
1 |
|
return $this->renderTag("optgroup", $attributes); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Checks if the value has been passed and if it is the same as the |
|
180
|
|
|
* value stored in the object |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $attributes |
|
183
|
|
|
* @param string $value |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
1 |
|
private function processValue(array $attributes, string $value = null): array |
|
188
|
|
|
{ |
|
189
|
1 |
|
if (null !== $value) { |
|
190
|
1 |
|
$attributes["value"] = $value; |
|
191
|
1 |
|
if (!empty($this->selected) && $value === $this->selected) { |
|
192
|
1 |
|
$attributes["selected"] = "selected"; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
1 |
|
return $attributes; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|