Passed
Push — master ( cd2db1...fdabae )
by Alexander
02:29
created

Email::getFullPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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