Passed
Pull Request — master (#300)
by Alexander
06:13 queued 03:06
created

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