Total Complexity | 14 |
Total Lines | 65 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
3 | class Nip_Form_Renderer_Elements_Select extends \Nip\Form\Renderer\Elements\AbstractElementRenderer |
||
4 | { |
||
5 | /** |
||
6 | * @return string |
||
7 | */ |
||
8 | public function generateElement() |
||
16 | } |
||
17 | |||
18 | public function renderOptions($options = false) |
||
19 | { |
||
20 | $options = $options ? $options : $this->getElement()->getOptions(); |
||
21 | $return = ''; |
||
22 | |||
23 | $selectedValue = $this->getElement()->getValue(); |
||
24 | |||
25 | foreach ($options as $value=>$atribs) { |
||
26 | if (is_string($value) && !isset($atribs['label'])) { |
||
27 | $return .= '<optgroup label="' . $value . '">'; |
||
28 | $return .= $this->renderOptions($atribs); |
||
29 | $return .= '</optgroup>'; |
||
30 | } else { |
||
31 | $return .= '<option'; |
||
32 | |||
33 | $label = $atribs['label']; |
||
34 | unset($atribs['label']); |
||
35 | |||
36 | $atribs['value'] = $value; |
||
37 | if ($this->isOptionSelected($value, $selectedValue)) { |
||
38 | $atribs['selected'] = 'selected'; |
||
39 | } |
||
40 | |||
41 | foreach ($atribs as $name=>$value) { |
||
|
|||
42 | $return .= ' ' . $name . '="' . $value . '"'; |
||
43 | } |
||
44 | $return .= '>' . $label . '</option>'; |
||
45 | } |
||
46 | } |
||
47 | return $return; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param $value |
||
52 | * @param $selectedValue |
||
53 | * @return bool |
||
54 | */ |
||
55 | protected function isOptionSelected($value, $selectedValue): bool |
||
68 | } |
||
69 | } |
||
70 |