comparison vendor/symfony/validator/Constraints/EmailValidator.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Validator\Constraints;
13
14 use Egulias\EmailValidator\Validation\EmailValidation;
15 use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
16 use Symfony\Component\Validator\Constraint;
17 use Symfony\Component\Validator\ConstraintValidator;
18 use Symfony\Component\Validator\Exception\RuntimeException;
19 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
20
21 /**
22 * @author Bernhard Schussek <bschussek@gmail.com>
23 */
24 class EmailValidator extends ConstraintValidator
25 {
26 /**
27 * @var bool
28 */
29 private $isStrict;
30
31 public function __construct($strict = false)
32 {
33 $this->isStrict = $strict;
34 }
35
36 /**
37 * {@inheritdoc}
38 */
39 public function validate($value, Constraint $constraint)
40 {
41 if (!$constraint instanceof Email) {
42 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email');
43 }
44
45 if (null === $value || '' === $value) {
46 return;
47 }
48
49 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
50 throw new UnexpectedTypeException($value, 'string');
51 }
52
53 $value = (string) $value;
54
55 if (null === $constraint->strict) {
56 $constraint->strict = $this->isStrict;
57 }
58
59 if ($constraint->strict) {
60 if (!class_exists('\Egulias\EmailValidator\EmailValidator')) {
61 throw new RuntimeException('Strict email validation requires egulias/email-validator ~1.2|~2.0');
62 }
63
64 $strictValidator = new \Egulias\EmailValidator\EmailValidator();
65
66 if (interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, new NoRFCWarningsValidation())) {
67 $this->context->buildViolation($constraint->message)
68 ->setParameter('{{ value }}', $this->formatValue($value))
69 ->setCode(Email::INVALID_FORMAT_ERROR)
70 ->addViolation();
71
72 return;
73 } elseif (!interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, false, true)) {
74 $this->context->buildViolation($constraint->message)
75 ->setParameter('{{ value }}', $this->formatValue($value))
76 ->setCode(Email::INVALID_FORMAT_ERROR)
77 ->addViolation();
78
79 return;
80 }
81 } elseif (!preg_match('/^.+\@\S+\.\S+$/', $value)) {
82 $this->context->buildViolation($constraint->message)
83 ->setParameter('{{ value }}', $this->formatValue($value))
84 ->setCode(Email::INVALID_FORMAT_ERROR)
85 ->addViolation();
86
87 return;
88 }
89
90 $host = (string) substr($value, strrpos($value, '@') + 1);
91
92 // Check for host DNS resource records
93 if ($constraint->checkMX) {
94 if (!$this->checkMX($host)) {
95 $this->context->buildViolation($constraint->message)
96 ->setParameter('{{ value }}', $this->formatValue($value))
97 ->setCode(Email::MX_CHECK_FAILED_ERROR)
98 ->addViolation();
99 }
100
101 return;
102 }
103
104 if ($constraint->checkHost && !$this->checkHost($host)) {
105 $this->context->buildViolation($constraint->message)
106 ->setParameter('{{ value }}', $this->formatValue($value))
107 ->setCode(Email::HOST_CHECK_FAILED_ERROR)
108 ->addViolation();
109 }
110 }
111
112 /**
113 * Check DNS Records for MX type.
114 *
115 * @param string $host Host
116 *
117 * @return bool
118 */
119 private function checkMX($host)
120 {
121 return '' !== $host && checkdnsrr($host, 'MX');
122 }
123
124 /**
125 * Check if one of MX, A or AAAA DNS RR exists.
126 *
127 * @param string $host Host
128 *
129 * @return bool
130 */
131 private function checkHost($host)
132 {
133 return '' !== $host && ($this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')));
134 }
135 }