Country.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Intl\Countries;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Exception\LogicException;
  14. /**
  15. * @Annotation
  16. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. */
  20. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  21. class Country extends Constraint
  22. {
  23. public const NO_SUCH_COUNTRY_ERROR = '8f900c12-61bd-455d-9398-996cd040f7f0';
  24. protected static $errorNames = [
  25. self::NO_SUCH_COUNTRY_ERROR => 'NO_SUCH_COUNTRY_ERROR',
  26. ];
  27. public $message = 'This value is not a valid country.';
  28. public $alpha3 = false;
  29. public function __construct(
  30. array $options = null,
  31. string $message = null,
  32. bool $alpha3 = null,
  33. array $groups = null,
  34. $payload = null
  35. ) {
  36. if (!class_exists(Countries::class)) {
  37. throw new LogicException('The Intl component is required to use the Country constraint. Try running "composer require symfony/intl".');
  38. }
  39. parent::__construct($options, $groups, $payload);
  40. $this->message = $message ?? $this->message;
  41. $this->alpha3 = $alpha3 ?? $this->alpha3;
  42. }
  43. }