Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 6811ce0

Browse files
authored
Merge pull request #46 from programmatordev/YAPV-19-create-email-rule
Create Email rule
2 parents 9209213 + 71ca5c4 commit 6811ce0

21 files changed

+281
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/vendor/
66
/logs/
77
/.idea
8+
/.ddev
89
/index.php

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
"require": {
1515
"php": ">=8.1",
16+
"egulias/email-validator": "^4.0",
1617
"symfony/intl": "^6.3",
1718
"symfony/polyfill-ctype": "^1.27"
1819
},

docs/03-rules.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Rules
22

33
- [Basic Rules](#basic-rules)
4+
- [String Rules](#string-rules)
45
- [Comparison Rules](#comparison-rules)
56
- [Date Rules](#date-rules)
67
- [Choice Rules](#choice-rules)
@@ -11,6 +12,10 @@
1112
- [NotBlank](03x-rules-not-blank.md)
1213
- [Type](03x-rules-type.md)
1314

15+
## String Rules
16+
17+
- [Email](03x-rules-email.md)
18+
1419
## Comparison Rules
1520

1621
- [GreaterThan](03x-rules-greater-than.md)

docs/03x-rules-choice.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ For example, if `maxConstraint` is 2, the input array must have at most 2 values
7878

7979
### `message`
8080

81-
type `string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
81+
type: `string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
8282

8383
Message that will be shown if input value is not a valid choice.
8484

@@ -92,7 +92,7 @@ The following parameters are available:
9292

9393
### `multipleMessage`
9494

95-
type `string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
95+
type: `string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
9696

9797
Message that will be shown when `multiple` is `true` and at least one of the input array values is not a valid choice.
9898

@@ -106,7 +106,7 @@ The following parameters are available:
106106

107107
### `minMessage`
108108

109-
type `string` default: `The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
109+
type: `string` default: `The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
110110

111111
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `minConstraint`.
112112

@@ -123,7 +123,7 @@ The following parameters are available:
123123

124124
### `maxMessage`
125125

126-
type `string` default: `The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
126+
type: `string` default: `The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
127127

128128
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `maxConstraint`.
129129

docs/03x-rules-email.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Email
2+
3+
Validates that a value is a valid email address.
4+
5+
```php
6+
Email(
7+
string $mode = 'html5',
8+
?callable $normalizer = null,
9+
string $message = 'The {{ name }} value is not a valid email address, {{ value }} given.'
10+
);
11+
```
12+
13+
## Basic Usage
14+
15+
```php
16+
// html5 mode (default)
17+
Validator::email()->validate('test@example.com'); // true
18+
Validator::email()->validate('test@example'); // false
19+
20+
// html5-allow-no-tld mode
21+
Validator::email(mode: 'html5-allow-no-tld')->validate('test@example.com'); // true
22+
Validator::email(mode: 'html5-allow-no-tld')->validate('test@example'); // true
23+
```
24+
25+
> **Note**
26+
> An `UnexpectedValueException` will be thrown when a `mode` option is invalid.
27+
28+
## Options
29+
30+
### `mode`
31+
32+
type: `string` default: `html5`
33+
34+
Set this option to define the validation mode.
35+
36+
Available options are:
37+
38+
- `html5` uses the regular expression of an HTML5 email input element, but enforces it to have a TLD extension.
39+
- `html5-allow-no-tld` uses the regular expression of an HTML5 email input element, which allows addresses without a TLD extension.
40+
- `strict` validates an address according to the [RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322) specification.
41+
42+
### `normalizer`
43+
44+
type: `callable` default: `null`
45+
46+
Allows to define a `callable` that will be applied to the value before checking if it is valid.
47+
48+
For example, use `trim`, or pass your own function, to ignore whitespace in the beginning or end of an email address:
49+
50+
```php
51+
Validator::email()->validate('test@example.com '); // false
52+
53+
Validator::email(normalizer: 'trim')->validate('test@example.com '); // true
54+
Validator::email(normalizer: fn($value) => trim($value))->validate('test@example.com '); // true
55+
```
56+
57+
### `message`
58+
59+
type `string` default: `The {{ name }} value is not a valid email address, {{ value }} given.`
60+
61+
Message that will be shown if the input value is not a valid email address.
62+
63+
The following parameters are available:
64+
65+
| Parameter | Description |
66+
|---------------|---------------------------|
67+
| `{{ value }}` | The current invalid value |
68+
| `{{ name }}` | Name of the invalid value |
69+
| `{{ mode }}` | Selected validation mode |
70+
71+
## Changelog
72+
73+
- `0.6.0` Created

docs/03x-rules-timezone.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Check the [official country codes](https://en.wikipedia.org/wiki/ISO_3166-1#Curr
7575

7676
### `message`
7777

78-
type `string` default: `The {{ name }} value is not a valid timezone, {{ value }} given.`
78+
type: `string` default: `The {{ name }} value is not a valid timezone, {{ value }} given.`
7979

8080
Message that will be shown if the input value is not a valid timezone.
8181

docs/03x-rules-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Available character type constraints:
7777

7878
### `message`
7979

80-
type `string` default: `The {{ name }} value should be of type {{ constraint }}, {{ value }} given.`
80+
type: `string` default: `The {{ name }} value should be of type {{ constraint }}, {{ value }} given.`
8181

8282
Message that will be shown if input value is not of a specific type.
8383

src/ChainedValidatorInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public function eachValue(
3333
string $message = 'At key {{ key }}: {{ message }}'
3434
): ChainedValidatorInterface&Validator;
3535

36+
static function email(
37+
string $mode = 'html5',
38+
?callable $normalizer = null,
39+
string $message = 'The {{ name }} value is not a valid email address, {{ value }} given.'
40+
): ChainedValidatorInterface&Validator;
41+
3642
public function greaterThan(
3743
mixed $constraint,
3844
string $message = 'The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.'

src/Exception/EmailException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class EmailException extends ValidationException {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class UnexpectedOptionException extends UnexpectedValueException
6+
{
7+
public function __construct(string $name, array $expected, string $given)
8+
{
9+
$message = \sprintf('Invalid %s "%s". Accepted values are: "%s".', $name, $given, \implode('", "', $expected));
10+
11+
parent::__construct($message);
12+
}
13+
}

0 commit comments

Comments
 (0)