1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
|
|
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
12
|
|
|
use Yiisoft\Validator\RuleInterface; |
13
|
|
|
use function function_exists; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Validates that the value is a valid email address. |
17
|
|
|
*/ |
18
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
19
|
|
|
final class Email implements RuleInterface |
20
|
|
|
{ |
21
|
|
|
use HandlerClassNameTrait; |
22
|
|
|
use RuleNameTrait; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
/** |
26
|
|
|
* @var string the regular expression used to validate value. |
27
|
|
|
* |
28
|
|
|
* @link http://www.regular-expressions.info/email.html |
29
|
|
|
*/ |
30
|
|
|
public string $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/', |
31
|
|
|
/** |
32
|
|
|
* @var string the regular expression used to validate email addresses with the name part. This property is used |
33
|
|
|
* only when {@see $allowName} is `true`. |
34
|
|
|
* |
35
|
|
|
* @see $allowName |
36
|
|
|
*/ |
37
|
|
|
public string $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/', |
38
|
|
|
/** |
39
|
|
|
* @var string the regular expression used to validate complex emails when {@see $enableIDN} is `true`. |
40
|
|
|
*/ |
41
|
|
|
public string $idnEmailPattern = '/^([a-zA-Z0-9._%+-]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/', |
42
|
|
|
/** |
43
|
|
|
* @var bool whether to allow name in the email address (e.g. "John Smith <[email protected]>"). Defaults |
44
|
|
|
* to `false`. |
45
|
|
|
* |
46
|
|
|
* @see $fullPattern |
47
|
|
|
*/ |
48
|
|
|
public bool $allowName = false, |
49
|
|
|
/** |
50
|
|
|
* @var bool whether to check whether the email's domain exists and has either an A or MX record. |
51
|
|
|
* Be aware that this check can fail due to temporary DNS problems even if the email address is |
52
|
|
|
* valid and an email would be deliverable. Defaults to `false`. |
53
|
|
|
*/ |
54
|
|
|
public bool $checkDNS = false, |
55
|
|
|
/** |
56
|
|
|
* @var bool whether validation process should take into account IDN (internationalized domain |
57
|
|
|
* names). Defaults to false meaning that validation of emails containing IDN will always fail. |
58
|
|
|
* Note that in order to use IDN validation you have to install and enable `intl` PHP extension, |
59
|
|
|
* otherwise an exception would be thrown. |
60
|
|
|
*/ |
61
|
|
|
public bool $enableIDN = false, |
62
|
|
|
public string $message = 'This value is not a valid email address.', |
63
|
|
|
public bool $skipOnEmpty = false, |
64
|
|
|
public bool $skipOnError = false, |
65
|
|
|
public ?Closure $when = null, |
66
|
|
|
) { |
67
|
|
|
if ($enableIDN && !function_exists('idn_to_ascii')) { |
68
|
|
|
throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
public function getOptions(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
4 |
|
'pattern' => $this->pattern, |
76
|
4 |
|
'fullPattern' => $this->fullPattern, |
77
|
4 |
|
'idnEmailPattern' => $this->idnEmailPattern, |
78
|
4 |
|
'allowName' => $this->allowName, |
79
|
4 |
|
'checkDNS' => $this->checkDNS, |
80
|
4 |
|
'enableIDN' => $this->enableIDN, |
81
|
|
|
'message' => [ |
82
|
4 |
|
'message' => $this->message, |
83
|
|
|
], |
84
|
4 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
85
|
4 |
|
'skipOnError' => $this->skipOnError, |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|