|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\ValidationBundle\Validator\Constraints; |
|
12
|
|
|
|
|
13
|
|
|
use Egulias\EmailValidator\Validation\RFCValidation; |
|
14
|
|
|
use Symfony\Component\Validator\Constraint; |
|
15
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
16
|
|
|
use Symfony\Component\Validator\Exception\RuntimeException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class EmailValidator |
|
20
|
|
|
* @package LoginCidadao\ValidationBundle\Validator\Constraints |
|
21
|
|
|
* |
|
22
|
|
|
* TODO: remove after update to Symfony 4.1 |
|
23
|
|
|
* @codeCoverageIgnore |
|
24
|
|
|
*/ |
|
25
|
|
|
class EmailValidator extends \Symfony\Component\Validator\Constraints\EmailValidator |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function validate($value, Constraint $constraint) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($constraint->strict) { |
|
33
|
|
|
if (!class_exists('\Egulias\EmailValidator\EmailValidator') || !class_exists('\Egulias\EmailValidator\Validation\RFCValidation')) { |
|
34
|
|
|
throw new RuntimeException('Strict email validation requires egulias/email-validator'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$strictValidator = new \Egulias\EmailValidator\EmailValidator(); |
|
38
|
|
|
|
|
39
|
|
|
if (!$strictValidator->isValid($value, new RFCValidation())) { |
|
40
|
|
|
if ($this->context instanceof ExecutionContextInterface) { |
|
|
|
|
|
|
41
|
|
|
$this->context->buildViolation($constraint->message) |
|
42
|
|
|
->setParameter('{{ value }}', $this->formatValue($value)) |
|
43
|
|
|
->setCode(Email::INVALID_FORMAT_ERROR) |
|
44
|
|
|
->addViolation(); |
|
45
|
|
|
} else { |
|
46
|
|
|
$this->buildViolation($constraint->message) |
|
47
|
|
|
->setParameter('{{ value }}', $this->formatValue($value)) |
|
48
|
|
|
->setCode(Email::INVALID_FORMAT_ERROR) |
|
49
|
|
|
->addViolation(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
} else { |
|
55
|
|
|
parent::validate($value, $constraint); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|