diff --git a/docs/03-rules.md b/docs/03-rules.md index 39a6c2e..ddf9037 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) +- [IsFalse](03-rules_is-false.md) - [IsNull](03-rules_is-null.md) - [IsTrue](03-rules_is-true.md) - [NotBlank](03-rules_not-blank.md) diff --git a/docs/03-rules_is-false.md b/docs/03-rules_is-false.md new file mode 100644 index 0000000..ed6346d --- /dev/null +++ b/docs/03-rules_is-false.md @@ -0,0 +1,37 @@ +# IsFalse + +Validates that a value is `false`. + +Check the [IsTrue](03-rules_is-true.md) rule for a `true` validation. + +```php +IsFalse( + ?string $message = null +); +``` + +## Basic Usage + +```php +// anything else will be false +Validator::isFalse()->validate(false); // true +``` + +## Options + +### `message` + +type: `?string` default: `The {{ name }} value should be false, {{ value }} given.` + +Message that will be shown if the value is false. + +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/docs/03-rules_is-true.md b/docs/03-rules_is-true.md index 3594894..6847ade 100644 --- a/docs/03-rules_is-true.md +++ b/docs/03-rules_is-true.md @@ -2,6 +2,8 @@ Validates that a value is `true`. +Check the [IsFalse](03-rules_is-false.md) rule for a `false` validation. + ```php IsTrue( ?string $message = null diff --git a/src/ChainedValidatorInterface.php b/src/ChainedValidatorInterface.php index 1554c93..6fb6d1c 100644 --- a/src/ChainedValidatorInterface.php +++ b/src/ChainedValidatorInterface.php @@ -80,6 +80,10 @@ public function greaterThanOrEqual( ?string $message = null ): ChainedValidatorInterface&Validator; + public function isFalse( + ?string $message = null + ): ChainedValidatorInterface&Validator; + public function isNull( ?string $message = null ): ChainedValidatorInterface&Validator; diff --git a/src/Exception/IsFalseException.php b/src/Exception/IsFalseException.php new file mode 100644 index 0000000..ddc10e3 --- /dev/null +++ b/src/Exception/IsFalseException.php @@ -0,0 +1,5 @@ +message = $message ?? $this->message; + } + + public function assert(mixed $value, ?string $name = null): void + { + if ($value !== false) { + throw new IsFalseException( + 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 ffba8c4..c6079a8 100644 --- a/src/StaticValidatorInterface.php +++ b/src/StaticValidatorInterface.php @@ -120,6 +120,10 @@ public static function notBlank( ?string $message = null ): ChainedValidatorInterface&Validator; + public static function isFalse( + ?string $message = null + ): ChainedValidatorInterface&Validator; + public static function notNull( ?string $message = null ): ChainedValidatorInterface&Validator; diff --git a/tests/IsFalseTest.php b/tests/IsFalseTest.php new file mode 100644 index 0000000..6a83ca2 --- /dev/null +++ b/tests/IsFalseTest.php @@ -0,0 +1,43 @@ + [new IsFalse(), 1, $exception, $message]; + yield 'string' => [new IsFalse(), 'string', $exception, $message]; + yield 'true' => [new IsFalse(), true, $exception, $message]; + yield 'array' => [new IsFalse(), [], $exception, $message]; + } + + public static function provideRuleSuccessConditionData(): \Generator + { + yield 'false' => [new IsFalse(), false]; + } + + public static function provideRuleMessageOptionData(): \Generator + { + yield 'message' => [ + new IsFalse( + message: '{{ name }} | {{ value }}' + ), + true, + 'test | true' + ]; + } +} \ No newline at end of file