/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/EmailValidator.php

https://gitlab.com/Marwamimo/Crowdrise_Web · PHP · 126 lines · 69 code · 21 blank · 36 comment · 16 complexity · ca657274427be0beb675cd1b6915fcc3 MD5 · raw file

  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\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\RuntimeException;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  15. /**
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @api
  19. */
  20. class EmailValidator extends ConstraintValidator
  21. {
  22. /**
  23. * isStrict
  24. *
  25. * @var bool
  26. */
  27. private $isStrict;
  28. public function __construct($strict = false)
  29. {
  30. $this->isStrict = $strict;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function validate($value, Constraint $constraint)
  36. {
  37. if (!$constraint instanceof Email) {
  38. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email');
  39. }
  40. if (null === $value || '' === $value) {
  41. return;
  42. }
  43. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  44. throw new UnexpectedTypeException($value, 'string');
  45. }
  46. $value = (string) $value;
  47. if (null === $constraint->strict) {
  48. $constraint->strict = $this->isStrict;
  49. }
  50. if ($constraint->strict) {
  51. if (!class_exists('\Egulias\EmailValidator\EmailValidator')) {
  52. throw new RuntimeException('Strict email validation requires egulias/email-validator');
  53. }
  54. $strictValidator = new \Egulias\EmailValidator\EmailValidator();
  55. if (!$strictValidator->isValid($value, false, true)) {
  56. $this->buildViolation($constraint->message)
  57. ->setParameter('{{ value }}', $this->formatValue($value))
  58. ->addViolation();
  59. return;
  60. }
  61. } elseif (!preg_match('/.+\@.+\..+/', $value)) {
  62. $this->buildViolation($constraint->message)
  63. ->setParameter('{{ value }}', $this->formatValue($value))
  64. ->addViolation();
  65. return;
  66. }
  67. $host = substr($value, strpos($value, '@') + 1);
  68. // Check for host DNS resource records
  69. if ($constraint->checkMX) {
  70. if (!$this->checkMX($host)) {
  71. $this->buildViolation($constraint->message)
  72. ->setParameter('{{ value }}', $this->formatValue($value))
  73. ->addViolation();
  74. }
  75. return;
  76. }
  77. if ($constraint->checkHost && !$this->checkHost($host)) {
  78. $this->buildViolation($constraint->message)
  79. ->setParameter('{{ value }}', $this->formatValue($value))
  80. ->addViolation();
  81. }
  82. }
  83. /**
  84. * Check DNS Records for MX type.
  85. *
  86. * @param string $host Host
  87. *
  88. * @return bool
  89. */
  90. private function checkMX($host)
  91. {
  92. return checkdnsrr($host, 'MX');
  93. }
  94. /**
  95. * Check if one of MX, A or AAAA DNS RR exists.
  96. *
  97. * @param string $host Host
  98. *
  99. * @return bool
  100. */
  101. private function checkHost($host)
  102. {
  103. return $this->checkMX($host) || (checkdnsrr($host, "A") || checkdnsrr($host, "AAAA"));
  104. }
  105. }