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.

232 lines
5.0 KiB
PHTML

<?php
namespace Artmark\Forms;
/**
* Description of AbstractField
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
abstract class AbstractField extends AbstractHtml
{
/**
* Форма, к которой относится поле
* @var Form or null
*/
private $form;
/**
* Текстовая метка поля
* @var Label
*/
private $label;
/**
* Тип поля
* @var string
*/
private $type;
/**
* Название поля
* @var string
*/
private $name;
4 years ago
/**
* Сообщения об ошибках
* @var string[]
*/
private $errors = [];
protected function __construct($form, $type, $name)
{
self::checkName($name);
self::checkType($type);
self::checkForm($form);
$this->label = new Label($this);
$this->form = $form;
$this->type = $type;
$this->name = Utils::cleanString($name);
}
/**
*
* @return Form or null
*/
public function form()
{
return $this->form;
}
/**
*
* @return string
*/
public function type()
{
return $this->type;
}
/**
*
* @return string
*/
public function name()
{
return $this->name;
}
public function nameWithFormPrefix()
{
if (!is_null($this->form) && $this->form->hasNamePrefix()) {
return $this->form->namePrefix() . $this->name;
}
return $this->name;
}
public function idWithFormPrefix()
{
if (!$this->hasId()) {
return '';
}
if (!is_null($this->form) && $this->form->hasIdPrefix()) {
return $this->form->idPrefix() . $this->id();
}
return $this->id();
}
/**
* Возвращает ранее заданное значение поля
* @return mixed
*/
public abstract function value();
/**
* Устанавливает новое значение для поля
* @param mixed $newValue новое значение
* @return $this
*/
public abstract function setValue($newValue);
/**
* Возвращает текстовую метку поля
* @return Label
*/
public function label()
{
return $this->label;
}
/**
* Устанавливает текст для метки поля
* @param string $text
* @return $this
*/
public function setLabelText($text)
{
$this->label->setText($text);
return $this;
}
/**
4 years ago
* Возвращает массив сообщений об ошибках
* @return string[]
*/
public function errors()
{
return $this->errors;
}
/**
* Проверяет, есть ли у поля сообщения об ошибках
* @return boolean
*/
public function hasErrors()
{
return count($this->errors) > 0;
}
/**
* Очищает сообщения об ошибках
* @return $this
*/
public function cleanErrors()
{
$this->errors = [];
return $this;
}
/**
* Добавляет новое сообщение об ошибке в поле
* @param string $message
* @return $this
*/
public function addError($message)
{
$cleanedMessage = trim(strval($message));
if (!in_array($cleanedMessage, $this->errors)) {
$this->errors[] = $cleanedMessage;
}
return $this;
}
/**
*
* @return array
*/
public function getAssociativeAttributes()
{
$attributes = parent::getAssociativeAttributes();
$attributes['name'] = $this->nameWithFormPrefix();
if ($this->hasId()) {
$attributes['id'] = $this->idWithFormPrefix();
}
return $attributes;
}
public static function checkForm($form)
{
if (is_null($form) || $form instanceof Form) {
return;
}
throw new AttributeException('The name of a field must be a non-empty string');
}
public static function checkName($name)
{
if (!self::isCorrectName($name)) {
throw new AttributeException('The name of a field must be a non-empty string');
}
}
public static function isCorrectName($name)
{
return is_numeric($name) || Utils::isNonEmptyString($name);
}
public static function checkType($type)
{
if (!self::isCorrectType($type)) {
throw new AttributeException('The type of a field must be a non-empty string with of english letters, numbers, underline and dash symbols');
}
}
public static function isCorrectType($type)
{
return is_string($type) && preg_match('/^[0-9a-z_\-]+$/ui', trim($type));
}
}