main
Robert 2 months ago
parent 8e1cdc6551
commit 74219b28c0
  1. 43
      src/classes/form_builder/html_form.php

@ -15,6 +15,7 @@ use CodeHydrater\enums\form_input_types as FORM_TYPE;
class html_form { class html_form {
private $output = ""; private $output = "";
private int $tabs = 0;
private bool $escape = true; private bool $escape = true;
public function __construct(protected Helper $html = new Helper()) {} public function __construct(protected Helper $html = new Helper()) {}
@ -27,8 +28,33 @@ class html_form {
$this->escape = $escape; $this->escape = $escape;
} }
private function append_out(?string $out, int $tabs = 2): ?string { public function set_tabs(int $tabs = 2) {
$this->output .= str_repeat("\t", $tabs) . $out . PHP_EOL; 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; return $out;
} }
@ -116,17 +142,20 @@ class html_form {
public function close(): string public function close(): string
{ {
return $this->append_out('</form>', 0); $this->tabs = 0;
return $this->append_out('</form>');
} }
public function fieldset_close(): string public function fieldset_close(): string
{ {
$this->dec_tabs();
return $this->append_out('</fieldset>'); return $this->append_out('</fieldset>');
} }
public function fieldset_open(array $options = []): string public function fieldset_open(array $options = []): string
{ {
return $this->append_out('<fieldset'.$this->html->attributes($options).'>'); $this->inc_tabs();
return $this->append_out('<fieldset'.$this->html->attributes($options).'>', "inc");
} }
public function input(string|null $name = null, array $options = []): string public function input(string|null $name = null, array $options = []): string
@ -142,11 +171,12 @@ class html_form {
public function open_div(array $options = []): string public function open_div(array $options = []): string
{ {
return $this->append_out('<div'.$this->html->attributes($options).'>'); return $this->append_out('<div'.$this->html->attributes($options).'>', "inc");
} }
public function close_div(): string public function close_div(): string
{ {
$this->dec_tabs();
return $this->append_out('</div>'); return $this->append_out('</div>');
} }
@ -169,7 +199,8 @@ class html_form {
$options['method'] ??= 'post'; $options['method'] ??= 'post';
$options['accept-charset'] ??= $this->html->get_charset(); $options['accept-charset'] ??= $this->html->get_charset();
return $this->append_out('<form'.$this->html->attributes($options).'>', 0); $this->tabs = 0;
return $this->append_out('<form'.$this->html->attributes($options).'>');
} }
public function open_multipart(string|null $action = null, array $options = []): string public function open_multipart(string|null $action = null, array $options = []): string

Loading…
Cancel
Save