<?php namespace PHPFUI\Input; class OptGroup extends \PHPFUI\HTML5Element implements \Countable { protected array $options = []; public function __construct(string $label) { parent::__construct('optgroup'); $this->addAttribute('label', $label); } public function addOption(string $label, ?string $value = null, bool $selected = false, bool $disabled = false) : static { $label = '' === $label ? ' ' : \PHPFUI\TextHelper::htmlentities($label); $this->options[] = ['label' => $label, 'value' => $value, 'selected' => $selected ? ' selected' : '', 'disabled' => $disabled ? ' disabled' : '', ]; return $this; } public function count() : int { return \count($this->options); } protected function getBody() : string { $output = ''; foreach ($this->options as $option) { $selected = $option['selected']; $disabled = $option['disabled']; $value = null !== $option['value'] ? " value='{$option['value']}'" : ''; $output .= "<option{$value}{$selected}{$disabled}>{$option['label']}</option>"; } return $output; } }