diff --git a/src/classes/form_builder/html_form.php b/src/classes/form_builder/html_form.php index 7a09bc5..c024727 100644 --- a/src/classes/form_builder/html_form.php +++ b/src/classes/form_builder/html_form.php @@ -15,6 +15,7 @@ use CodeHydrater\enums\form_input_types as FORM_TYPE; class html_form { private $output = ""; + private int $tabs = 0; private bool $escape = true; public function __construct(protected Helper $html = new Helper()) {} @@ -27,8 +28,33 @@ class html_form { $this->escape = $escape; } - private function append_out(?string $out, int $tabs = 2): ?string { - $this->output .= str_repeat("\t", $tabs) . $out . PHP_EOL; + public function set_tabs(int $tabs = 2) { + if ($tabs >= 0 && $tabs <= 10) { + $this->tabs = $tabs; + } + } + + public function dec_tabs() { + if ($this->tabs > 1) { + $this->tabs -= 1; + } + } + + public function inc_tabs() { + if ($this->tabs <= 10) { + $this->tabs += 1; + } + } + + private function append_out(?string $out, string $do = ""): ?string { + $output = str_repeat("\t", $this->tabs) . $out . PHP_EOL; + $this->output .= $output; + if ($do === "inc") { + $this->inc_tabs(); + } + if ($do === "dev") { + $this->dec_tabs(); + } return $out; } @@ -116,17 +142,20 @@ class html_form { public function close(): string { - return $this->append_out('', 0); + $this->tabs = 0; + return $this->append_out(''); } public function fieldset_close(): string { + $this->dec_tabs(); return $this->append_out(''); } public function fieldset_open(array $options = []): string { - return $this->append_out('html->attributes($options).'>'); + $this->inc_tabs(); + return $this->append_out('html->attributes($options).'>', "inc"); } public function input(string|null $name = null, array $options = []): string @@ -142,11 +171,12 @@ class html_form { public function open_div(array $options = []): string { - return $this->append_out('html->attributes($options).'>'); + return $this->append_out('html->attributes($options).'>', "inc"); } public function close_div(): string { + $this->dec_tabs(); return $this->append_out(''); } @@ -169,7 +199,8 @@ class html_form { $options['method'] ??= 'post'; $options['accept-charset'] ??= $this->html->get_charset(); - return $this->append_out('html->attributes($options).'>', 0); + $this->tabs = 0; + return $this->append_out('html->attributes($options).'>'); } public function open_multipart(string|null $action = null, array $options = []): string