1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
15
|
|
|
|
16
|
|
|
use Egulias\EmailValidator\EmailValidator; |
17
|
|
|
use Egulias\EmailValidator\Validation\RFCValidation; |
18
|
|
|
use const FILTER_VALIDATE_EMAIL; |
19
|
|
|
use function class_exists; |
20
|
|
|
use function filter_var; |
21
|
|
|
use function is_string; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Validates an email address. |
25
|
|
|
* |
26
|
|
|
* @author Eduardo Gulias Davis <[email protected]> |
27
|
|
|
* @author Henrique Moody <[email protected]> |
28
|
|
|
* @author Paul Karikari <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class Email extends AbstractRule |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Assigns email validator. |
34
|
|
|
* |
35
|
|
|
* @param EmailValidator $emailValidator |
36
|
|
|
*/ |
37
|
7 |
|
public function __construct(EmailValidator $emailValidator = null) |
38
|
|
|
{ |
39
|
7 |
|
$this->emailValidator = $emailValidator; |
|
|
|
|
40
|
7 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets email validator. |
44
|
|
|
* |
45
|
|
|
* @return EmailValidator | null |
46
|
|
|
*/ |
47
|
20 |
|
public function getEmailValidator() |
48
|
|
|
{ |
49
|
20 |
|
if (class_exists(EmailValidator::class) |
50
|
20 |
|
&& !$this->emailValidator instanceof EmailValidator) { |
51
|
18 |
|
$this->emailValidator = new EmailValidator(); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
20 |
|
return $this->emailValidator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
18 |
|
public function validate($input): bool |
61
|
|
|
{ |
62
|
18 |
|
$emailValidator = $this->getEmailValidator(); |
63
|
18 |
|
if (!$emailValidator instanceof EmailValidator) { |
64
|
|
|
return is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL); |
65
|
|
|
} |
66
|
|
|
|
67
|
18 |
|
if (!class_exists(RFCValidation::class)) { |
68
|
|
|
return $emailValidator->isValid($input); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
18 |
|
return $emailValidator->isValid($input, new RFCValidation()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|