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

Commit 45abd30

Browse files
committed
docs: removed dependency injection examples
1 parent d3dec57 commit 45abd30

File tree

3 files changed

+32
-140
lines changed

3 files changed

+32
-140
lines changed

README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,18 @@ Simple usage looks like:
3939
use ProgrammatorDev\Validator\Rule;
4040
use ProgrammatorDev\Validator\Validator;
4141

42-
// Do this...
42+
// do this...
4343
$validator = Validator::notBlank()->greaterThanOrEqual(18);
4444

45-
// Or this...
46-
$validator = new Validator(
47-
new Rule\NotBlank(),
48-
new Rule\GreaterThanOrEqual(18)
49-
);
50-
51-
// Validate with these:
45+
// ...and validate with these:
5246
$validator->validate(16); // returns bool: false
5347
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
5448
```
5549

5650
## Documentation
5751

5852
- [Get Started](docs/01-get-started.md)
59-
- [Usage](docs/02-usage.md)
53+
- [How to Use](docs/02-usage.md)
6054
- [Usage](docs/02-usage.md#usage)
6155
- [Methods](docs/02-usage.md#methods)
6256
- [Error Handling](docs/02-usage.md#error-handling)

docs/02-usage.md

Lines changed: 14 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
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
2515
use 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:
7747
use ProgrammatorDev\Validator\Exception\ValidationException;
7848
use 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

8959
try {
90-
getWeatherTemperature(latitude: 100, longitude: 50, unitSystem: 'METRIC');
60+
getWeather(latitude: 100, longitude: 50, unitSystem: 'metric');
9161
}
9262
catch (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

10571
This method always returns a `bool` when a rule fails, useful for conditions.
@@ -114,77 +80,10 @@ An example:
11480
use ProgrammatorDev\Validator\Validator;
11581

11682
if (!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

19089
When using the [`assert`](#assert) method, an exception is thrown when a rule fails.
@@ -199,16 +98,16 @@ use ProgrammatorDev\Validator\Validator;
19998
try {
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
}
204103
catch (Exception\RangeException $exception) {
205-
// Do something when Range fails
104+
// do something when Range fails
206105
}
207106
catch (Exception\NotBlankException $exception) {
208-
// Do something when NotBlank fails
107+
// do something when NotBlank fails
209108
}
210109
catch (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;
221120
try {
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
}
226125
catch (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
```

docs/04-custom-rules.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use ProgrammatorDev\Validator\Rule\RuleInterface;
2929

3030
class CustomRule extends AbstractRule implements RuleInterface
3131
{
32-
public function assert(mixed $value, string $name): void
32+
public function assert(mixed $value, ?string $name = null): void
3333
{
34-
// Do validation
34+
// do validation
3535
}
3636
}
3737
```
@@ -47,7 +47,7 @@ use My\Project\Exception\CustomRuleException;
4747

4848
class CustomRule extends AbstractRule implements RuleInterface
4949
{
50-
public function assert(mixed $value, string $name): void
50+
public function assert(mixed $value, ?string $name = null): void
5151
{
5252
if ($value === 0) {
5353
throw new CustomRuleException(
@@ -66,18 +66,13 @@ In the example above, a new custom rule was created that validates if the input
6666
To use your new custom rule, simply do the following:
6767

6868
```php
69-
// Fluent way, notice the rule() method
69+
// notice the rule() method
7070
$validator = Validator::rule(new CustomRule());
71-
// With multiple rules
71+
// with multiple rules
7272
$validator = Validator::range(-10, 10)->rule(new CustomRule());
7373

74-
// Dependency injection way
75-
$validator = new Validator(new CustomRule());
76-
// With multiple rules
77-
$validator = new Validator(new Range(-10, 10), new CustomRule());
78-
79-
$validator->assert(0, 'test'); // throws: The test value cannot be zero!
8074
$validator->validate(0); // false
75+
$validator->assert(0, 'test'); // throws: The test value cannot be zero!
8176
```
8277

8378
## Message Template
@@ -88,15 +83,17 @@ This means that you can have dynamic content in your messages.
8883
To make it work, just pass an associative array with the name and value of your parameters, and they will be available in the message:
8984

9085
```php
91-
// Exception
86+
// exception
9287
class FavoriteException extends ValidationException {}
88+
```
9389

94-
// Rule
90+
```php
91+
// rule
9592
class Favorite extends AbstractRule implements RuleInterface
9693
{
9794
public function __construct(
98-
private readonly string $favorite
99-
)
95+
private readonly string $favorite
96+
) {}
10097

10198
public function assert(mixed $value, ?string $name = null): void
10299
{
@@ -112,7 +109,9 @@ class Favorite extends AbstractRule implements RuleInterface
112109
}
113110
}
114111
}
112+
```
115113

116-
// Throws: My favorite animal is "cat", not "human"!
114+
```php
115+
// throws: My favorite animal is "cat", not "human"!
117116
Validator::rule(new Favorite('cat'))->assert('human', 'animal');
118117
```

0 commit comments

Comments
 (0)