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

Commit 9aabedf

Browse files
authored
Merge pull request #18 from programmatordev/YAPV-12-create-documentation
Create documentation
2 parents cce236e + d66e292 commit 9aabedf

File tree

11 files changed

+228
-13
lines changed

11 files changed

+228
-13
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Yet Another PHP Validator
22

3-
Focused on validating development code with expressive error messages.
3+
Versatile library focused on validating development code with expressive error messages.
44

55
## Requirements
66

@@ -25,12 +25,21 @@ require_once 'vendor/autoload.php';
2525
Simple usage looks like:
2626

2727
```php
28+
use ProgrammatorDev\YetAnotherPhpValidator\Rule;
2829
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
2930

30-
$ageValidator = Validator::notBlank()->greaterThanOrEqual(18);
31+
// Do this...
32+
$validator = Validator::notBlank()->greaterThanOrEqual(18);
3133

32-
$ageValidator->validate(16); // returns false
33-
$ageValidator->assert(16, 'Age'); // throws exception: The "Age" value should be greater than or equal to "18", "16" given.
34+
// ...or this...
35+
$validator = new Validator(
36+
new Rule\NotBlank(),
37+
new Rule\GreaterThanOrEqual(18)
38+
);
39+
40+
// ...and validate with these:
41+
$validator->validate(16); // returns bool: false
42+
$validator->assert(16, 'Age'); // throws exception: The "Age" value should be greater than or equal to "18", "16" given.
3443
```
3544

3645
## Documentation

