1
|
|
|
<?php /** MicroHtml */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Web\Html; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Html class file. |
7
|
|
|
* |
8
|
|
|
* @author Oleg Lunegov <[email protected]> |
9
|
|
|
* @link https://github.com/lugnsk/micro |
10
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
11
|
|
|
* @license /LICENSE |
12
|
|
|
* @package Micro |
13
|
|
|
* @subpackage Web\Html |
14
|
|
|
* @version 1.0 |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class Html |
18
|
|
|
{ |
19
|
|
|
use TagTrait; |
20
|
|
|
use HeadTagTrait; |
21
|
|
|
use TableTagTrait; |
22
|
|
|
use Html5TagTrait; |
23
|
|
|
use FieldTagTrait; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Render begin form tag |
28
|
|
|
* |
29
|
|
|
* @access public |
30
|
|
|
* |
31
|
|
|
* @param string $action path to URL action |
32
|
|
|
* @param string $method method of request |
33
|
|
|
* @param array $attributes attributes tag |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
* @static |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public static function beginForm($action, $method = 'POST', array $attributes = []) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$attributes['action'] = $action; |
41
|
|
|
$attributes['method'] = $method; |
42
|
|
|
|
43
|
|
|
return static::openTag('form', $attributes); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Render end form tag |
48
|
|
|
* |
49
|
|
|
* @access public |
50
|
|
|
* @return string |
51
|
|
|
* @static |
52
|
|
|
*/ |
53
|
|
|
public static function endForm() |
54
|
|
|
{ |
55
|
|
|
return static::closeTag('form'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Render image button tag |
61
|
|
|
* |
62
|
|
|
* @access public |
63
|
|
|
* |
64
|
|
|
* @param string $name image name |
65
|
|
|
* @param string $file image file path |
66
|
|
|
* @param array $attributesButton attributes for button |
67
|
|
|
* @param array $attributesImage attributes for image |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
* @static |
71
|
|
|
*/ |
72
|
|
|
public static function imageButton($name, $file, array $attributesButton = [], array $attributesImage = []) |
73
|
|
|
{ |
74
|
|
|
return static::button(static::image($name, $file, $attributesImage), $attributesButton); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Render button tag |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* |
82
|
|
|
* @param string $text text for button |
83
|
|
|
* @param array $attributes attributes tag |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
* @static |
87
|
|
|
*/ |
88
|
|
|
public static function button($text, array $attributes = []) |
89
|
|
|
{ |
90
|
|
|
return static::openTag('button', $attributes) . $text . static::closeTag('button'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Render textArea tag |
95
|
|
|
* |
96
|
|
|
* @access public |
97
|
|
|
* |
98
|
|
|
* @param string $name textArea name |
99
|
|
|
* @param string $text textArea text |
100
|
|
|
* @param array $attributes attributes tag |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
* @static |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public static function textArea($name, $text, array $attributes = []) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$attributes['id'] = $name; |
108
|
|
|
$attributes['name'] = $name; |
109
|
|
|
|
110
|
|
|
return static::openTag('textarea', $attributes) . $text . static::closeTag('textarea'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render legend tag |
115
|
|
|
* |
116
|
|
|
* @access public |
117
|
|
|
* |
118
|
|
|
* @param string $text legend text |
119
|
|
|
* @param array $attributes attributes tag |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
* @static |
123
|
|
|
*/ |
124
|
|
|
public static function legend($text, array $attributes = []) |
125
|
|
|
{ |
126
|
|
|
return static::openTag('legend', $attributes) . $text . static::closeTag('legend'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Render label tag |
131
|
|
|
* |
132
|
|
|
* @access public |
133
|
|
|
* |
134
|
|
|
* @param string $name label name |
135
|
|
|
* @param string $elemId element ID |
136
|
|
|
* @param array $attributes attributes tag |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
* @static |
140
|
|
|
*/ |
141
|
|
|
public static function label($name, $elemId = '', array $attributes = []) |
142
|
|
|
{ |
143
|
|
|
$attributes['for'] = $elemId; |
144
|
|
|
|
145
|
|
|
return static::openTag('label', $attributes) . $name . static::closeTag('label'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Render dropDownList (select tag) |
150
|
|
|
* |
151
|
|
|
* @access public |
152
|
|
|
* |
153
|
|
|
* @param string $name dropDown name |
154
|
|
|
* @param array $options format array(value, text, attributes) OR array(label, options, attributes) |
155
|
|
|
* @param array $attributes attributes tag |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
* @static |
159
|
|
|
*/ |
160
|
|
|
public static function dropDownList($name, array $options = [], array $attributes = []) |
161
|
|
|
{ |
162
|
|
|
$attributes['id'] = $name; |
163
|
|
|
$attributes['size'] = 1; |
164
|
|
|
|
165
|
|
|
return static::listBox($name, $options, $attributes); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Render listBox (select tag) |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* |
173
|
|
|
* @param string $name listBox name |
174
|
|
|
* @param array $options format array(value, text, attributes) OR array(label, options, attributes) |
175
|
|
|
* @param array $attributes attributes tag |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
* @static |
179
|
|
|
*/ |
180
|
|
|
public static function listBox($name, array $options = [], array $attributes = []) |
181
|
|
|
{ |
182
|
|
|
if (!empty($attributes['selected'])) { |
183
|
|
|
$selected = $attributes['selected']; |
184
|
|
|
unset($attributes['selected']); |
185
|
|
|
} else { |
186
|
|
|
$selected = null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$attributes['name'] = $name; |
190
|
|
|
$opts = ''; |
191
|
|
|
foreach ($options AS $option) { |
192
|
|
|
if (!empty($option['label'])) { |
193
|
|
|
$opts .= static::optGroup($option['label'], $option['options'], $option['attributes']); |
194
|
|
|
} else { |
195
|
|
|
$attr = []; |
196
|
|
|
if (!empty($option['attributes'])) { |
197
|
|
|
$attr = $option['attributes']; |
198
|
|
|
unset($option['attributes']); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (!empty($option['value']) && (string)$option['value'] === (string)$selected) { |
202
|
|
|
$attr['selected'] = 'selected'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$text = ''; |
206
|
|
|
if (!empty($option['text'])) { |
207
|
|
|
$text = $option['text']; |
208
|
|
|
unset($option['text']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$opts .= static::option(!empty($option['value']) ? $option['value'] : '', $text, $attr); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$attributes['name'] .= array_key_exists('multiple', $attributes) ? '[]' : ''; |
216
|
|
|
|
217
|
|
|
return static::openTag('select', $attributes) . $opts . static::closeTag('select'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Render optGroup tag |
222
|
|
|
* |
223
|
|
|
* @access public |
224
|
|
|
* |
225
|
|
|
* @param string $label label for options group |
226
|
|
|
* @param array $options format array(value, text, attributes) OR array(label, options, attributes) |
227
|
|
|
* @param array $attributes attributes tag |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
* @static |
231
|
|
|
*/ |
232
|
|
|
public static function optGroup($label, array $options = [], array $attributes = []) |
233
|
|
|
{ |
234
|
|
|
$attributes['label'] = $label; |
235
|
|
|
$opts = ''; |
236
|
|
|
foreach ($options AS $option) { |
237
|
|
|
if (!empty($option['label'])) { |
238
|
|
|
$opts .= static::optGroup($option['label'], $option['options'], $option['attributes']); |
239
|
|
|
} else { |
240
|
|
|
$opts .= static::option($option['value'], $option['text'], $option['attributes']); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return static::openTag('optgroup', $attributes) . $opts . static::closeTag('optgroup'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Render option tag |
249
|
|
|
* |
250
|
|
|
* @access public |
251
|
|
|
* |
252
|
|
|
* @param string $value option value |
253
|
|
|
* @param string $text label for option |
254
|
|
|
* @param array $attributes attributes tag |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
* @static |
258
|
|
|
*/ |
259
|
|
|
public static function option($value, $text, array $attributes = []) |
260
|
|
|
{ |
261
|
|
|
$attributes['value'] = $value; |
262
|
|
|
|
263
|
|
|
return static::openTag('option', $attributes) . $text . static::closeTag('option'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Converting array to options |
268
|
|
|
* |
269
|
|
|
* @param array $arr Input array |
270
|
|
|
* |
271
|
|
|
* @return array|null Output array |
272
|
|
|
*/ |
273
|
|
|
public static function arrayToOptions(array $arr = []) |
274
|
|
|
{ |
275
|
|
|
$result = []; |
276
|
|
|
foreach ($arr AS $n => $m) { |
277
|
|
|
$result[] = ['value' => $n, 'text' => $m]; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $result; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Render reset button tag |
285
|
|
|
* |
286
|
|
|
* @access public |
287
|
|
|
* |
288
|
|
|
* @param string $label text for label on button |
289
|
|
|
* @param array $attributes attributes tag |
290
|
|
|
* |
291
|
|
|
* @return string |
292
|
|
|
* @static |
293
|
|
|
*/ |
294
|
|
View Code Duplication |
public static function resetButton($label = 'Reset', array $attributes = []) |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
$attributes['type'] = 'reset'; |
297
|
|
|
$attributes['value'] = $label; |
298
|
|
|
|
299
|
|
|
return static::tag('input', $attributes); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Render submit button tag |
304
|
|
|
* |
305
|
|
|
* @access public |
306
|
|
|
* |
307
|
|
|
* @param string $label text for label on button |
308
|
|
|
* @param array $attributes attributes tag |
309
|
|
|
* |
310
|
|
|
* @return string |
311
|
|
|
* @static |
312
|
|
|
*/ |
313
|
|
View Code Duplication |
public static function submitButton($label = 'Submit', array $attributes = []) |
|
|
|
|
314
|
|
|
{ |
315
|
|
|
$attributes['type'] = 'submit'; |
316
|
|
|
$attributes['value'] = $label; |
317
|
|
|
|
318
|
|
|
return static::tag('input', $attributes); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.