+Базовая версия пакета форм
This commit is contained in:
115
src/AbstractChoiceOption.php
Normal file
115
src/AbstractChoiceOption.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user