Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. WeekValidator.php

class WeekValidator

@author Alexandre Daubois <alex.daubois@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
    • class \Symfony\Component\Validator\Constraints\WeekValidator extends \Symfony\Component\Validator\ConstraintValidator

Expanded class hierarchy of WeekValidator

File

vendor/symfony/validator/Constraints/WeekValidator.php, line 22

Namespace

Symfony\Component\Validator\Constraints
View source
final class WeekValidator extends ConstraintValidator {
    public function validate(mixed $value, Constraint $constraint) : void {
        if (!$constraint instanceof Week) {
            throw new UnexpectedTypeException($constraint, Week::class);
        }
        if (null === $value) {
            return;
        }
        if (!\is_string($value) && !$value instanceof \Stringable) {
            throw new UnexpectedValueException($value, 'string');
        }
        if (!preg_match('/^\\d{4}-W(0[1-9]|[1-4][0-9]|5[0-3])$/D', $value)) {
            $this->context
                ->buildViolation($constraint->invalidFormatMessage)
                ->setCode(Week::INVALID_FORMAT_ERROR)
                ->addViolation();
            return;
        }
        [
            $year,
            $weekNumber,
        ] = \explode('-W', $value, 2);
        $weeksInYear = (int) \date('W', \mktime(0, 0, 0, 12, 28, $year));
        if ($weekNumber > $weeksInYear) {
            $this->context
                ->buildViolation($constraint->invalidWeekNumberMessage)
                ->setCode(Week::INVALID_WEEK_NUMBER_ERROR)
                ->setParameter('{{ value }}', $value)
                ->addViolation();
            return;
        }
        if ($constraint->min) {
            [
                $minYear,
                $minWeekNumber,
            ] = \explode('-W', $constraint->min, 2);
            if ($year < $minYear || $year === $minYear && $weekNumber < $minWeekNumber) {
                $this->context
                    ->buildViolation($constraint->tooLowMessage)
                    ->setCode(Week::TOO_LOW_ERROR)
                    ->setInvalidValue($value)
                    ->setParameter('{{ min }}', $constraint->min)
                    ->addViolation();
                return;
            }
        }
        if ($constraint->max) {
            [
                $maxYear,
                $maxWeekNumber,
            ] = \explode('-W', $constraint->max, 2);
            if ($year > $maxYear || $year === $maxYear && $weekNumber > $maxWeekNumber) {
                $this->context
                    ->buildViolation($constraint->tooHighMessage)
                    ->setCode(Week::TOO_HIGH_ERROR)
                    ->setInvalidValue($value)
                    ->setParameter('{{ max }}', $constraint->max)
                    ->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 &quot;__toString()&quot; 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 (&quot;Y-m-d H:i:s&quot;).
WeekValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
RSS feed
Powered by Drupal