forms/ActiveForm.php
2024-05-28 17:10:06 +03:00

107 lines
2.7 KiB
PHP
Executable File

<?php
namespace itguild\forms\form;
use itguild\forms\form\builders\ButtonBuilder;
use itguild\forms\form\builders\CheckBoxBuilder;
use itguild\forms\form\builders\HiddenBuilder;
use itguild\forms\form\builders\RadioButtonBuilder;
use itguild\forms\form\builders\SelectBuilder;
use itguild\forms\form\builders\TextAreaBuilder;
use itguild\forms\form\builders\TextInputBuilder;
use itguild\forms\form\inputs\BaseInput;
use itguild\forms\form\inputs\Button;
use itguild\forms\form\inputs\Checkbox;
use itguild\forms\form\inputs\Hidden;
use itguild\forms\form\inputs\RadioButton;
use itguild\forms\form\inputs\Select;
use itguild\forms\form\inputs\TextArea;
use itguild\forms\form\inputs\TextInput;
class ActiveForm
{
private BaseInput $fieldObject;
/**
* @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>";
}
public function field($class, string $name, array $params = [])
{
if ($class === Select::class){
$this->fieldObject = SelectBuilder::build($name, $params);
}
elseif ($class === TextInput::class){
$this->fieldObject = TextInputBuilder::build($name, $params);
}
elseif ($class === TextArea::class){
$this->fieldObject = TextAreaBuilder::build($name, $params);
}
elseif ($class === Checkbox::class) {
$this->fieldObject = CheckBoxBuilder::build($name, $params);
}
elseif ($class === Button::class) {
$this->fieldObject = ButtonBuilder::build($name, $params);
}
elseif ($class === RadioButton::class){
$this->fieldObject = RadioButtonBuilder::build($name, $params);
}
elseif ($class === Hidden::class){
$this->fieldObject = HiddenBuilder::build($name, $params);
}
else {
$this->fieldObject = new $class($name, $params);
}
return $this;
}
public function setLabel(string $title): self
{
$this->fieldObject->setLabel($title);
return $this;
}
public function setOptions(array $options): self
{
$this->fieldObject->setOptions($options);
return $this;
}
public function setTemplate($template): self
{
$this->fieldObject->setTemplate($template);
return $this;
}
public function render()
{
$this->fieldObject->create();
$this->fieldObject->render();
}
public function fetch()
{
$this->fieldObject->create();
return $this->fieldObject->fetch();
}
}