class HexColorConstraintValidator in Color API 8
Validates the hexadecimal_color constraint.
Hierarchy
- class \Drupal\colorapi\Plugin\Validation\Constraint\HexColorConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of HexColorConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ HexColorConstraintValidator.php, line 14
Namespace
Drupal\colorapi\Plugin\Validation\ConstraintView source
class HexColorConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The Color API service.
*
* @var \Drupal\colorapi\Service\ColorApiServiceInterface
*/
protected $colorapiService;
/**
* Constructs a HexColorConstraintalidator object.
*
* @param \Drupal\colorapi\Service\ColorapiServiceInterface $colorapiService
* THe Color API service.
*/
public function __construct(ColorapiServiceInterface $colorapiService) {
$this->colorapiService = $colorapiService;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('colorapi.service'));
}
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
if (is_array($items)) {
foreach ($items as $item) {
if (!$this
->isHexColorString($item)) {
// The value is not a valid hexadecimal color string, so a violation,
// aka error, is applied.
$this->context
->addViolation($constraint->notValidHexadecimalColorString, [
'%value' => (string) $item,
]);
}
}
}
elseif (!$this
->isHexColorString($items)) {
$this->context
->addViolation($constraint->notValidHexadecimalColorString, [
'%value' => (string) $items,
]);
}
}
/**
* Check if a string is a valid hexadecimal color string.
*
* @param mixed $value
* The item to check as a hexadecimal color string.
*
* @return bool
* TRUE if the given value is a valid hexadecimal color string. FALSE if it
* is not.
*/
private function isHexColorString($value) {
return $this->colorapiService
->isValidHexadecimalColorString($value);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HexColorConstraintValidator:: |
protected | property | The Color API service. | |
HexColorConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
HexColorConstraintValidator:: |
private | function | Check if a string is a valid hexadecimal color string. | |
HexColorConstraintValidator:: |
public | function | Checks if the passed value is valid. | |
HexColorConstraintValidator:: |
public | function | Constructs a HexColorConstraintalidator object. |