class GranularityStringConstraintValidator in Duration Field 8.2
Same name and namespace in other branches
- 3.0.x src/Plugin/Validation/Constraint/GranularityStringConstraintValidator.php \Drupal\duration_field\Plugin\Validation\Constraint\GranularityStringConstraintValidator
Validates the granularity_string constraint.
Hierarchy
- class \Drupal\duration_field\Plugin\Validation\Constraint\GranularityStringConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of GranularityStringConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ GranularityStringConstraintValidator.php, line 11
Namespace
Drupal\duration_field\Plugin\Validation\ConstraintView source
class GranularityStringConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
if (is_array($items)) {
foreach ($items as $item) {
if (!$this
->isGranularityString($item)) {
// The value is not an integer, so a violation, aka error, is applied.
// The type of violation applied comes from the constraint description
// in step 1.
$this->context
->addViolation($constraint->notValidGranularityString, [
'%value' => (string) $item,
]);
}
}
}
else {
if (!$this
->isGranularityString($items)) {
$this->context
->addViolation($constraint->notValidGranularityString, [
'%value' => (string) $items,
]);
}
}
}
/**
* Check if a string is a valid granularity string.
*
* @param mixed $value
* The item to check as a granularity string.
*
* @return bool
* TRUE if the given value is a valid granularity string. FALSE if it is
* not.
*/
private function isGranularityString($value) {
if (is_string($value)) {
return preg_match(GraularityStringInterface::GRANULARITY_STRING_PATTERN, $value);
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GranularityStringConstraintValidator:: |
private | function | Check if a string is a valid granularity string. | |
GranularityStringConstraintValidator:: |
public | function | Checks if the passed value is valid. |