You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.4 KiB
PHTML
116 lines
2.4 KiB
PHTML
5 years ago
|
<?php
|
||
|
|
||
|
namespace Artmark\Forms;
|
||
|
|
||
|
use Artmark\Forms\Attributes\DisabledAttribute;
|
||
|
|
||
|
/**
|
||
|
* Description of AbstractChoiceOption
|
||
|
*
|
||
|
* @author Andrey Pokidov <pokidov@e-traffic.ru>
|
||
|
*/
|
||
|
abstract class AbstractChoiceOption extends AbstractVisibleHtml
|
||
|
{
|
||
|
use DisabledAttribute;
|
||
|
|
||
|
/**
|
||
|
* The value of the option
|
||
|
* @var string|int|float
|
||
|
*/
|
||
|
private $value = '';
|
||
|
|
||
|
/**
|
||
|
* The label of the option
|
||
|
* @var string
|
||
|
*/
|
||
|
private $text = '';
|
||
|
|
||
|
public function __construct($value, $text)
|
||
|
{
|
||
|
$this->value = self::correctValue($value);
|
||
|
$this->text = strval($text);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Метод возвращает поле типа AbstractChoiceField
|
||
|
* @return AbstractChoiceField
|
||
|
*/
|
||
|
public abstract function field();
|
||
|
|
||
|
/**
|
||
|
* Возвращает значение опции
|
||
|
* @return string|int|float
|
||
|
*/
|
||
|
public function value()
|
||
|
{
|
||
|
return $this->value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Возвращает текст опции
|
||
|
* @return string
|
||
|
*/
|
||
|
public function text()
|
||
|
{
|
||
|
return $this->text;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Задёт новый текст для опции
|
||
|
* @param string $newText
|
||
|
* @return $this
|
||
|
*/
|
||
|
public function setText($newText)
|
||
|
{
|
||
|
$this->text = self::correctValue($newText);
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* The methods allows to determine is the option selected or not
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function isSelected()
|
||
|
{
|
||
|
return $this->field()->isSelected($this);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Отмечает опцию как выбранную
|
||
|
* @return $this
|
||
|
*/
|
||
|
public function select()
|
||
|
{
|
||
|
$this->field()->select($this);
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
public function getAssociativeAttributes()
|
||
|
{
|
||
|
$attributes = parent::getAssociativeAttributes();
|
||
|
|
||
|
$attributes['value'] = $this->value;
|
||
|
|
||
|
$this->appendVisibleAttributes($attributes);
|
||
|
$this->appendDisabledAttribute($attributes);
|
||
|
|
||
|
$this->field()->appendAttributesToOption($this, $attributes);
|
||
|
|
||
|
return $attributes;
|
||
|
}
|
||
|
|
||
|
private static function correctValue($value)
|
||
|
{
|
||
|
if (is_string($value) || is_numeric($value)) {
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
if (is_null($value)) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
return strval($value);
|
||
|
}
|
||
|
}
|