From 54c2336183b486daab9dca2d3d1522081996750d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Wed, 7 Aug 2024 16:04:09 +0100 Subject: [PATCH 1/2] chore: refactored Validator class --- src/Validator.php | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index e1748d4..2b8eca1 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -6,12 +6,13 @@ use ProgrammatorDev\Validator\Exception\UnexpectedValueException; use ProgrammatorDev\Validator\Exception\ValidationException; use ProgrammatorDev\Validator\Factory\Factory; +use ProgrammatorDev\Validator\Rule\AbstractRule; use ProgrammatorDev\Validator\Rule\RuleInterface; /** * @mixin StaticValidatorInterface */ -class Validator implements RuleInterface +class Validator extends AbstractRule implements RuleInterface { /** @var RuleInterface[] */ private array $rules; @@ -59,18 +60,6 @@ public function assert(mixed $value, ?string $name = null): void } } - public function validate(mixed $value): bool - { - try { - $this->assert($value); - } - catch (ValidationException) { - return false; - } - - return true; - } - public function getRules(): array { return $this->rules; From 89ea05d17643a77697d963e70960f193857cbc2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Wed, 7 Aug 2024 16:35:32 +0100 Subject: [PATCH 2/2] feat: added isNull rule --- docs/03-rules.md | 1 + docs/03-rules_is-null.md | 35 +++++++++++++++++++++++++ src/ChainedValidatorInterface.php | 4 +++ src/Exception/IsNullException.php | 5 ++++ src/Rule/IsNull.php | 30 +++++++++++++++++++++ src/StaticValidatorInterface.php | 4 +++ tests/IsNullTest.php | 43 +++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+) create mode 100644 docs/03-rules_is-null.md create mode 100644 src/Exception/IsNullException.php create mode 100644 src/Rule/IsNull.php create mode 100644 tests/IsNullTest.php diff --git a/docs/03-rules.md b/docs/03-rules.md index 58474c3..a435d51 100644 --- a/docs/03-rules.md +++ b/docs/03-rules.md @@ -12,6 +12,7 @@ - [Blank](03-rules_blank.md) - [Count](03-rules_count.md) +- [IsNull](03-rules_is-null.md) - [NotBlank](03-rules_not-blank.md) - [Type](03-rules_type.md) diff --git a/docs/03-rules_is-null.md b/docs/03-rules_is-null.md new file mode 100644 index 0000000..00afcf9 --- /dev/null +++ b/docs/03-rules_is-null.md @@ -0,0 +1,35 @@ +# IsNull + +Validates that a value is `null`. + +```php +IsNull( + ?string $message = null +); +``` + +## Basic Usage + +```php +// anything else will be false +Validator::isNull()->validate(null); // true +``` + +## Options + +### `message` + +type: `?string` default: `The {{ name }} value should be null, {{ value }} given.` + +Message that will be shown if the value is null. + +The following parameters are available: + +| Parameter | Description | +|---------------|---------------------------| +| `{{ value }}` | The current invalid value | +| `{{ name }}` | Name of the invalid value | + +## Changelog + +- `1.3.0` Created \ No newline at end of file diff --git a/src/ChainedValidatorInterface.php b/src/ChainedValidatorInterface.php index c7b286b..8aec8f0 100644 --- a/src/ChainedValidatorInterface.php +++ b/src/ChainedValidatorInterface.php @@ -80,6 +80,10 @@ public function greaterThanOrEqual( ?string $message = null ): ChainedValidatorInterface&Validator; + public function isNull( + ?string $message = null + ): ChainedValidatorInterface&Validator; + public function language( string $code = 'alpha-2', ?string $message = null diff --git a/src/Exception/IsNullException.php b/src/Exception/IsNullException.php new file mode 100644 index 0000000..fe9725e --- /dev/null +++ b/src/Exception/IsNullException.php @@ -0,0 +1,5 @@ +message = $message ?? $this->message; + } + + public function assert(mixed $value, ?string $name = null): void + { + if (!\is_null($value)) { + throw new IsNullException( + message: $this->message, + parameters: [ + 'value' => $value, + 'name' => $name + ] + ); + } + } +} \ No newline at end of file diff --git a/src/StaticValidatorInterface.php b/src/StaticValidatorInterface.php index 691032e..7b759c8 100644 --- a/src/StaticValidatorInterface.php +++ b/src/StaticValidatorInterface.php @@ -79,6 +79,10 @@ public static function greaterThanOrEqual( ?string $message = null ): ChainedValidatorInterface&Validator; + public static function isNull( + ?string $message = null + ): ChainedValidatorInterface&Validator; + public static function language( string $code = 'alpha-2', ?string $message = null diff --git a/tests/IsNullTest.php b/tests/IsNullTest.php new file mode 100644 index 0000000..1524430 --- /dev/null +++ b/tests/IsNullTest.php @@ -0,0 +1,43 @@ + [new IsNull(), 1, $exception, $message]; + yield 'string' => [new IsNull(), 'string', $exception, $message]; + yield 'bool' => [new IsNull(), true, $exception, $message]; + yield 'array' => [new IsNull(), [], $exception, $message]; + } + + public static function provideRuleSuccessConditionData(): \Generator + { + yield 'null' => [new IsNull(), null]; + } + + public static function provideRuleMessageOptionData(): \Generator + { + yield 'message' => [ + new IsNull( + message: '{{ name }} | {{ value }}' + ), + 'string', + 'test | "string"' + ]; + } +} \ No newline at end of file