GranularityStringConstraintValidator.php in Duration Field 8.2
File
src/Plugin/Validation/Constraint/GranularityStringConstraintValidator.php
View source
<?php
namespace Drupal\duration_field\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class GranularityStringConstraintValidator extends ConstraintValidator {
public function validate($items, Constraint $constraint) {
if (is_array($items)) {
foreach ($items as $item) {
if (!$this
->isGranularityString($item)) {
$this->context
->addViolation($constraint->notValidGranularityString, [
'%value' => (string) $item,
]);
}
}
}
else {
if (!$this
->isGranularityString($items)) {
$this->context
->addViolation($constraint->notValidGranularityString, [
'%value' => (string) $items,
]);
}
}
}
private function isGranularityString($value) {
if (is_string($value)) {
return preg_match(GraularityStringInterface::GRANULARITY_STRING_PATTERN, $value);
}
return FALSE;
}
}