vendor/symfony/validator/Constraints/Email.php line 26

Open in your IDE?
  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 Egulias\EmailValidator\EmailValidator as StrictEmailValidator;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Exception\InvalidArgumentException;
  14. use Symfony\Component\Validator\Exception\LogicException;
  15. /**
  16. * @Annotation
  17. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  22. class Email extends Constraint
  23. {
  24. public const VALIDATION_MODE_HTML5_ALLOW_NO_TLD = 'html5-allow-no-tld';
  25. public const VALIDATION_MODE_HTML5 = 'html5';
  26. public const VALIDATION_MODE_STRICT = 'strict';
  27. /**
  28. * @deprecated since Symfony 6.2, use VALIDATION_MODE_HTML5 instead
  29. */
  30. public const VALIDATION_MODE_LOOSE = 'loose';
  31. public const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310';
  32. public const VALIDATION_MODES = [
  33. self::VALIDATION_MODE_HTML5_ALLOW_NO_TLD,
  34. self::VALIDATION_MODE_HTML5,
  35. self::VALIDATION_MODE_STRICT,
  36. self::VALIDATION_MODE_LOOSE,
  37. ];
  38. protected const ERROR_NAMES = [
  39. self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
  40. ];
  41. /**
  42. * @deprecated since Symfony 6.1, use const ERROR_NAMES instead
  43. */
  44. protected static $errorNames = self::ERROR_NAMES;
  45. public $message = 'This value is not a valid email address.';
  46. public $mode;
  47. /** @var callable|null */
  48. public $normalizer;
  49. public function __construct(
  50. ?array $options = null,
  51. ?string $message = null,
  52. ?string $mode = null,
  53. ?callable $normalizer = null,
  54. ?array $groups = null,
  55. mixed $payload = null,
  56. ) {
  57. if (\is_array($options) && \array_key_exists('mode', $options) && !\in_array($options['mode'], self::VALIDATION_MODES, true)) {
  58. throw new InvalidArgumentException('The "mode" parameter value is not valid.');
  59. }
  60. if (null !== $mode && !\in_array($mode, self::VALIDATION_MODES, true)) {
  61. throw new InvalidArgumentException('The "mode" parameter value is not valid.');
  62. }
  63. parent::__construct($options, $groups, $payload);
  64. $this->message = $message ?? $this->message;
  65. $this->mode = $mode ?? $this->mode;
  66. $this->normalizer = $normalizer ?? $this->normalizer;
  67. if (self::VALIDATION_MODE_LOOSE === $this->mode) {
  68. trigger_deprecation('symfony/validator', '6.2', 'The "%s" mode is deprecated. It will be removed in 7.0 and the default mode will be changed to "%s".', self::VALIDATION_MODE_LOOSE, self::VALIDATION_MODE_HTML5);
  69. }
  70. if (self::VALIDATION_MODE_STRICT === $this->mode && !class_exists(StrictEmailValidator::class)) {
  71. throw new LogicException(\sprintf('The "egulias/email-validator" component is required to use the "%s" constraint in strict mode. Try running "composer require egulias/email-validator".', __CLASS__));
  72. }
  73. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  74. throw new InvalidArgumentException(\sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  75. }
  76. }
  77. }