招标
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

115 lines
2.4 KiB

<?php
/**
*
* FormText.php
* User: ChenLong
* DateTime: 2020/3/11 14:40
*/
namespace sdModule\makeAdminBasics\htmlUnit;
use sdModule\makeAdminBasics\Basics;
/**
* 单选
* Class FormRadio
* @package sdModule\makeAdminBasics\htmlUnit
*/
class FormRadio implements FormUnitInterface
{
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $name;
/**
* @var
*/
private $default;
/**
*
* FormText constructor.
* @param string $label
* @param string $name
*/
public function __construct(string $label, string $name)
{
$this->label = $label;
$this->name = $name;
}
/**
* 获取html
* @param array $data
* @return mixed|string
*/
public function getHtml($data = [])
{
return <<<HTML
<div class="layui-form-item">{$this->getLabel()}
<div class="layui-input-block">{$this->getForm($data)}</div>
</div>
HTML;
}
/**
* 获取label
* @return string
*/
private function getLabel()
{
return Basics::indentAndLineFeed(4, Basics::BEFORE) .
"<label class=\"layui-form-label\">{$this->label}</label>";
}
/**
* 获取表单html文本
* @param $data
* @return string
*/
private function getForm($data)
{
$html = '';
$dataOriginTitle = is_string($data) ?
explode(':', strtr($data, [':' => ':']))[0]
: $this->name . '_data';
$html .= Basics::indentAndLineFeed(5, Basics::BEFORE) .
implode(Basics::indentAndLineFeed(5, Basics::BEFORE), [
"{foreach \${$dataOriginTitle} as \$value => \$title}",
$this->default
? "<input type=\"radio\" name=\"{$this->name}\" value=\"{\$value}\" title=\"{\$title}\" {if \$value == '$this->default'}checked{/if} autocomplete=\"off\" class=\"layui-input\">"
: "<input type=\"radio\" name=\"{$this->name}\" value=\"{\$value}\" title=\"{\$title}\" autocomplete=\"off\" class=\"layui-input\">"
,
"{/foreach}"
]);
return $html . Basics::indentAndLineFeed(4, Basics::BEFORE);
}
/**
* 设置默认值
* @param $value
* @return mixed
*/
public function setDefault($value)
{
$value and $this->default = $value;
return $this;
}
}