class TimezoneValidator
Validates whether a value is a valid timezone identifier.
@author Javier Spagnoletti <phansys@gmail.com> @author Hugo Hamon <hugohamon@neuf.fr>
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Symfony\Component\Validator\Constraints\TimezoneValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of TimezoneValidator
File
-
vendor/
symfony/ validator/ Constraints/ TimezoneValidator.php, line 27
Namespace
Symfony\Component\Validator\ConstraintsView source
class TimezoneValidator extends ConstraintValidator {
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();
}
private static function getPhpTimezones(int $zone, ?string $countryCode = null) : array {
if (null !== $countryCode) {
try {
return @\DateTimeZone::listIdentifiers($zone, $countryCode) ?: [];
} catch (\ValueError) {
return [];
}
}
return \DateTimeZone::listIdentifiers($zone);
}
private static function getIntlTimezones(int $zone, ?string $countryCode = null) : array {
if (!class_exists(Timezones::class)) {
return [];
}
if (null !== $countryCode) {
try {
return Timezones::forCountryCode($countryCode);
} catch (MissingResourceException) {
return [];
}
}
$timezones = Timezones::getIds();
if (\DateTimeZone::ALL === (\DateTimeZone::ALL & $zone)) {
return $timezones;
}
$filtered = [];
foreach ((new \ReflectionClass(\DateTimeZone::class))->getConstants() as $const => $flag) {
if ($flag !== ($flag & $zone)) {
continue;
}
$filtered[] = array_filter($timezones, static fn($id) => 0 === stripos($id, $const . '/'));
}
return $filtered ? array_merge(...$filtered) : [];
}
}
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"). |
|
TimezoneValidator::getIntlTimezones | private static | function | ||
TimezoneValidator::getPhpTimezones | private static | function | ||
TimezoneValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |