firstPHP/src/ActiveForm.php

78 lines
1.6 KiB
PHP
Raw Normal View History

2024-03-15 16:56:03 +03:00
<?php
namespace itguild\forms;
2024-03-15 17:54:06 +03:00
use itguild\forms\builders\SelectBuilder;
2024-03-18 17:52:16 +03:00
use itguild\forms\builders\TextAreaBuilder;
2024-03-15 16:56:03 +03:00
use itguild\forms\debug\Debug;
use itguild\forms\inputs\BaseInput;
2024-03-15 17:54:06 +03:00
use itguild\forms\inputs\Select;
2024-03-18 17:52:16 +03:00
use itguild\forms\inputs\TextArea;
use itguild\forms\templates\Template;
2024-03-15 16:56:03 +03:00
class ActiveForm
{
private BaseInput $fieldObject;
2024-03-18 18:24:24 +03:00
2024-03-18 17:52:16 +03:00
/**
* @param string $action
* @return void
*/
public function beginForm(string $action): void
{
echo "<form method='POST' action='$action'>";
}
/**
* @return void
*/
public function endForm(): void
{
echo "</form>";
}
2024-03-15 16:56:03 +03:00
public function field($class, string $name, array $params = [])
{
2024-03-15 17:54:06 +03:00
if ($class === Select::class){
$this->fieldObject = SelectBuilder::build($name, $params);
}
2024-03-18 18:24:24 +03:00
elseif ($class === TextArea::class){
2024-03-18 17:52:16 +03:00
$this->fieldObject = TextAreaBuilder::build($name, $params);
}
2024-03-15 17:54:06 +03:00
else {
$this->fieldObject = new $class($name, $params);
}
2024-03-15 16:56:03 +03:00
return $this;
}
2024-03-15 17:54:06 +03:00
public function setLabel(string $title): self
2024-03-15 16:56:03 +03:00
{
$this->fieldObject->setLabel($title);
return $this;
}
2024-03-15 17:54:06 +03:00
public function setOptions(array $options): self
{
$this->fieldObject->setOptions($options);
return $this;
}
2024-03-18 17:52:16 +03:00
public function setTemplate($template): self
{
$this->fieldObject->setTemplate($template);
return $this;
}
2024-03-15 16:56:03 +03:00
public function render()
{
2024-03-15 17:54:06 +03:00
$this->fieldObject->create();
2024-03-15 16:56:03 +03:00
$this->fieldObject->render();
}
}