Work on the Form class.

This commit is contained in:
Dave Smith-Hayes 2025-07-28 22:27:49 -04:00
parent 3a6f92f78a
commit ec8bb01dc8

View File

@ -16,8 +16,7 @@ class Form
public readonly string $name, public readonly string $name,
public readonly string $method, public readonly string $method,
public readonly string $action public readonly string $action
) { ) { }
}
public function output(): string public function output(): string
{ {
@ -26,16 +25,30 @@ class Form
return $doc->saveHTML($this->form); return $doc->saveHTML($this->form);
} }
protected function addElement(string $type, array $properties): DOMElement /**
* Helper method for adding an element to the Form.
*
* @param string $type The type of element to add
* @param array<string, mixed> $properties Proprties for the element
* @return DOMElement
*/
protected function addElement(string $type, array $properties = []): DOMElement
{ {
$elem = new DOMElement($type); $elem = new DOMElement($type);
foreach ($properties as $attr => $value) {
$elem->setAttribute($attr, $value);
}
if (!empty($properties)) {
foreach ($properties as $attr => $value) {
$elem->setAttribute($attr, $value);
}
}
return $elem; return $elem;
} }
/**
* @param string $name
* @param array<string, mixed> $properties
* @return satic
*/
public function addInput(string $name, array $properties = []): static public function addInput(string $name, array $properties = []): static
{ {
$input = $this->addElement('input', [ $input = $this->addElement('input', [