|
| 1 | +# Custom Rules |
| 2 | + |
| 3 | +- [Create a Rule](#create-a-rule) |
| 4 | +- [Message Template](#message-template) |
| 5 | + |
| 6 | +## Create a Rule |
| 7 | + |
| 8 | +You can create your own rules to use with this library. |
| 9 | + |
| 10 | +For that, you need to create an exception that extends the `ValidationException` and a rule that extends the `AbstractRule` and implements the `RuleInterface`. |
| 11 | + |
| 12 | +First, create your custom rule exception... |
| 13 | + |
| 14 | +```php |
| 15 | +namespace My\Project\Exception; |
| 16 | + |
| 17 | +use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException; |
| 18 | + |
| 19 | +class CustomRuleException extends ValidationException {} |
| 20 | +``` |
| 21 | + |
| 22 | +...then create your custom rule class... |
| 23 | + |
| 24 | +```php |
| 25 | +namespace My\Project\Rules; |
| 26 | + |
| 27 | +use ProgrammatorDev\YetAnotherPhpValidator\Rule\AbstractRule; |
| 28 | +use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface; |
| 29 | + |
| 30 | +class CustomRule extends AbstractRule implements RuleInterface |
| 31 | +{ |
| 32 | + public function assert(mixed $value, string $name): void |
| 33 | + { |
| 34 | + // Do validation |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +...and finally, you need to throw your custom exception when your validation fails, so it looks something like the following: |
| 40 | + |
| 41 | +```php |
| 42 | +namespace My\Project\Rules; |
| 43 | + |
| 44 | +use My\Project\Exception\CustomRuleException; |
| 45 | +use ProgrammatorDev\YetAnotherPhpValidator\Rule\AbstractRule; |
| 46 | +use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface; |
| 47 | + |
| 48 | +class CustomRule extends AbstractRule implements RuleInterface |
| 49 | +{ |
| 50 | + public function assert(mixed $value, string $name): void |
| 51 | + { |
| 52 | + if ($value === 0) { |
| 53 | + throw new CustomRuleException( |
| 54 | + message: 'The "{{ name }}" value cannot be zero!', |
| 55 | + parameters: [ |
| 56 | + 'name' => $name |
| 57 | + ] |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +In the example above, a new custom rule was created that validates if the input value is not zero. |
| 65 | + |
| 66 | +To use your new custom rule, simply do the following: |
| 67 | + |
| 68 | +```php |
| 69 | +// Fluent way, notice the rule() method |
| 70 | +$validator = Validator::rule(new CustomRule()); |
| 71 | +// With multiple rules |
| 72 | +$validator = Validator::range(-10, 10)->rule(new CustomRule()); |
| 73 | + |
| 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! |
| 80 | +$validator->validate(0); // false |
| 81 | +``` |
| 82 | + |
| 83 | +## Message Template |
| 84 | + |
| 85 | +When an exception extends the `ValidationException`, you're able to create error message templates. |
| 86 | +This means that you can have dynamic content in your messages. |
| 87 | + |
| 88 | +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: |
| 89 | + |
| 90 | +```php |
| 91 | +class FavoriteException extends ValidationException {} |
| 92 | + |
| 93 | +class Favorite extends AbstractRule implements RuleInterface |
| 94 | +{ |
| 95 | + public function __construct( |
| 96 | + private readonly string $favorite |
| 97 | + ) |
| 98 | + |
| 99 | + public function assert(mixed $value, string $name): void |
| 100 | + { |
| 101 | + if ($this->favorite !== $value) { |
| 102 | + throw new FavoriteException( |
| 103 | + message: 'My favorite {{ name }} is "{{ favorite }}", not "{{ value }}"!', |
| 104 | + parameters: [ |
| 105 | + 'name' => $name, |
| 106 | + 'favorite' => $this->favorite, |
| 107 | + 'value' => $value |
| 108 | + ] |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// Throws: My favorite animal is "cat", not "human"! |
| 115 | +Validator::rule(new Favorite('cat'))->assert('human', 'animal'); |
| 116 | +``` |
0 commit comments