forms2/src/Form.php
2025-03-14 17:42:58 +03:00

86 lines
1.8 KiB
PHP

<?php
namespace Itguild\Form2;
class Form
{
protected array $data = [];
protected array $formParams = [];
protected string $formParamsStr = '';
protected string $html = '';
public function __construct(array $formParams = [])
{
$this->formParams = $formParams;
$this->createParams();
}
protected function _start(): void
{
$this->html .= "<form $this->formParamsStr>";
}
protected function _end(): void
{
$this->html .= "</form>";
}
public function render(): void
{
$this->_start();
$this->createFields();
$this->_end();
echo $this->html;
}
public function fetch(): string
{
$this->_start();
$this->createFields();
$this->_end();
return $this->html;
}
public function field(string $type, string $name, array $params = []): void
{
$fieldArr = [];
$fieldArr['type'] = $type;
$fieldArr = array_merge($fieldArr, $params);
$this->data[$name] = $fieldArr;
}
public function activeField()
{
}
/**
* @param array $arr
* @return void
*/
public function load(array $arr): void
{
$this->data = $arr;
}
public function createFields(): void
{
foreach ($this->data as $key => $input) {
$this->html .= "</br><label for='$key'>" . $input['label'] . "</label>";
$this->html .= "<input type='" . $input['type'] . "' name='$key' id='" . $input['id'] . "' class='" . $input['class'] . "' >";
}
}
protected function createParams(): void
{
foreach ($this->formParams as $key => $param){
$this->formParamsStr .= " $key='$param'";
}
}
private function setSome()
{
}
}