docs/01-get-started.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Get Started
2+
3+
- [Requirements](#requirements)
4+
- [Installation](#installation)
5+
- [Basic Usage](#basic-usage)
6+
7+
## Requirements
8+
9+
- PHP 8.1 or higher.
10+
11+
## Installation
12+
13+
You can install the library via [Composer](https://getcomposer.org/):
14+
15+
```bash
16+
composer require programmatordev/yet-another-php-validator
17+
```
18+
19+
To use the library, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):
20+
21+
```php
22+
require_once 'vendor/autoload.php';
23+
```
24+
25+
## Basic Usage
26+
27+
Simple usage looks like:
28+
29+
```php
30+
use ProgrammatorDev\YetAnotherPhpValidator\Rule;
31+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
32+
33+
// Do this...
34+
$validator = Validator::notBlank()->greaterThanOrEqual(18);
35+
36+
// ...or this...
37+
$validator = new Validator(
38+
new Rule\NotBlank(),
39+
new Rule\GreaterThanOrEqual(18)
40+
);
41+
42+
// ...or even this...
43+
$validator = (new Validator())
44+
->addRule(new Rule\NotBlank())
45+
->addRule(new Rule\GreaterThanOrEqual(18));
46+
47+
// ...and validate with these:
48+
$validator->validate(16); // returns bool: false
49+
$validator->assert(16, 'Age'); // throws exception: The "Age" value should be greater than or equal to "18", "16" given.
50+
```

docs/02-usage.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Using Yet Another PHP Validator
2+
3+
- Usage
4+
- Fluent
5+
- Dependency Injection
6+
- Validation
7+
- assert
8+
- validate
9+
10+
## Usage
11+
12+
### Fluent
13+
14+
```php
15+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
16+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
17+
18+
/**
19+
* @throws ValidationException
20+
*/
21+
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
22+
{
23+
Validator::range(-90, 90)->assert($latitude, 'Latitude');
24+
Validator::range(-180, 180)->assert($longitude, 'Longitude');
25+
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
26+
27+
// ...
28+
}
29+
```
30+
31+
### Dependency Injection
32+
33+
```php
34+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
35+
use ProgrammatorDev\YetAnotherPhpValidator\Rule;
36+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
37+
38+
/**
39+
* @throws ValidationException
40+
*/
41+
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
42+
{
43+
(new Validator(new Rule\Range(-90, 90)))
44+
->assert($latitude, 'Latitude');
45+
(new Validator(new Rule\Range(-180, 180)))
46+
->assert($longitude, 'Longitude');
47+
(new Validator(new Rule\NotBlank(), new Rule\Choice(['METRIC', 'IMPERIAL'])))
48+
->assert($unitSystem, 'Unit System');
49+
50+
// ...
51+
}
52+
```
53+
54+
A `addRule` method is also available that may be useful for conditional rules:
55+
56+
```php
57+
use ProgrammatorDev\YetAnotherPhpValidator\Rule;
58+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
59+
60+
function calculateDiscount(float $price, float $discount, string $type): float
61+
{
62+
$discountValidator = new Validator(new GreaterThan(0));
63+
64+
if ($type === 'PERCENT') {
65+
$discountValidator->addRule(new Rule\LessThanOrEqual(100));
66+
}
67+
68+
$discountValidator->assert($discount, 'Discount');
69+
70+
// ...
71+
}
72+
```
73+
74+
## Validation
75+
76+
### `assert`
77+
78+
```php
79+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
80+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
81+
82+
// function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem)
83+
84+
try {
85+
getWeatherTemperature(100, 50, 'METRIC');
86+
}
87+
catch (ValidationException $exception) {
88+
echo $exception->getMessage(); // The "Latitude" value should be between "-90" and "90", "100" given.
89+
}
90+
```
91+
92+
### `validate`
93+
94+
```php
95+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
96+
97+
if (!Validator::range(-90, 90)->validate($latitude)) {
98+
// do something...
99+
}
100+
```

docs/03-rules-notblank.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# NotBlank
2+
3+
Validates that a value is not equal to a blank string, blank array, `false` or `null`.
4+
5+
## Basic Usage
6+
7+
```php
8+
Validator::notBlank()->validate(''); // false
9+
Validator::notBlank()->validate([]); // false
10+
Validator::notBlank()->validate(false); // false
11+
Validator::notBlank()->validate(null); // false
12+
```
13+
14+
## Options
15+
16+
```php
17+
use ProgrammatorDev\YetAnotherPhpValidator\Rule;
18+
19+
// Defaults
20+
new Rule\NotBlank(
21+
array $options = [
22+
'normalizer' => null,
23+
'message' => 'The "{{ name }}" value should not be blank, "{{ value }}" given.'
24+
]
25+
);
26+
```
27+
28+
### `options`
29+
30+
type: `array`
31+
32+
`normalizer`
33+
34+
type: `callable` default: `null`
35+
36+
`message`
37+
38+
type: `string` default: `The "{{ name }}" value should not be blank, "{{ value }}" given.`
39+
40+
41+
42+
Normalizer

docs/03-rules.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Rules
2+
3+
- Basic Constraints
4+
- NotBlank
5+
- Comparison Constraints
6+
- LessThan
7+
- LessThanOrEqual
8+
- GreaterThan
9+
- GreaterThanOrEqual
10+
- Range
11+
- Choice Constraints
12+
- Choice
13+
- Other Constraints
14+
- All

src/Rule/AbstractComparisonRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function assert(mixed $value, string $name): void
1818
);
1919
}
2020

21-
if (!$this->comparison($value, $this->constraint)) {
21+
if (!$this->compareValues($value, $this->constraint)) {
2222
throw new ($this->getException())(
2323
message: $this->options['message'],
2424
parameters: [
@@ -30,7 +30,7 @@ public function assert(mixed $value, string $name): void
3030
}
3131
}
3232

33-
protected abstract function comparison(mixed $value1, mixed $value2): bool;
33+
protected abstract function compareValues(mixed $value1, mixed $value2): bool;
3434

3535
protected abstract function getException(): string;
3636
}

src/Rule/GreaterThan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
$this->options = $resolver->resolve($options);
2424
}
2525

26-
protected function comparison(mixed $value1, mixed $value2): bool
26+
protected function compareValues(mixed $value1, mixed $value2): bool
2727
{
2828
if (\is_string($value1) && \is_string($value2)) {
2929
return strcmp($value1, $value2) > 0;

src/Rule/GreaterThanOrEqual.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
$this->options = $resolver->resolve($options);
2424
}
2525

26-
protected function comparison(mixed $value1, mixed $value2): bool
26+
protected function compareValues(mixed $value1, mixed $value2): bool
2727
{
2828
if (\is_string($value1) && \is_string($value2)) {
2929
return strcmp($value1, $value2) >= 0;

src/Rule/LessThan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
$this->options = $resolver->resolve($options);
2424
}
2525

26-
protected function comparison(mixed $value1, mixed $value2): bool
26+
protected function compareValues(mixed $value1, mixed $value2): bool
2727
{
2828
if (\is_string($value1) && \is_string($value2)) {
2929
return strcmp($value1, $value2) < 0;

src/Rule/LessThanOrEqual.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
$this->options = $resolver->resolve($options);
2424
}
2525

26-
protected function comparison(mixed $value1, mixed $value2): bool
26+
protected function compareValues(mixed $value1, mixed $value2): bool
2727
{
2828
if (\is_string($value1) && \is_string($value2)) {
2929
return strcmp($value1, $value2) <= 0;

0 commit comments

Comments
 (0)