firstPHP/src/inputs/RadioButton.php

63 lines
1.7 KiB
PHP
Raw Normal View History

2024-03-15 14:17:25 +03:00
<?php
2024-03-15 16:56:03 +03:00
namespace itguild\forms\inputs;
2024-03-19 17:40:27 +03:00
use itguild\forms\templates\Simple\SimpleTemplate;
2024-03-15 17:54:06 +03:00
use itguild\forms\traits\CreateOption;
2024-03-15 16:56:03 +03:00
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
2024-03-20 17:46:58 +03:00
class RadioButton extends BaseInput
2024-03-15 14:17:25 +03:00
{
use CreateParams;
2024-03-15 17:54:06 +03:00
use CreateOption;
2024-03-15 14:17:25 +03:00
private $name;
private $paramsArray;
2024-03-18 11:07:09 +03:00
private $options = [];
private $value;
2024-03-19 17:40:27 +03:00
2024-03-15 16:56:03 +03:00
2024-03-15 14:17:25 +03:00
public function __construct(string $name, array $paramsArray = [])
{
$this->name = $name;
$this->paramsArray = $paramsArray;
2024-03-19 17:40:27 +03:00
$this->inputTemplate = new SimpleTemplate();
2024-03-15 14:17:25 +03:00
}
/**
2024-03-15 16:56:03 +03:00
* @return $this
2024-03-15 14:17:25 +03:00
*/
2024-03-15 16:56:03 +03:00
public function create(): self
2024-03-15 14:17:25 +03:00
{
$paramsString = $this->createParams($this->paramsArray);
2024-03-15 17:54:06 +03:00
$optionsString = $this->createOption($this->options, $this->value);
2024-03-19 17:40:27 +03:00
$radioButton = "<input name='$this->name' type='radio' $paramsString>$optionsString";
$label = "";
2024-03-18 13:25:38 +03:00
if($this->hasLabel == true) {
2024-03-20 17:46:58 +03:00
$label = "<label >$this->labelTitle</label>";
2024-03-18 13:25:38 +03:00
}
2024-03-19 17:40:27 +03:00
$this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $label, $this->html);
2024-03-15 16:56:03 +03:00
return $this;
2024-03-15 14:17:25 +03:00
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $paramsArray = []): void
{
2024-03-19 17:40:27 +03:00
$radioButton = new self($name, $paramsArray);
$radioButton->create()->render();
2024-03-15 16:56:03 +03:00
}
2024-03-15 17:54:06 +03:00
/**
* @param $options
* @return $this
*/
public function setOptions($options)
{
$this->options = array_merge($options, $this->options);
2024-03-15 16:56:03 +03:00
return $this;
2024-03-15 14:17:25 +03:00
}
}