Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
EmailAddress | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
validate | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
getInverseError | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Verja\Validator; |
4 | |
5 | use Verja\Error; |
6 | use Verja\Validator; |
7 | |
8 | class EmailAddress extends Validator |
9 | { |
10 | const LOCAL_PART_PATTERN = '[A-Za-z0-9.!#$%&\'*+\/=?^_`{|}~.-]+'; |
11 | const DOMAIN_PART_PATTERN = '[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*(?:\.[a-zA-Z0-9]{2,}(?:-[a-zA-Z0-9]+)*)+'; |
12 | |
13 | /** {@inheritdoc} */ |
14 | public function validate($value, array $context = []): bool |
15 | { |
16 | if (!preg_match('/^' . self::LOCAL_PART_PATTERN . '@' . self::DOMAIN_PART_PATTERN . '$/', $value)) { |
17 | $this->error = new Error( |
18 | 'NO_EMAIL_ADDRESS', |
19 | $value, |
20 | 'value should be a valid email address', |
21 | null |
22 | ); |
23 | return false; |
24 | } |
25 | |
26 | return true; |
27 | } |
28 | |
29 | /** {@inheritdoc} */ |
30 | public function getInverseError($value) |
31 | { |
32 | return new Error( |
33 | 'IS_EMAIL_ADDRESS', |
34 | $value, |
35 | 'value should not be an email address', |
36 | null |
37 | ); |
38 | } |
39 | } |