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

Breadcrumb

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

function TimezoneValidator::validate

Overrides ConstraintValidatorInterface::validate

File

vendor/symfony/validator/Constraints/TimezoneValidator.php, line 29

Class

TimezoneValidator
Validates whether a value is a valid timezone identifier.

Namespace

Symfony\Component\Validator\Constraints

Code

public function validate(mixed $value, Constraint $constraint) : void {
    if (!$constraint instanceof Timezone) {
        throw new UnexpectedTypeException($constraint, Timezone::class);
    }
    if (null === $value || '' === $value) {
        return;
    }
    if (!\is_scalar($value) && !$value instanceof \Stringable) {
        throw new UnexpectedValueException($value, 'string');
    }
    $value = (string) $value;
    if ($constraint->intlCompatible && 'Etc/Unknown' === \IntlTimeZone::createTimeZone($value)->getID()) {
        $this->context
            ->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode(Timezone::TIMEZONE_IDENTIFIER_INTL_ERROR)
            ->addViolation();
        return;
    }
    if (\in_array($value, self::getPhpTimezones($constraint->zone, $constraint->countryCode), true) || \in_array($value, self::getIntlTimezones($constraint->zone, $constraint->countryCode), true)) {
        return;
    }
    if ($constraint->countryCode) {
        $code = Timezone::TIMEZONE_IDENTIFIER_IN_COUNTRY_ERROR;
    }
    elseif (\DateTimeZone::ALL !== $constraint->zone) {
        $code = Timezone::TIMEZONE_IDENTIFIER_IN_ZONE_ERROR;
    }
    else {
        $code = Timezone::TIMEZONE_IDENTIFIER_ERROR;
    }
    $this->context
        ->buildViolation($constraint->message)
        ->setParameter('{{ value }}', $this->formatValue($value))
        ->setCode($code)
        ->addViolation();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal