AutorÃa | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Validator;
use Laminas\Validator\AbstractValidator;
class HtmlColorCheck extends AbstractValidator
{
const PATRON = '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/';
const INVALID = 'Invalid';
/**
* Validation failure message template definitions
*
* @var array
*/
protected $messageTemplates = [
self::INVALID => 'Must contain a # followed by 3 or 6 characters from A-F, a-f or 0-9',
];
/**
* Returns true if and only if $value contains only hexadecimal digit characters
*
* @param string $value
* @return bool
*/
public function isValid($value)
{
$matches = [];
preg_match (self::PATRON, $value, $matches);
if (empty($matches)) {
$valid = false;
$this->error(self::INVALID);
} else {
$valid = true;
$this->setValue($matches[0]);
}
return $valid;
}
}