Timezone.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. /**
  14. * @Annotation
  15. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16. *
  17. * @author Javier Spagnoletti <phansys@gmail.com>
  18. * @author Hugo Hamon <hugohamon@neuf.fr>
  19. */
  20. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  21. class Timezone extends Constraint
  22. {
  23. public const TIMEZONE_IDENTIFIER_ERROR = '5ce113e6-5e64-4ea2-90fe-d2233956db13';
  24. public const TIMEZONE_IDENTIFIER_IN_ZONE_ERROR = 'b57767b1-36c0-40ac-a3d7-629420c775b8';
  25. public const TIMEZONE_IDENTIFIER_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b';
  26. public const TIMEZONE_IDENTIFIER_INTL_ERROR = '45863c26-88dc-41ba-bf53-c73bd1f7e90d';
  27. public $zone = \DateTimeZone::ALL;
  28. public $countryCode;
  29. public $intlCompatible = false;
  30. public $message = 'This value is not a valid timezone.';
  31. protected static $errorNames = [
  32. self::TIMEZONE_IDENTIFIER_ERROR => 'TIMEZONE_IDENTIFIER_ERROR',
  33. self::TIMEZONE_IDENTIFIER_IN_ZONE_ERROR => 'TIMEZONE_IDENTIFIER_IN_ZONE_ERROR',
  34. self::TIMEZONE_IDENTIFIER_IN_COUNTRY_ERROR => 'TIMEZONE_IDENTIFIER_IN_COUNTRY_ERROR',
  35. self::TIMEZONE_IDENTIFIER_INTL_ERROR => 'TIMEZONE_IDENTIFIER_INTL_ERROR',
  36. ];
  37. /**
  38. * {@inheritdoc}
  39. *
  40. * @param int|array|null $zone A combination of {@see \DateTimeZone} class constants or a set of options
  41. */
  42. public function __construct(
  43. $zone = null,
  44. string $message = null,
  45. string $countryCode = null,
  46. bool $intlCompatible = null,
  47. array $groups = null,
  48. $payload = null,
  49. array $options = []
  50. ) {
  51. if (\is_array($zone)) {
  52. $options = array_merge($zone, $options);
  53. } elseif (null !== $zone) {
  54. $options['value'] = $zone;
  55. }
  56. parent::__construct($options, $groups, $payload);
  57. $this->message = $message ?? $this->message;
  58. $this->countryCode = $countryCode ?? $this->countryCode;
  59. $this->intlCompatible = $intlCompatible ?? $this->intlCompatible;
  60. if (null === $this->countryCode) {
  61. if (0 >= $this->zone || \DateTimeZone::ALL_WITH_BC < $this->zone) {
  62. throw new ConstraintDefinitionException('The option "zone" must be a valid range of "\DateTimeZone" constants.');
  63. }
  64. } elseif (\DateTimeZone::PER_COUNTRY !== (\DateTimeZone::PER_COUNTRY & $this->zone)) {
  65. throw new ConstraintDefinitionException('The option "countryCode" can only be used when the "zone" option is configured with "\DateTimeZone::PER_COUNTRY".');
  66. }
  67. if ($this->intlCompatible && !class_exists(\IntlTimeZone::class)) {
  68. throw new ConstraintDefinitionException('The option "intlCompatible" can only be used when the PHP intl extension is available.');
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getDefaultOption()
  75. {
  76. return 'zone';
  77. }
  78. }