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

63 lines
1.6 KiB
PHP
Executable File

<?php
namespace itguild\forms\form\inputs;
use itguild\forms\form\templates\Simple\SimpleTemplate;
use itguild\forms\form\traits\CreateOption;
use itguild\forms\form\traits\CreateParams;
class RadioButton extends BaseInput
{
use CreateParams;
use CreateOption;
private $name;
private $paramsArray;
private $options = [];
private $value;
public function __construct(string $name, array $paramsArray = [])
{
$this->name = $name;
$this->paramsArray = $paramsArray;
$this->inputTemplate = new SimpleTemplate();
}
/**
* @return $this
*/
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$optionsString = $this->createOption($this->options, $this->value);
$radioButton = "<input name='$this->name' type='radio' $paramsString>$optionsString";
$label = "";
$this->createLabel();
$this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $paramsArray = []): void
{
$radioButton = new self($name, $paramsArray);
$radioButton->create()->render();
}
/**
* @param $options
* @return $this
*/
public function setOptions($options)
{
$this->options = array_merge($options, $this->options);
return $this;
}
}