|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraint; |
| 15 | +use Symfony\Component\Validator\ConstraintValidator; |
| 16 | +use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 17 | +use Symfony\Component\Validator\Exception\UnexpectedValueException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Validates whether a value is a valid timezone identifier. |
| 21 | + * |
| 22 | + * @author Javier Spagnoletti <phansys@gmail.com> |
| 23 | + * @author Hugo Hamon <hugohamon@neuf.fr> |
| 24 | + */ |
| 25 | +class TimezoneValidator extends ConstraintValidator |
| 26 | +{ |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function validate($value, Constraint $constraint) |
| 31 | + { |
| 32 | + if (!$constraint instanceof Timezone) { |
| 33 | + throw new UnexpectedTypeException($constraint, Timezone::class); |
| 34 | + } |
| 35 | + |
| 36 | + if (null === $value || '' === $value) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { |
| 41 | + throw new UnexpectedValueException($value, 'string'); |
| 42 | + } |
| 43 | + |
| 44 | + $value = (string) $value; |
| 45 | + |
| 46 | + // @see: https://bugs.php.net/bug.php?id=75928 |
| 47 | + if ($constraint->countryCode) { |
| 48 | + $timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode); |
| 49 | + } else { |
| 50 | + $timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone); |
| 51 | + } |
| 52 | + |
| 53 | + if ($timezoneIds && \in_array($value, $timezoneIds, true)) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if ($constraint->countryCode) { |
| 58 | + $code = Timezone::TIMEZONE_IDENTIFIER_IN_COUNTRY_ERROR; |
| 59 | + } elseif (\DateTimeZone::ALL !== $constraint->zone) { |
| 60 | + $code = Timezone::TIMEZONE_IDENTIFIER_IN_ZONE_ERROR; |
| 61 | + } else { |
| 62 | + $code = Timezone::TIMEZONE_IDENTIFIER_ERROR; |
| 63 | + } |
| 64 | + |
| 65 | + $this->context->buildViolation($constraint->message) |
| 66 | + ->setParameter('{{ value }}', $this->formatValue($value)) |
| 67 | + ->setCode($code) |
| 68 | + ->addViolation(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + */ |
| 74 | + public function getDefaultOption() |
| 75 | + { |
| 76 | + return 'zone'; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritdoc} |
| 81 | + */ |
| 82 | + protected function formatValue($value, $format = 0) |
| 83 | + { |
| 84 | + $value = parent::formatValue($value, $format); |
| 85 | + |
| 86 | + if (!$value || \DateTimeZone::PER_COUNTRY === $value) { |
| 87 | + return $value; |
| 88 | + } |
| 89 | + |
| 90 | + return array_search($value, (new \ReflectionClass(\DateTimeZone::class))->getConstants(), true) ?: $value; |
| 91 | + } |
| 92 | +} |
0 commit comments