1 | <?php |
||
11 | class DropdownElement extends InputElement { |
||
12 | |||
13 | /** @var array OptGroup[] */ |
||
14 | protected $optGroups = array(); |
||
15 | |||
16 | /** |
||
17 | * @param string $name The name of this form element |
||
18 | * @param array $options The available options |
||
19 | * @param string $label The label text for this element (will be autoescaped) |
||
20 | */ |
||
21 | public function __construct($name, $options, $label = '') { |
||
27 | |||
28 | /** |
||
29 | * Add an `<optgroup>` and respective options |
||
30 | * |
||
31 | * @param string $label |
||
32 | * @param array $options |
||
33 | * @return OptGroup a reference to the added optgroup |
||
34 | * @throws \Exception |
||
35 | */ |
||
36 | public function addOptGroup($label, $options) { |
||
43 | |||
44 | /** |
||
45 | * Set or get the optgroups of an Dropdown-Element. |
||
46 | * |
||
47 | * optgroups have to be given as associative array |
||
48 | * * the key being the label of the group |
||
49 | * * the value being an array of options as defined in @see OptGroup::options() |
||
50 | * |
||
51 | * @param null|array $optGroups |
||
52 | * @return OptGroup[]|DropdownElement |
||
53 | */ |
||
54 | public function optGroups($optGroups = null) { |
||
67 | |||
68 | /** |
||
69 | * Get or set the options of the Dropdown |
||
70 | * |
||
71 | * Options can be given as associative array (value => label) or as an |
||
72 | * indexd array (label = value) or as an array of arrays. In the latter |
||
73 | * case an element has to look as follows: |
||
74 | * option-value => array ( |
||
75 | * 'label' => option-label, |
||
76 | * 'attrs' => array ( |
||
77 | * attr-key => attr-value, ... |
||
78 | * ) |
||
79 | * ) |
||
80 | * |
||
81 | * @param null|array $options |
||
82 | * @return $this|array |
||
83 | */ |
||
84 | public function options($options = null) { |
||
91 | |||
92 | /** |
||
93 | * Gets or sets an attribute |
||
94 | * |
||
95 | * When no $value is given, the current content of the attribute is returned. |
||
96 | * An empty string is returned for unset attributes. |
||
97 | * |
||
98 | * When a $value is given, the content is set to that value and the Element |
||
99 | * itself is returned for easy chaining |
||
100 | * |
||
101 | * @param string $name Name of the attribute to access |
||
102 | * @param null|string $value New value to set |
||
103 | * @return string|$this |
||
104 | */ |
||
105 | public function attr($name, $value = null) { |
||
111 | |||
112 | /** |
||
113 | * Get or set the current value |
||
114 | * |
||
115 | * When setting a value that is not defined in the options, the value is ignored |
||
116 | * and the first option's value is selected instead |
||
117 | * |
||
118 | * @param null|string $value The value to set |
||
119 | * @return $this|string |
||
120 | */ |
||
121 | public function val($value = null) { |
||
136 | |||
137 | /** |
||
138 | * Returns the first options as it will be rendered in HTML |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function getFirstOption() { |
||
154 | |||
155 | /** |
||
156 | * Set the value in the OptGroups, including the optgroup for the options without optgroup. |
||
157 | * |
||
158 | * @param string $value |
||
159 | * @return bool |
||
160 | */ |
||
161 | protected function setValueInOptGroups($value) { |
||
172 | |||
173 | /** |
||
174 | * Create the HTML for the select it self |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | protected function mainElementHTML() { |
||
187 | |||
188 | } |
||
189 |