Passed
Pull Request — master (#332)
by Dmitriy
03:02
created

Email   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 26
c 3
b 0
f 0
dl 0
loc 117
ccs 29
cts 29
cp 1
rs 10

10 Methods

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