This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Select.php
58 lines (52 loc) · 1.51 KB
/
Select.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace ephFrame\HTML\Form\Element;
use
\ephFrame\HTML\Tag
;
class Select extends MultipleChoice
{
public $attributes = array(
'multiple' => false,
'size' => 1,
);
public function tag()
{
if (isset($this->attributes['multiple']) && $this->attributes['multiple']) {
$this->attributes['multiple'] = 'multiple';
}
$options = array();
foreach($this->options as $value => $label) {
if (is_array($label)) {
$options[] = $this->optGroup($value, $label);
} else {
$options[] = $this->optionTag($label, $value);
}
}
unset($this->attributes['value']);
return new Tag($this->tag, $options, $this->attributes + array('escaped' => false));
}
protected function optGroup($label, Array $options = array())
{
$optionTags = array();
foreach($options as $value => $optionLabel) {
$optionTags[] = $this->optionTag($optionLabel, $value);
}
return new Tag('optgroup', $optionTags, array('label' => $label));
}
protected function optionTag($label, $value)
{
$optionAttributes = array(
'value' => $value,
);
if (!isset($this->data) && isset($this->attributes['value'])) {
if ($this->attributes['value'] === $value || (is_array($this->attributes['value']) && in_array($value, $this->attributes['value']))) {
$optionAttributes['selected'] = 'selected';
}
} else {
if ($this->data == $value || (is_array($this->data) && in_array($value, $this->data))) {
$optionAttributes['selected'] = 'selected';
}
}
return new Tag('option', $label, $optionAttributes);
}
}