Passed
Pull Request — master (#240)
by Dmitriy
03:45
created

Email::getMessage()   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 JetBrains\PhpStorm\ArrayShape;
10
use RuntimeException;
11
use Yiisoft\Validator\ParametrizedRuleInterface;
12
use Yiisoft\Validator\BeforeValidationInterface;
13
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
17
use Yiisoft\Validator\ValidationContext;
18
19
use function function_exists;
20
21
/**
22
 * Validates that the value is a valid email address.
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY)]
25
final class Email implements ParametrizedRuleInterface, BeforeValidationInterface
26
{
27
    use BeforeValidationTrait;
28
    use HandlerClassNameTrait;
29
    use RuleNameTrait;
30
31 1
    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
        private bool $skipOnEmpty = false,
71
        private bool $skipOnError = false,
72
        /**
73
         * @var Closure(mixed, ValidationContext):bool|null
74
         */
75
        private ?Closure $when = null,
76
    ) {
77 1
        if ($enableIDN && !function_exists('idn_to_ascii')) {
78
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
79
        }
80
    }
81
82
    /**
83
     * @return string
84
     */
85 73
    public function getPattern(): string
86
    {
87 73
        return $this->pattern;
88
    }
89
90
    /**
91
     * @return string
92
     */
93 20
    public function getFullPattern(): string
94
    {
95 20
        return $this->fullPattern;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 10
    public function getIdnEmailPattern(): string
102
    {
103 10
        return $this->idnEmailPattern;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109 47
    public function isAllowName(): bool
110
    {
111 47
        return $this->allowName;
112
    }
113
114
    /**
115
     * @return bool
116
     */
117 39
    public function isCheckDNS(): bool
118
    {
119 39
        return $this->checkDNS;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125 83
    public function isEnableIDN(): bool
126
    {
127 83
        return $this->enableIDN;
128
    }
129
130
    /**
131
     * @return string
132
     */
133 45
    public function getMessage(): string
134
    {
135 45
        return $this->message;
136
    }
137
138 4
    #[ArrayShape([
139
        'pattern' => 'string',
140
        'fullPattern' => 'string',
141
        'idnEmailPattern' => 'string',
142
        'allowName' => 'bool',
143
        'checkDNS' => 'bool',
144
        'enableIDN' => 'bool',
145
        'message' => 'string[]',
146
        'skipOnEmpty' => 'bool',
147
        'skipOnError' => 'bool',
148
    ])]
149
    public function getOptions(): array
150
    {
151
        return [
152 4
            'pattern' => $this->pattern,
153 4
            'fullPattern' => $this->fullPattern,
154 4
            'idnEmailPattern' => $this->idnEmailPattern,
155 4
            'allowName' => $this->allowName,
156 4
            'checkDNS' => $this->checkDNS,
157 4
            'enableIDN' => $this->enableIDN,
158
            'message' => [
159 4
                'message' => $this->message,
160
            ],
161 4
            'skipOnEmpty' => $this->skipOnEmpty,
162 4
            'skipOnError' => $this->skipOnError,
163
        ];
164
    }
165
}
166