diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1e4fd0db..e95e132a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -42,7 +42,7 @@ create a validator that validates if a string is equal to "Hello World". ### Creating the rule The rule itself needs to implement the `Validatable` interface but, it is -convenient to just extend the `AbstractRule` class. +convenient to just extend the `Simple` or `Standard` class. Doing that, you'll only need to declare one method: `validate($input)`. This method must return `true` or `false`. @@ -62,14 +62,15 @@ declare(strict_types=1); namespace Respect\Validation\Rules; use Respect\Validation\Message\Template; +use Respect\Validation\Rules\Core\Simple; #[Template( '{{name}} must be a Hello World', '{{name}} must not be a Hello World', )] -final class HelloWorld extends AbstractRule +final class HelloWorld extends Simple { - public function validate(mixed $input): bool + protected function isValid(mixed $input): bool { return $input === 'Hello World'; } diff --git a/docs/06-custom-rules.md b/docs/06-custom-rules.md index 0184ad32..fbefad90 100644 --- a/docs/06-custom-rules.md +++ b/docs/06-custom-rules.md @@ -3,23 +3,24 @@ You can also create and use your own rules. To do this, you will need to create a rule and an exception to go with the rule. -To create a rule, you need to create a class that extends the AbstractRule class -and is within the Rules `namespace`. When the rule is called the logic inside the -validate method will be executed. Here's how the class should look: +To create a rule, you need to create a class that implements the `Validatable` interface +and is within the Rules `namespace`. It is convenient to just extend the `Simple` or +`Standard` class. When the rule is called the logic inside the validate method will be +executed. Here's how the class should look: ```php namespace My\Validation\Rules; -use Respect\Validation\Rules\AbstractRule; use Respect\Validation\Message\Template; +use Respect\Validation\Rules\Core\Simple; #[Template( '{{name}} is something', '{{name}} is not something', )] -final class Something extends AbstractRule +final class Something extends Simple { - public function validate(mixed $input): bool + protected function isValid(mixed $input): bool { // Do something here with the $input and return a boolean value } diff --git a/library/Exceptions/ValidationException.php b/library/Exceptions/ValidationException.php index e9e89565..970c8605 100644 --- a/library/Exceptions/ValidationException.php +++ b/library/Exceptions/ValidationException.php @@ -10,106 +10,26 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; use InvalidArgumentException; -use Respect\Validation\Message\Template; -use Respect\Validation\Message\TemplateRenderer; -use function count; -use function preg_match; - -class ValidationException extends InvalidArgumentException implements Exception +final class ValidationException extends InvalidArgumentException implements Exception { - public const MODE_DEFAULT = 'default'; - public const MODE_NEGATIVE = 'negative'; - - private string $mode = self::MODE_DEFAULT; - - /** - * @var array