11# Using Yet Another PHP Validator
22
33- [ Usage] ( #usage )
4- - [ Fluent] ( #fluent )
5- - [ Dependency Injection] ( #dependency-injection )
64- [ Methods] ( #methods )
75 - [ assert] ( #assert )
86 - [ validate] ( #validate )
9- - [ getRules] ( #getrules )
10- - [ addRule] ( #addrule )
117- [ Error Handling] ( #error-handling )
128- [ Custom Error Messages] ( #custom-error-messages )
139
1410## Usage
1511
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:
2313
2414``` php
2515use ProgrammatorDev\Validator\Exception\ValidationException;
@@ -28,36 +18,16 @@ use ProgrammatorDev\Validator\Validator;
2818/**
2919 * @throws ValidationException
3020 */
31- function getWeatherTemperature (float $latitude, float $longitude, string $unitSystem): float
21+ public function getWeather (float $latitude, float $longitude, string $unitSystem): float
3222{
3323 Validator::range(-90, 90)->assert($latitude, 'latitude');
3424 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');
3626
3727 // ...
3828}
3929```
4030
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-
6131## Methods
6232
6333### ` assert `
@@ -77,17 +47,17 @@ An example on how to handle an error:
7747use ProgrammatorDev\Validator\Exception\ValidationException;
7848use ProgrammatorDev\Validator\Validator;
7949
80- function getWeatherTemperature (float $latitude, float $longitude, string $unitSystem): float
50+ function getWeather (float $latitude, float $longitude, string $unitSystem): float
8151{
8252 Validator::range(-90, 90)->assert($latitude, 'latitude');
8353 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');
8555
8656 // ...
8757}
8858
8959try {
90- getWeatherTemperature (latitude: 100, longitude: 50, unitSystem: 'METRIC ');
60+ getWeather (latitude: 100, longitude: 50, unitSystem: 'metric ');
9161}
9262catch (ValidationException $exception) {
9363 echo $exception->getMessage(); // The latitude value should be between -90 and 90, 100 given.
@@ -96,10 +66,6 @@ catch (ValidationException $exception) {
9666> [ !NOTE]
9767> Check the [ Error Handling] ( #error-handling ) section for more information.
9868
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-
10369### ` validate `
10470
10571This method always returns a ` bool ` when a rule fails, useful for conditions.
@@ -114,77 +80,10 @@ An example:
11480use ProgrammatorDev\Validator\Validator;
11581
11682if (!Validator::range(-90, 90)->validate($latitude)) {
117- // Do something...
83+ // do something...
11884}
11985```
12086
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-
18887## Error Handling
18988
19089When using the [ ` assert ` ] ( #assert ) method, an exception is thrown when a rule fails.
@@ -199,16 +98,16 @@ use ProgrammatorDev\Validator\Validator;
19998try {
20099 Validator::range(-90, 90)->assert($latitude, 'latitude');
201100 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');
203102}
204103catch (Exception\RangeException $exception) {
205- // Do something when Range fails
104+ // do something when Range fails
206105}
207106catch (Exception\NotBlankException $exception) {
208- // Do something when NotBlank fails
107+ // do something when NotBlank fails
209108}
210109catch (Exception\ChoiceException $exception) {
211- // Do something when Choice fails
110+ // do something when Choice fails
212111}
213112```
214113
@@ -221,10 +120,10 @@ use ProgrammatorDev\Validator\Validator;
221120try {
222121 Validator::range(-90, 90)->assert($latitude, 'latitude');
223122 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');
225124}
226125catch (ValidationException $exception) {
227- // Do something when a rule fails
126+ // do something when a rule fails
228127 echo $exception->getMessage();
229128}
230129```
@@ -264,5 +163,5 @@ Validator::choice(
264163 message: '{{ value }} is not a valid {{ name }}! You must select one of {{ constraints }}.'
265164)->assert('yellow', 'color');
266165
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"].
268167```
0 commit comments