1
1
# Using Yet Another PHP Validator
2
2
3
3
- [ Usage] ( #usage )
4
- - [ Fluent] ( #fluent )
5
- - [ Dependency Injection] ( #dependency-injection )
6
4
- [ Methods] ( #methods )
7
5
- [ assert] ( #assert )
8
6
- [ validate] ( #validate )
9
- - [ getRules] ( #getrules )
10
- - [ addRule] ( #addrule )
11
7
- [ Error Handling] ( #error-handling )
12
8
- [ Custom Error Messages] ( #custom-error-messages )
13
9
14
10
## Usage
15
11
16
- This library allows you to validate data in two different ways:
17
- - In a fluent way, making use of magic methods. The goal is to be able to create a set of rules with minimum setup;
18
- - In a traditional way, making use of dependency injection. You may not like the fluent approach, and prefer to work this way.
19
-
20
- Both should work exactly the same.
21
-
22
- ### Fluent
12
+ This library allows you to validate data with a set of rules with minimum setup:
23
13
24
14
``` php
25
15
use ProgrammatorDev\Validator\Exception\ValidationException;
@@ -28,36 +18,16 @@ use ProgrammatorDev\Validator\Validator;
28
18
/**
29
19
* @throws ValidationException
30
20
*/
31
- function getWeatherTemperature (float $latitude, float $longitude, string $unitSystem): float
21
+ public function getWeather (float $latitude, float $longitude, string $unitSystem): float
32
22
{
33
23
Validator::range(-90, 90)->assert($latitude, 'latitude');
34
24
Validator::range(-180, 180)->assert($longitude, 'longitude');
35
- Validator::notBlank()->choice(['METRIC ', 'IMPERIAL '])->assert($unitSystem, 'unit system');
25
+ Validator::notBlank()->choice(['metric ', 'imperial '])->assert($unitSystem, 'unit system');
36
26
37
27
// ...
38
28
}
39
29
```
40
30
41
- ### Dependency Injection
42
-
43
- ``` php
44
- use ProgrammatorDev\Validator\Exception\ValidationException;
45
- use ProgrammatorDev\Validator\Rule;
46
- use ProgrammatorDev\Validator\Validator;
47
-
48
- /**
49
- * @throws ValidationException
50
- */
51
- function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
52
- {
53
- (new Validator(new Rule\Range(-90, 90)))->assert($latitude, 'latitude');
54
- (new Validator(new Rule\Range(-180, 180)))->assert($longitude, 'longitude');
55
- (new Validator(new Rule\NotBlank(), new Rule\Choice(['METRIC', 'IMPERIAL'])))->assert($unitSystem, 'unit system');
56
-
57
- // ...
58
- }
59
- ```
60
-
61
31
## Methods
62
32
63
33
### ` assert `
@@ -77,17 +47,17 @@ An example on how to handle an error:
77
47
use ProgrammatorDev\Validator\Exception\ValidationException;
78
48
use ProgrammatorDev\Validator\Validator;
79
49
80
- function getWeatherTemperature (float $latitude, float $longitude, string $unitSystem): float
50
+ function getWeather (float $latitude, float $longitude, string $unitSystem): float
81
51
{
82
52
Validator::range(-90, 90)->assert($latitude, 'latitude');
83
53
Validator::range(-180, 180)->assert($longitude, 'longitude');
84
- Validator::notBlank()->choice(['METRIC ', 'IMPERIAL '])->assert($unitSystem, 'unit system');
54
+ Validator::notBlank()->choice(['metric ', 'imperial '])->assert($unitSystem, 'unit system');
85
55
86
56
// ...
87
57
}
88
58
89
59
try {
90
- getWeatherTemperature (latitude: 100, longitude: 50, unitSystem: 'METRIC ');
60
+ getWeather (latitude: 100, longitude: 50, unitSystem: 'metric ');
91
61
}
92
62
catch (ValidationException $exception) {
93
63
echo $exception->getMessage(); // The latitude value should be between -90 and 90, 100 given.
@@ -96,10 +66,6 @@ catch (ValidationException $exception) {
96
66
> [ !NOTE]
97
67
> Check the [ Error Handling] ( #error-handling ) section for more information.
98
68
99
- > [ !NOTE]
100
- > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
101
- > Check the [ Usage] ( #usage ) section for more information.
102
-
103
69
### ` validate `
104
70
105
71
This method always returns a ` bool ` when a rule fails, useful for conditions.
@@ -114,77 +80,10 @@ An example:
114
80
use ProgrammatorDev\Validator\Validator;
115
81
116
82
if (!Validator::range(-90, 90)->validate($latitude)) {
117
- // Do something...
83
+ // do something...
118
84
}
119
85
```
120
86
121
- > [ !NOTE]
122
- > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
123
- > Check the [ Usage] ( #usage ) section for more information.
124
-
125
- ### ` getRules `
126
-
127
- Returns an array with the defined set of rules.
128
-
129
- ``` php
130
- /**
131
- * @return RuleInterface[]
132
- */
133
- getRules(): array
134
- ```
135
-
136
- An example:
137
-
138
- ``` php
139
- use ProgrammatorDev\Validator\Rule;
140
- use ProgrammatorDev\Validator\Validator;
141
-
142
- $validator = new Validator(new Rule\GreaterThanOrEqual(0), new Rule\LessThanOrEqual(100));
143
-
144
- print_r($validator->getRules());
145
-
146
- // Array (
147
- // [0] => ProgrammatorDev\Validator\Rule\GreaterThanOrEqual Object
148
- // [1] => ProgrammatorDev\Validator\Rule\LessThanOrEqual Object
149
- // )
150
- ```
151
-
152
- > [ !NOTE]
153
- > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
154
- > Check the [ Usage] ( #usage ) section for more information.
155
-
156
- ### ` addRule `
157
-
158
- Adds a rule to a set of rules. May be useful for conditional validations.
159
-
160
- ``` php
161
- addRule(RuleInterface $rule): self
162
- ```
163
-
164
- An example:
165
-
166
- ``` php
167
- use ProgrammatorDev\Validator\Rule;
168
- use ProgrammatorDev\Validator\Validator;
169
-
170
- function calculateDiscount(float $price, float $discount, string $type): float
171
- {
172
- $discountValidator = new Validator(new GreaterThan(0));
173
-
174
- if ($type === 'PERCENT') {
175
- $discountValidator->addRule(new Rule\LessThanOrEqual(100));
176
- }
177
-
178
- $discountValidator->assert($discount, 'discount');
179
-
180
- // ...
181
- }
182
- ```
183
-
184
- > [ !NOTE]
185
- > The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
186
- > Check the [ Usage] ( #usage ) section for more information.
187
-
188
87
## Error Handling
189
88
190
89
When using the [ ` assert ` ] ( #assert ) method, an exception is thrown when a rule fails.
@@ -199,16 +98,16 @@ use ProgrammatorDev\Validator\Validator;
199
98
try {
200
99
Validator::range(-90, 90)->assert($latitude, 'latitude');
201
100
Validator::range(-180, 180)->assert($longitude, 'longitude');
202
- Validator::notBlank()->choice(['METRIC ', 'IMPERIAL '])->assert($unitSystem, 'unit system');
101
+ Validator::notBlank()->choice(['metric ', 'imperial '])->assert($unitSystem, 'unit system');
203
102
}
204
103
catch (Exception\RangeException $exception) {
205
- // Do something when Range fails
104
+ // do something when Range fails
206
105
}
207
106
catch (Exception\NotBlankException $exception) {
208
- // Do something when NotBlank fails
107
+ // do something when NotBlank fails
209
108
}
210
109
catch (Exception\ChoiceException $exception) {
211
- // Do something when Choice fails
110
+ // do something when Choice fails
212
111
}
213
112
```
214
113
@@ -221,10 +120,10 @@ use ProgrammatorDev\Validator\Validator;
221
120
try {
222
121
Validator::range(-90, 90)->assert($latitude, 'latitude');
223
122
Validator::range(-180, 180)->assert($longitude, 'longitude');
224
- Validator::notBlank()->choice(['METRIC ', 'IMPERIAL '])->assert($unitSystem, 'unit system');
123
+ Validator::notBlank()->choice(['metric ', 'imperial '])->assert($unitSystem, 'unit system');
225
124
}
226
125
catch (ValidationException $exception) {
227
- // Do something when a rule fails
126
+ // do something when a rule fails
228
127
echo $exception->getMessage();
229
128
}
230
129
```
@@ -264,5 +163,5 @@ Validator::choice(
264
163
message: '{{ value }} is not a valid {{ name }}! You must select one of {{ constraints }}.'
265
164
)->assert('yellow', 'color');
266
165
267
- // Throws : "yellow" is not a valid color! You must select one of ["red", "green", "blue"].
166
+ // throws : "yellow" is not a valid color! You must select one of ["red", "green", "blue"].
268
167
```
0 commit comments