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 = 'html5';
  25. public const VALIDATION_MODE_STRICT = 'strict';
  26. public const VALIDATION_MODE_LOOSE = 'loose';
  27. public const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310';
  28. protected static $errorNames = [
  29. self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
  30. ];
  31. /**
  32. * @var string[]
  33. *
  34. * @internal
  35. */
  36. public static $validationModes = [
  37. self::VALIDATION_MODE_HTML5,
  38. self::VALIDATION_MODE_STRICT,
  39. self::VALIDATION_MODE_LOOSE,
  40. ];
  41. public $message = 'This value is not a valid email address.';
  42. public $mode;
  43. public $normalizer;
  44. public function __construct(
  45. ?array $options = null,
  46. ?string $message = null,
  47. ?string $mode = null,
  48. ?callable $normalizer = null,
  49. ?array $groups = null,
  50. $payload = null
  51. ) {
  52. if (\is_array($options) && \array_key_exists('mode', $options) && !\in_array($options['mode'], self::$validationModes, true)) {
  53. throw new InvalidArgumentException('The "mode" parameter value is not valid.');
  54. }
  55. if (null !== $mode && !\in_array($mode, self::$validationModes, true)) {
  56. throw new InvalidArgumentException('The "mode" parameter value is not valid.');
  57. }
  58. parent::__construct($options, $groups, $payload);
  59. $this->message = $message ?? $this->message;
  60. $this->mode = $mode ?? $this->mode;
  61. $this->normalizer = $normalizer ?? $this->normalizer;
  62. if (self::VALIDATION_MODE_STRICT === $this->mode && !class_exists(StrictEmailValidator::class)) {
  63. throw new LogicException(sprintf('The "egulias/email-validator" component is required to use the "%s" constraint in strict mode.', __CLASS__));
  64. }
  65. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  66. throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  67. }
  68. }
  69. }