Passed
Pull Request — master (#288)
by Alexander
10:50 queued 07:32
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 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 2
    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
        private $skipOnEmptyCallback = null,
68
        private bool $skipOnError = false,
69
        /**
70
         * @var Closure(mixed, ValidationContext):bool|null
71
         */
72
        private ?Closure $when = null,
73
    ) {
74 2
        $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
75
76 2
        if ($enableIDN && !function_exists('idn_to_ascii')) {
77
            throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
78
        }
79
    }
80
81
    /**
82
     * @return string
83
     */
84 73
    public function getPattern(): string
85
    {
86 73
        return $this->pattern;
87
    }
88
89
    /**
90
     * @return string
91
     */
92 20
    public function getFullPattern(): string
93
    {
94 20
        return $this->fullPattern;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 10
    public function getIdnEmailPattern(): string
101
    {
102 10
        return $this->idnEmailPattern;
103
    }
104
105
    /**
106
     * @return bool
107
     */
108 47
    public function isAllowName(): bool
109
    {
110 47
        return $this->allowName;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116 39
    public function isCheckDNS(): bool
117
    {
118 39
        return $this->checkDNS;
119
    }
120
121
    /**
122
     * @return bool
123
     */
124 83
    public function isEnableIDN(): bool
125
    {
126 83
        return $this->enableIDN;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 45
    public function getMessage(): string
133
    {
134 45
        return $this->message;
135
    }
136
137 4
    public function getOptions(): array
138
    {
139
        return [
140 4
            'pattern' => $this->pattern,
141 4
            'fullPattern' => $this->fullPattern,
142 4
            'idnEmailPattern' => $this->idnEmailPattern,
143 4
            'allowName' => $this->allowName,
144 4
            'checkDNS' => $this->checkDNS,
145 4
            'enableIDN' => $this->enableIDN,
146
            'message' => [
147 4
                'message' => $this->message,
148
            ],
149 4
            'skipOnEmpty' => $this->skipOnEmpty,
150 4
            'skipOnError' => $this->skipOnError,
151
        ];
152
    }
153
154 1
    public function getHandlerClassName(): string
155
    {
156 1
        return EmailHandler::class;
157
    }
158
}
159