class TimeValidator
@author Bernhard Schussek <bschussek@gmail.com>
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Symfony\Component\Validator\Constraints\TimeValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of TimeValidator
File
-
vendor/
symfony/ validator/ Constraints/ TimeValidator.php, line 22
Namespace
Symfony\Component\Validator\ConstraintsView source
class TimeValidator extends ConstraintValidator {
public const PATTERN = '/^(\\d{2}):(\\d{2}):(\\d{2})$/D';
public const PATTERN_WITHOUT_SECONDS = '/^(\\d{2}):(\\d{2})$/D';
/**
* Checks whether a time is valid.
*
* @internal
*/
public static function checkTime(int $hour, int $minute, float $second) : bool {
return $hour >= 0 && $hour < 24 && $minute >= 0 && $minute < 60 && $second >= 0 && $second < 60;
}
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Time) {
throw new UnexpectedTypeException($constraint, Time::class);
}
if (null === $value || '' === $value) {
return;
}
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if (!preg_match($constraint->withSeconds ? static::PATTERN : static::PATTERN_WITHOUT_SECONDS, $value, $matches)) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Time::INVALID_FORMAT_ERROR)
->addViolation();
return;
}
if (!self::checkTime($matches[1], $matches[2], $constraint->withSeconds ? $matches[3] : 0)) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Time::INVALID_TIME_ERROR)
->addViolation();
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ConstraintValidator::$context | protected | property | ||
ConstraintValidator::formatTypeOf | protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator::formatValue | protected | function | Returns a string representation of the value. | |
ConstraintValidator::formatValues | protected | function | Returns a string representation of a list of values. | |
ConstraintValidator::initialize | public | function | Initializes the constraint validator. | Overrides ConstraintValidatorInterface::initialize |
ConstraintValidator::OBJECT_TO_STRING | public | constant | Whether to cast objects with a "__toString()" method to strings. | |
ConstraintValidator::PRETTY_DATE | public | constant | Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter} (if it is available) or as RFC-3339 dates ("Y-m-d H:i:s"). |
|
TimeValidator::checkTime | public static | function | Checks whether a time is valid. | |
TimeValidator::PATTERN | public | constant | ||
TimeValidator::PATTERN_WITHOUT_SECONDS | public | constant | ||
TimeValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |