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.

87 lines
1.6 KiB
PHP

<?php
namespace Artmark\Forms\Attributes;
/**
* Description of LanguageAttribute
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
trait LanguageAttribute
{
/**
*
* @var string
*/
private $language = '';
/**
* HTML lang attribute of the tag
* @return string
*/
public function language()
{
return $this->language;
}
/**
*
* @return boolean
*/
public function hasLanguage()
{
return $this->language !== '';
}
/**
*
* @param string $newLanguage
* @return $this
*/
public function setLanguage($newLanguage)
{
self::checkLanguage($newLanguage);
$this->language = trim($newLanguage);
return $this;
}
/**
*
* @return $this
*/
public function removeLanguage()
{
$this->language = '';
return $this;
}
protected function appendLanguageAttribute(array & $attributes)
{
if ($this->hasLanguage())
{
$attributes['lang'] = $this->language;
}
}
public static function checkLanguage($language)
{
if (!self::isCorrectLanguage($language)) {
throw new AttributeException('Incorrect language value: `' . strval($language) . '`');
}
}
public static function isCorrectLanguage($language)
{
if (!is_string($language)) {
return false;
}
$cleaned = trim($language);
return $cleaned === '' || preg_match('/^[A-Za-z]{2}([_\-][A-Za-z]{2}){0,1}$/', $cleaned);
}
}