forms/inputs/Select.php

80 lines
1.9 KiB
PHP
Raw Normal View History

2024-05-28 17:10:06 +03:00
<?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 Select extends BaseInput
{
use CreateParams;
use CreateOption;
private $name;
private $options;
private $value;
private $paramsArray;
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
*/
public function __construct(string $name, array $options = [], $value = null, array $paramsArray = [])
{
$this->name = $name;
$this->options = $options;
$this->value = $value;
$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);
$label = "";
$select = "<select name='$this->name' $paramsString>$optionsString</select>";
$this->createLabel();
$this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
$this->html = str_replace('{label}', $this->labelString, $this->html);
return $this;
}
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
{
$select = new self($name, $options, $value, $paramsArray);
$select->create()->render();
}
/**
* @param $options
* @return $this
*/
public function setOptions($options)
{
$this->options = array_merge($options, $this->options);
return $this;
}
}