$options Associative: value => display text.
*/
public function select(
string $id,
string $label,
array $options,
string $desc = '',
bool $required = false,
string $selected = ''
): static {
$req = $required ? 'required' : '';
$opts = $this->build_options($options, $selected);
$ctrl = '';
return $this->item($id, $label, $ctrl, $desc);
}
public function textarea(
string $id,
string $label,
string $desc = '',
bool $required = false,
string $value = '',
int $rows = 5
): static {
$req = $required ? 'required' : '';
$ctrl = '';
return $this->item($id, $label, $ctrl, $desc);
}
// -------------------------------------------------------------------------
// Rendering
// -------------------------------------------------------------------------
public function render(): string
{
return ''
. implode('', $this->items)
. '
';
}
// -------------------------------------------------------------------------
// Private helpers
// -------------------------------------------------------------------------
private function item(string $id, string $label, string $control_html, string $desc): static
{
$d = $desc !== '' ? '' . esc_html($desc) . '
' : '';
$this->items[] = ''
. ''
. $control_html
. $d
. '';
return $this;
}
/**
* @param array $options
*/
private function build_options(array $options, string $selected): string
{
$html = '';
foreach ($options as $value => $text) {
$is_selected = (string) $value === $selected ? 'selected' : '';
$html .= '';
}
return $html;
}
}