Passed
Pull Request — master (#333)
by Sergei
02:38
created

Email   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
eloc 25
dl 0
loc 122
ccs 30
cts 31
cp 0.9677
rs 10
c 3
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdnEmailPattern() 0 3 1
A isAllowName() 0 3 1
A getFullPattern() 0 3 1
A getHandlerClassName() 0 3 1
A getOptions() 0 14 1
A isEnableIDN() 0 3 1
A getName() 0 3 1
A getMessage() 0 3 1
A __construct() 0 52 3
A getPattern() 0 3 1
A isCheckDNS() 0 3 1
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\SkipOnEmptyTrait;
11
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
12
use Yiisoft\Validator\Rule\Trait\WhenTrait;
13
use Yiisoft\Validator\SerializableRuleInterface;
14
use Yiisoft\Validator\SkipOnEmptyInterface;
15
use Yiisoft\Validator\SkipOnErrorInterface;
16
use Yiisoft\Validator\ValidationContext;
17
use Yiisoft\Validator\WhenInterface;
18
19
use function function_exists;
20
21
/**
22
 * Validates that the value is a valid email address.
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
final class Email implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
26
{
27
    use SkipOnEmptyTrait;
28
    use SkipOnErrorTrait;
29
    use WhenTrait;
30
31 2
    public function __construct(
32
        /**
33
         * @var string the regular expression used to validate value.
34
         *
35
         * @link http://www.regular-expressions.info/email.html
36
         */
37
        private 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])?$/',
38
        /**
39
         * @var string the regular expression used to validate email addresses with the name part. This property is used
40
         * only when {@see $allowName} is `true`.
41
         *
42
         * @see $allowName
43
         */
44
        private 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])?>$/',
45
        /**
46
         * @var string the regular expression used to validate complex emails when {@see $enableIDN} is `true`.
47
         */
48
        private string $idnEmailPattern = '/^([a-zA-Z0-9._%+-]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/',
49
        /**
50
         * @var bool whether to allow name in the email address (e.g. "John Smith <[email protected]>"). Defaults
51
         * to `false`.
52
         *
53
         * @see $fullPattern
54
         */
55
        private bool $allowName = false,
56
        /**
57
         * @var bool whether to check whether the email's domain exists and has either an A or MX record.
58
         * Be aware that this check can fail due to temporary DNS problems even if the email address is
59
         * valid and an email would be deliverable. Defaults to `false`.
60
         */
61
        private bool $checkDNS = false,
62
        /**
63
         * @var bool whether validation process should take into account IDN (internationalized domain
64
         * names). Defaults to false meaning that validation of emails containing IDN will always fail.
65
         * Note that in order to use IDN validation you have to install and enable `intl` PHP extension,
66
         * otherwise an exception would be thrown.
67
         */
68
        private bool $enableIDN = false,
69
        private string $message = 'This value is not a valid email address.',
70
71
        /**
72
         * @var bool|callable|null
73
         */
74
        private $skipOnEmpty = null,
75
        private bool $skipOnError = false,
76
        /**
77
         * @var Closure(mixed, ValidationContext):bool|null
78
         */
79
        private ?Closure $when = null,
80
    ) {
81 2
        if ($enableIDN && !function_exists('idn_to_ascii')) {
82
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
83
        }
84
    }
85
86 1
    public function getName(): string
87
    {
88 1
        return 'email';
89
    }
90
91 73
    public function getPattern(): string
92
    {
93 73
        return $this->pattern;
94
    }
95
96 20
    public function getFullPattern(): string
97
    {
98 20
        return $this->fullPattern;
99
    }
100
101 10
    public function getIdnEmailPattern(): string
102
    {
103 10
        return $this->idnEmailPattern;
104
    }
105
106 47
    public function isAllowName(): bool
107
    {
108 47
        return $this->allowName;
109
    }
110
111 39
    public function isCheckDNS(): bool
112
    {
113 39
        return $this->checkDNS;
114
    }
115
116 83
    public function isEnableIDN(): bool
117
    {
118 83
        return $this->enableIDN;
119
    }
120
121 45
    public function getMessage(): string
122
    {
123 45
        return $this->message;
124
    }
125
126 4
    public function getOptions(): array
127
    {
128
        return [
129 4
            'pattern' => $this->pattern,
130 4
            'fullPattern' => $this->fullPattern,
131 4
            'idnEmailPattern' => $this->idnEmailPattern,
132 4
            'allowName' => $this->allowName,
133 4
            'checkDNS' => $this->checkDNS,
134 4
            'enableIDN' => $this->enableIDN,
135
            'message' => [
136 4
                'message' => $this->message,
137
            ],
138 4
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
139 4
            'skipOnError' => $this->skipOnError,
140
        ];
141
    }
142
143 1
    public function getHandlerClassName(): string
144
    {
145 1
        return EmailHandler::class;
146
    }
147
}
148