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.
83 lines
1.4 KiB
PHP
83 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Artmark\Forms\Attributes;
|
|
|
|
use Artmark\Forms\Utils;
|
|
|
|
/**
|
|
* Description of IdAttribute
|
|
*
|
|
* @author Andrey Pokidov <pokidov@e-traffic.ru>
|
|
*/
|
|
trait IdAttribute
|
|
{
|
|
/**
|
|
*
|
|
* @var string
|
|
*/
|
|
private $id = '';
|
|
|
|
/**
|
|
* HTML Id attribute of the tag
|
|
* @return string
|
|
*/
|
|
public function id()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function hasId()
|
|
{
|
|
return $this->id !== '';
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $newId
|
|
* @return $this
|
|
*/
|
|
public function setId($newId)
|
|
{
|
|
self::checkId($newId);
|
|
|
|
$this->id = Utils::cleanString($newId);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function removeId()
|
|
{
|
|
$this->id = '';
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function appendIdAttribute(array & $attributes)
|
|
{
|
|
if ($this->hasId())
|
|
{
|
|
$attributes['id'] = $this->id;
|
|
}
|
|
}
|
|
|
|
public static function checkId($id)
|
|
{
|
|
if (!self::isCorrectId($id)) {
|
|
throw new AttributeException('Incorrect id value: `' . strval($id) . '`');
|
|
}
|
|
}
|
|
|
|
public static function isCorrectId($id)
|
|
{
|
|
return is_numeric($id) || (is_string($id) && !preg_match('/[ \r\n\t\b]+/', trim($id)));
|
|
}
|
|
}
|