firstPHP/src/inputs/Radio.php

69 lines
1.6 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-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;
class Radio 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-15 17:54:06 +03:00
private bool $hasLabel = false;
private string $labelTitle = '';
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-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-18 11:07:09 +03:00
$this->html = "<input name='$this->name' type='radio' $paramsString>$optionsString</input>";
2024-03-15 17:54:06 +03:00
$this->html = "<label>$this->labelTitle</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
{
$label = new self($name, $paramsArray);
2024-03-15 16:56:03 +03:00
$label->create()->render();
}
/**
* @param string $title
* @return $this
*/
public function setLabel(string $title): self
{
2024-03-15 17:54:06 +03:00
$this->hasLabel = true;
$this->labelTitle = $title;
return $this;
}
/**
* @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
}
}