1 | <?php |
||
18 | class Select extends AbstractInput |
||
19 | { |
||
20 | /** |
||
21 | * |
||
22 | * A stack of HTML pieces for the select. |
||
23 | * |
||
24 | * @var array |
||
25 | * |
||
26 | */ |
||
27 | protected $stack = array(); |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * Are we currently processing an optgroup? |
||
32 | * |
||
33 | * @var bool |
||
34 | * |
||
35 | */ |
||
36 | protected $optgroup = false; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * The current option indent level. |
||
41 | * |
||
42 | * @var int |
||
43 | * |
||
44 | */ |
||
45 | protected $optlevel = 1; |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | * The value of the 'placeholder' pseudo-attribute. |
||
50 | * |
||
51 | * @var mixed |
||
52 | * |
||
53 | */ |
||
54 | protected $placeholder; |
||
55 | |||
56 | /** |
||
57 | * |
||
58 | * Use strict equality when matching selected option values? |
||
59 | * |
||
60 | * @var bool |
||
61 | * |
||
62 | */ |
||
63 | protected $strict = false; |
||
64 | |||
65 | /** |
||
66 | * |
||
67 | * If a $spec is passed, returns a full select tag with options; if no $spec |
||
68 | * is passed, returns this helper object itself. |
||
69 | * |
||
70 | * @param array $spec A select input specfication. |
||
71 | * |
||
72 | * @return string|self |
||
73 | * |
||
74 | */ |
||
75 | 3 | public function __invoke(array $spec = null) |
|
76 | { |
||
77 | 3 | if ($spec !== null) { |
|
78 | $this->prep($spec); |
||
79 | $this->attribs($this->attribs); |
||
80 | $this->options($this->options); |
||
81 | $this->selected($this->value); |
||
82 | } |
||
83 | |||
84 | 3 | return $this; |
|
85 | 3 | } |
|
86 | |||
87 | /** |
||
88 | * |
||
89 | * Returns a select tag with options. |
||
90 | * |
||
91 | * @return string |
||
92 | * |
||
93 | */ |
||
94 | 3 | public function __toString() |
|
95 | { |
||
96 | // build the html |
||
97 | 3 | $html = $this->buildSelect() |
|
98 | . $this->buildOptionPlaceholder() |
||
99 | . $this->buildOptions() |
||
100 | . $this->indent(0, '</select>'); |
||
101 | |||
102 | // reset for next time |
||
103 | 3 | $this->stack = array(); |
|
104 | 3 | $this->optgroup = false; |
|
105 | 3 | $this->optlevel = 1; |
|
106 | 3 | $this->placeholder = null; |
|
107 | 3 | $this->strict = false; |
|
108 | |||
109 | // done! |
||
110 | 3 | return $html; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * |
||
115 | * Sets the HTML attributes for the select tag. |
||
116 | * |
||
117 | * @param array $attribs The attribues to set. |
||
118 | * |
||
119 | * @return string |
||
120 | * |
||
121 | */ |
||
122 | 3 | public function attribs(array $attribs) |
|
123 | { |
||
124 | 3 | $this->attribs = $attribs; |
|
125 | 3 | if (isset($this->attribs['placeholder'])) { |
|
126 | $this->placeholder($this->attribs['placeholder']); |
||
127 | 1 | unset($this->attribs['placeholder']); |
|
128 | } |
||
129 | 3 | if (isset($this->attribs['strict'])) { |
|
130 | $this->strict($this->attribs['strict']); |
||
131 | 1 | unset($this->attribs['strict']); |
|
132 | } |
||
133 | 3 | return $this; |
|
134 | 3 | } |
|
135 | |||
136 | /** |
||
137 | * |
||
138 | * Adds a single option to the stack. |
||
139 | * |
||
140 | * @param string $value The option value. |
||
141 | * |
||
142 | * @param string $label The option label. |
||
143 | * |
||
144 | * @param array $attribs Attributes for the option. |
||
145 | * |
||
146 | * @return self |
||
147 | * |
||
148 | */ |
||
149 | public function option($value, $label, array $attribs = array()) |
||
150 | { |
||
151 | $this->stack[] = array('buildOption', $value, $label, $attribs); |
||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * |
||
157 | * Adds multiple options to the stack. |
||
158 | * |
||
159 | * @param array $options An array of options where the key is the option |
||
160 | * value, and the value is the option label. If the value is an array, |
||
161 | * the key is treated as a label for an optgroup, and the value is |
||
162 | * a sub-array of options. |
||
163 | * |
||
164 | * @param array $attribs Attributes to be used on each option. |
||
165 | * |
||
166 | * @return self |
||
167 | * |
||
168 | */ |
||
169 | 1 | public function options(array $options, array $attribs = array()) |
|
170 | { |
||
171 | // set the options and optgroups |
||
172 | foreach ($options as $key => $val) { |
||
173 | if (is_array($val)) { |
||
174 | // the key is an optgroup label |
||
175 | $this->optgroup($key); |
||
176 | // recursively descend into the array |
||
177 | $this->options($val, $attribs); |
||
178 | } else { |
||
179 | // the key is an option value and the val is an option label |
||
180 | $this->option($key, $val, $attribs); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | 1 | return $this; |
|
185 | 1 | } |
|
186 | |||
187 | /** |
||
188 | * |
||
189 | * Adds an optgroup input to the stack. |
||
190 | * |
||
191 | * @param string $label The optgroup label. |
||
192 | * |
||
193 | * @param array $attribs Attributes for the optgroup. |
||
194 | * |
||
195 | * @return self |
||
196 | * |
||
197 | */ |
||
198 | 2 | public function optgroup($label, array $attribs = array()) |
|
199 | { |
||
200 | if ($this->optgroup) { |
||
201 | 2 | $this->stack[] = array('endOptgroup'); |
|
202 | } |
||
203 | $this->stack[] = array('beginOptgroup', $label, $attribs); |
||
204 | $this->optgroup = true; |
||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * |
||
210 | * Sets the selected value(s). |
||
211 | * |
||
212 | * @param mixed $selected The selected value(s). |
||
213 | * |
||
214 | * @return self |
||
215 | * |
||
216 | */ |
||
217 | 3 | public function selected($selected) |
|
222 | |||
223 | /** |
||
224 | * |
||
225 | * Sets the text for a placeholder option. |
||
226 | * |
||
227 | * @param string $placeholder The placeholder text. |
||
228 | * |
||
229 | * @return self |
||
230 | * |
||
231 | */ |
||
232 | 1 | public function placeholder($placeholder) |
|
237 | |||
238 | /** |
||
239 | * |
||
240 | * Use strict equality when matching selected option values? |
||
241 | * |
||
242 | * @param bool $strict True for strict equality, false for loose equality. |
||
243 | * |
||
244 | * @return self |
||
245 | * |
||
246 | * @see buildOption() |
||
247 | * |
||
248 | */ |
||
249 | 1 | public function strict($strict = true) |
|
254 | |||
255 | /** |
||
256 | * |
||
257 | * Builds the opening select tag. |
||
258 | * |
||
259 | * @return string |
||
260 | * |
||
261 | */ |
||
262 | 3 | protected function buildSelect() |
|
277 | |||
278 | /** |
||
279 | * |
||
280 | * Builds the 'placeholder' option (if any). |
||
281 | * |
||
282 | * @return string |
||
283 | * |
||
284 | */ |
||
285 | 3 | protected function buildOptionPlaceholder() |
|
295 | |||
296 | /** |
||
297 | * |
||
298 | * Builds the collection of option tags. |
||
299 | * |
||
300 | * @return string |
||
301 | * |
||
302 | */ |
||
303 | 3 | protected function buildOptions() |
|
319 | |||
320 | /** |
||
321 | * |
||
322 | * Builds the HTML for a single option. |
||
323 | * |
||
324 | * @param array $info The option info. |
||
325 | * |
||
326 | * @return string |
||
327 | * |
||
328 | */ |
||
329 | 2 | protected function buildOption($info) |
|
348 | |||
349 | /** |
||
350 | * |
||
351 | * Builds the HTML to begin an optgroup. |
||
352 | * |
||
353 | * @param array $info The optgroup info. |
||
354 | * |
||
355 | * @return null |
||
356 | * |
||
357 | */ |
||
358 | protected function beginOptgroup($info) |
||
366 | |||
367 | /** |
||
368 | * |
||
369 | * Builds the HTML to end an optgroup. |
||
370 | * |
||
371 | * @return null |
||
372 | * |
||
373 | */ |
||
374 | protected function endOptgroup() |
||
379 | } |
||
380 |