|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
|
6
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
|
7
|
|
|
use Egulias\EmailValidator\Validation\Exception\EmptyValidationList; |
|
8
|
|
|
use Egulias\EmailValidator\Result\MultipleErrors; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class MultipleValidationWithAnd implements EmailValidation |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* If one of validations fails, the remaining validations will be skept. |
|
14
|
|
|
* This means MultipleErrors will only contain a single error, the first found. |
|
15
|
|
|
*/ |
|
16
|
|
|
const STOP_ON_ERROR = 0; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* All of validations will be invoked even if one of them got failure. |
|
20
|
|
|
* So MultipleErrors will contain all causes. |
|
21
|
|
|
*/ |
|
22
|
|
|
const ALLOW_ALL_ERRORS = 1; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EmailValidation[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $validations = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $warnings = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var MultipleErrors|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $error; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
private $mode; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param EmailValidation[] $validations The validations. |
|
46
|
|
|
* @param int $mode The validation mode (one of the constants). |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(array $validations, $mode = self::ALLOW_ALL_ERRORS) |
|
49
|
|
|
{ |
|
50
|
|
|
if (count($validations) == 0) { |
|
51
|
|
|
throw new EmptyValidationList(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->validations = $validations; |
|
55
|
|
|
$this->mode = $mode; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function isValid($email, EmailLexer $emailLexer) |
|
62
|
|
|
{ |
|
63
|
|
|
$result = true; |
|
64
|
|
|
foreach ($this->validations as $validation) { |
|
65
|
|
|
$emailLexer->reset(); |
|
66
|
|
|
$validationResult = $validation->isValid($email, $emailLexer); |
|
67
|
|
|
$result = $result && $validationResult; |
|
68
|
|
|
$this->warnings = array_merge($this->warnings, $validation->getWarnings()); |
|
69
|
|
|
if (!$validationResult) { |
|
70
|
|
|
$this->processError($validation); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($this->shouldStop($result)) { |
|
74
|
|
|
break; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function initErrorStorage() |
|
82
|
|
|
{ |
|
83
|
|
|
if (null === $this->error) { |
|
84
|
|
|
$this->error = new MultipleErrors(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function processError(EmailValidation $validation) |
|
89
|
|
|
{ |
|
90
|
|
|
if (null !== $validation->getError()) { |
|
91
|
|
|
$this->initErrorStorage(); |
|
92
|
|
|
$this->error->addReason($validation->getError()->reason()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param bool $result |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
private function shouldStop($result) |
|
102
|
|
|
{ |
|
103
|
|
|
return !$result && $this->mode === self::STOP_ON_ERROR; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the validation errors. |
|
108
|
|
|
* |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getError() : ?InvalidEmail |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->error; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getWarnings() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->warnings; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: