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

Commit ddabc65

Browse files
authored
Merge pull request #48 from programmatordev/1.x
1.x
2 parents 604b954 + 403c5a9 commit ddabc65

34 files changed

+576
-54
lines changed

.gitignore

+1
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

+1
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

+6
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,11 @@
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+
- [URL](03x-rules-url.md)
19+
1420
## Comparison Rules
1521

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

docs/03x-rules-choice.md

+9-5
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

@@ -136,4 +136,8 @@ The following parameters are available:
136136
| `{{ name }}` | Name of the invalid value |
137137
| `{{ constraints }}` | The array of valid choices |
138138
| `{{ minConstraint }}` | The minimum number of valid choices |
139-
| `{{ maxConstraint }}` | The maximum number of valid choices |
139+
| `{{ maxConstraint }}` | The maximum number of valid choices |
140+
141+
## Changelog
142+
143+
- `0.1.0` Created

docs/03x-rules-country.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ The following parameters are available:
5252
|---------------|---------------------------|
5353
| `{{ value }}` | The current invalid value |
5454
| `{{ name }}` | Name of the invalid value |
55-
| `{{ code }}` | Selected code type |
55+
| `{{ code }}` | Selected code type |
56+
57+
## Changelog
58+
59+
- `0.2.0` Created

docs/03x-rules-each-key.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ The following parameters are available:
4646
| `{{ name }}` | Name of the invalid value |
4747
| `{{ key }}` | The key of the invalid iterable element |
4848
| `{{ element }}` | The value of the invalid iterable element |
49-
| `{{ message }}` | The rule message of the invalid iterable element |
49+
| `{{ message }}` | The rule message of the invalid iterable element |
50+
51+
## Changelog
52+
53+
- `0.4.0` Created

docs/03x-rules-each-value.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ The following parameters are available:
4646
| `{{ name }}` | Name of the invalid value |
4747
| `{{ key }}` | The key of the invalid iterable element |
4848
| `{{ element }}` | The value of the invalid iterable element |
49-
| `{{ message }}` | The rule message of the invalid iterable element |
49+
| `{{ message }}` | The rule message of the invalid iterable element |
50+
51+
## Changelog
52+
53+
- `0.4.0` Created

docs/03x-rules-email.md

+73
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-greater-than-or-equal.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ The following parameters are available:
5454
|--------------------|---------------------------|
5555
| `{{ value }}` | The current invalid value |
5656
| `{{ name }}` | Name of the invalid value |
57-
| `{{ constraint }}` | The comparison value |
57+
| `{{ constraint }}` | The comparison value |
58+
59+
## Changelog
60+
61+
- `0.1.0` Created

docs/03x-rules-greater-than.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ The following parameters are available:
5454
|--------------------|---------------------------|
5555
| `{{ value }}` | The current invalid value |
5656
| `{{ name }}` | Name of the invalid value |
57-
| `{{ constraint }}` | The comparison value |
57+
| `{{ constraint }}` | The comparison value |
58+
59+
## Changelog
60+
61+
- `0.1.0` Created

docs/03x-rules-less-than-or-equal.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ The following parameters are available:
5454
|--------------------|---------------------------|
5555
| `{{ value }}` | The current invalid value |
5656
| `{{ name }}` | Name of the invalid value |
57-
| `{{ constraint }}` | The comparison value |
57+
| `{{ constraint }}` | The comparison value |
58+
59+
## Changelog
60+
61+
- `0.1.0` Created

docs/03x-rules-less-than.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ The following parameters are available:
5454
|--------------------|---------------------------|
5555
| `{{ value }}` | The current invalid value |
5656
| `{{ name }}` | Name of the invalid value |
57-
| `{{ constraint }}` | The comparison value |
57+
| `{{ constraint }}` | The comparison value |
58+
59+
## Changelog
60+
61+
- `0.1.0` Created

