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.
92 lines
1.8 KiB
PHP
92 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Artmark\Forms\Attributes;
|
|
|
|
/**
|
|
* Description of DirectionAttribute
|
|
*
|
|
* @author Andrey Pokidov <pokidov@e-traffic.ru>
|
|
*/
|
|
trait DirectionAttribute
|
|
{
|
|
/**
|
|
*
|
|
* @var string
|
|
*/
|
|
private $direction = '';
|
|
|
|
/**
|
|
* HTML dir attribute of the tag
|
|
* @return string
|
|
*/
|
|
public function direction()
|
|
{
|
|
return $this->direction;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function hasDirection()
|
|
{
|
|
return $this->direction !== '';
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $newDirection
|
|
* @return $this
|
|
*/
|
|
public function setDirection($newDirection)
|
|
{
|
|
self::checkDirection($newDirection);
|
|
|
|
$this->direction = self::normalizeString($newDirection);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function removeDirection()
|
|
{
|
|
$this->direction = '';
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function appendDirectionAttribute(array & $attributes)
|
|
{
|
|
if ($this->hasDirection())
|
|
{
|
|
$attributes['dir'] = $this->direction;
|
|
}
|
|
}
|
|
|
|
public static function checkDirection($direction)
|
|
{
|
|
if (!self::isCorrectDirection($direction)) {
|
|
throw new AttributeException('Incorrect direction value: `' . strval($direction) . '`');
|
|
}
|
|
}
|
|
|
|
public static function isCorrectDirection($direction)
|
|
{
|
|
if (!is_string($direction)) {
|
|
return false;
|
|
}
|
|
|
|
$corrected = self::normalizeString($direction);
|
|
|
|
return $corrected === '' || $corrected === 'ltr' || $corrected === 'rtl';
|
|
}
|
|
|
|
private static function normalizeString($value)
|
|
{
|
|
return strtolower(trim($value));
|
|
}
|
|
}
|