-
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.
- Loading branch information
1 parent
53670db
commit 29eab27
Showing
7 changed files
with
122 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,35 @@ | ||
# IsTrue | ||
|
||
Validates that a value is `true`. | ||
|
||
```php | ||
IsTrue( | ||
?string $message = null | ||
); | ||
``` | ||
|
||
## Basic Usage | ||
|
||
```php | ||
// anything else will be false | ||
Validator::isTrue()->validate(true); // true | ||
``` | ||
|
||
## Options | ||
|
||
### `message` | ||
|
||
type: `?string` default: `The {{ name }} value should be true, {{ value }} given.` | ||
|
||
Message that will be shown if the value is true. | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace ProgrammatorDev\Validator\Exception; | ||
|
||
class IsTrueException 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\IsTrueException; | ||
|
||
class IsTrue extends AbstractRule implements RuleInterface | ||
{ | ||
private string $message = 'The {{ name }} value should be true, {{ value }} given.'; | ||
|
||
public function __construct( | ||
?string $message = null | ||
) | ||
{ | ||
$this->message = $message ?? $this->message; | ||
} | ||
|
||
public function assert(mixed $value, ?string $name = null): void | ||
{ | ||
if ($value !== true) { | ||
throw new IsTrueException( | ||
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\IsTrueException; | ||
use ProgrammatorDev\Validator\Rule\IsTrue; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait; | ||
|
||
class IsTrueTest extends AbstractTest | ||
{ | ||
use TestRuleFailureConditionTrait; | ||
use TestRuleSuccessConditionTrait; | ||
use TestRuleMessageOptionTrait; | ||
|
||
public static function provideRuleFailureConditionData(): \Generator | ||
{ | ||
$exception = IsTrueException::class; | ||
$message = '/The (.*) value should be true, (.*) given\./'; | ||
|
||
yield 'int' => [new IsTrue(), 1, $exception, $message]; | ||
yield 'string' => [new IsTrue(), 'string', $exception, $message]; | ||
yield 'false' => [new IsTrue(), false, $exception, $message]; | ||
yield 'array' => [new IsTrue(), [], $exception, $message]; | ||
} | ||
|
||
public static function provideRuleSuccessConditionData(): \Generator | ||
{ | ||
yield 'true' => [new IsTrue(), true]; | ||
} | ||
|
||
public static function provideRuleMessageOptionData(): \Generator | ||
{ | ||
yield 'message' => [ | ||
new IsTrue( | ||
message: '{{ name }} | {{ value }}' | ||
), | ||
false, | ||
'test | false' | ||
]; | ||
} | ||
} |