-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from programmatordev/YAPV-58-create-isfalse-rule
Create IsFalse rule
- Loading branch information
Showing
8 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace ProgrammatorDev\Validator\Exception; | ||
|
||
class IsFalseException extends ValidationException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace ProgrammatorDev\Validator\Rule; | ||
|
||
use ProgrammatorDev\Validator\Exception\IsFalseException; | ||
|
||
class IsFalse extends AbstractRule implements RuleInterface | ||
{ | ||
private string $message = 'The {{ name }} value should be false, {{ value }} given.'; | ||
|
||
public function __construct( | ||
?string $message = null | ||
) | ||
{ | ||
$this->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 | ||
] | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace ProgrammatorDev\Validator\Test; | ||
|
||
use ProgrammatorDev\Validator\Exception\IsFalseException; | ||
use ProgrammatorDev\Validator\Rule\IsFalse; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait; | ||
|
||
class IsFalseTest extends AbstractTest | ||
{ | ||
use TestRuleFailureConditionTrait; | ||
use TestRuleSuccessConditionTrait; | ||
use TestRuleMessageOptionTrait; | ||
|
||
public static function provideRuleFailureConditionData(): \Generator | ||
{ | ||
$exception = IsFalseException::class; | ||
$message = '/The (.*) value should be false, (.*) given\./'; | ||
|
||
yield 'int' => [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' | ||
]; | ||
} | ||
} |