| Total Complexity | 6 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | final class SelectHtmlRenderer implements FieldViewRendererInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var SelectHtmlRenderer|null |
||
| 18 | */ |
||
| 19 | private static $instance; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | 12 | public function render(FieldViewInterface $view, array $attributes): string |
|
| 25 | { |
||
| 26 | 12 | if (!$choices = $view->choices()) { |
|
| 27 | 1 | throw new InvalidArgumentException('Choices must be provided for render a select element.'); |
|
| 28 | } |
||
| 29 | |||
| 30 | 11 | $attributes['name'] = $view->name(); |
|
| 31 | 11 | $attributes['required'] = $view->required(); |
|
| 32 | |||
| 33 | 11 | if (!empty($attributes['multiple'])) { |
|
| 34 | 2 | $attributes['name'] .= '[]'; |
|
| 35 | } |
||
| 36 | |||
| 37 | 11 | $options = ''; |
|
| 38 | |||
| 39 | 11 | foreach ($choices as $choice) { |
|
| 40 | 11 | $options .= HtmlRenderer::element('option', ['value' => $choice->value(), 'selected' => $choice->selected()], htmlentities($choice->label())); |
|
| 41 | } |
||
| 42 | |||
| 43 | 11 | return HtmlRenderer::element('select', $attributes, $options); |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get the renderer instance |
||
| 48 | * |
||
| 49 | * @return self |
||
| 50 | */ |
||
| 51 | 19 | public static function instance(): self |
|
| 58 | } |
||
| 59 | } |
||
| 60 |