docs/03x-rules-not-blank.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ The following parameters are available:
5050
| Parameter | Description |
5151
|---------------|---------------------------|
5252
| `{{ value }}` | The current invalid value |
53-
| `{{ name }}` | Name of the invalid value |
53+
| `{{ name }}` | Name of the invalid value |
54+
55+
## Changelog
56+
57+
- `0.1.0` Created

docs/03x-rules-range.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,8 @@ The following parameters are available:
6868
| `{{ value }}` | The current invalid value |
6969
| `{{ name }}` | Name of the invalid value |
7070
| `{{ minConstraint }}` | The minimum range value |
71-
| `{{ maxConstraint }}` | The maximum range value |
71+
| `{{ maxConstraint }}` | The maximum range value |
72+
73+
## Changelog
74+
75+
- `0.1.0` Created

docs/03x-rules-timezone.md

+5-1
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

@@ -86,3 +86,7 @@ The following parameters are available:
8686
| `{{ value }}` | The current invalid value |
8787
| `{{ name }}` | Name of the invalid value |
8888
| `{{ countryCode }}` | Selected country code |
89+
90+
## Changelog
91+
92+
- `0.3.0` Created

docs/03x-rules-type.md

+6-2
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

@@ -87,4 +87,8 @@ The following parameters are available:
8787
|--------------------|---------------------------|
8888
| `{{ value }}` | The current invalid value |
8989
| `{{ name }}` | Name of the invalid value |
90-
| `{{ constraint }}` | The valid type(s) |
90+
| `{{ constraint }}` | The valid type(s) |
91+
92+
## Changelog
93+
94+
- `0.2.0` Created

docs/03x-rules-url.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# URL
2+
3+
Validates that a value is a valid URL address.
4+
5+
```php
6+
Url(
7+
array $protocols = ['http', 'https'],
8+
bool $allowRelativeProtocol = false,
9+
?callable $normalizer = null,
10+
string $message = 'The {{ name }} value is not a valid URL address, {{ value }} given.'
11+
);
12+
```
13+
14+
## Basic Usage
15+
16+
```php
17+
Validator::url()->validate('https://example.com'); // true
18+
19+
// Only allow the https protocol
20+
Validator::url(protocols: ['https'])->validate('http://example.com'); // false
21+
// Or allow the ftp protocol too
22+
Validator::url(protocols: ['https', 'ftp'])->validate('ftp://example.com'); // true
23+
24+
// Allow relative protocol
25+
Validator::url()->validate('//example.com'); // false
26+
Validator::url(allowRelativeProtocol: true)->validate('//example.com'); // true
27+
Validator::url(allowRelativeProtocol: true)->validate('https://example.com'); // true
28+
```
29+
30+
## Options
31+
32+
### `protocols`
33+
34+
type: `array` default: `['http', 'https']`
35+
36+
Set this option to define the allowed protocols.
37+
38+
### `allowRelativeProtocol`
39+
40+
type: `bool` default: `false`
41+
42+
If this option is `true`, inclusion of a protocol in the URL will be optional.
43+
44+
### `normalizer`
45+
46+
type: `callable` default: `null`
47+
48+
Allows to define a `callable` that will be applied to the value before checking if it is valid.
49+
50+
For example, use `trim`, or pass your own function, to ignore whitespace in the beginning or end of a URL address:
51+
52+
```php
53+
Validator::url()->validate('https://example.com '); // false
54+
55+
Validator::url(normalizer: 'trim')->validate('https://example.com '); // true
56+
Validator::url(normalizer: fn($value) => trim($value))->validate('https://example.com '); // true
57+
```
58+
59+
### `message`
60+
61+
type `string` default: `The {{ name }} value is not a valid URL address, {{ value }} given.`
62+
63+
Message that will be shown if the input value is not a valid URL address.
64+
65+
The following parameters are available:
66+
67+
| Parameter | Description |
68+
|-------------------|---------------------------|
69+
| `{{ value }}` | The current invalid value |
70+
| `{{ name }}` | Name of the invalid value |
71+
| `{{ protocols }}` | Allowed protocols |
72+
73+
## Changelog
74+
75+
- `0.6.0` Created

0 commit comments

Comments
 (0)