Email.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 => 'STRICT_CHECK_FAILED_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. parent::__construct($options, $groups, $payload);
  56. $this->message = $message ?? $this->message;
  57. $this->mode = $mode ?? $this->mode;
  58. $this->normalizer = $normalizer ?? $this->normalizer;
  59. if (self::VALIDATION_MODE_STRICT === $this->mode && !class_exists(StrictEmailValidator::class)) {
  60. throw new LogicException(sprintf('The "egulias/email-validator" component is required to use the "%s" constraint in strict mode.', __CLASS__));
  61. }
  62. if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
  63. throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
  64. }
  65. }
  66. }