|
| 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 | +``` |
0 commit comments