Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create IsTrue rule #73

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/03-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Blank](03-rules_blank.md)
- [Count](03-rules_count.md)
- [IsNull](03-rules_is-null.md)
- [IsTrue](03-rules_is-true.md)
- [NotBlank](03-rules_not-blank.md)
- [NotNull](03-rules_not-null.md)
- [Type](03-rules_type.md)
Expand Down
35 changes: 35 additions & 0 deletions docs/03-rules_is-true.md
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
4 changes: 4 additions & 0 deletions src/ChainedValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function isNull(
?string $message = null
): ChainedValidatorInterface&Validator;

public function isTrue(
?string $message = null
): ChainedValidatorInterface&Validator;

public function language(
string $code = 'alpha-2',
?string $message = null
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/IsTrueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ProgrammatorDev\Validator\Exception;

class IsTrueException extends ValidationException {}
30 changes: 30 additions & 0 deletions src/Rule/IsTrue.php
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
]
);
}
}
}
4 changes: 4 additions & 0 deletions src/StaticValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public static function notNull(
?string $message = null
): ChainedValidatorInterface&Validator;

public static function isTrue(
?string $message = null
): ChainedValidatorInterface&Validator;

public static function optional(
Validator $validator
): ChainedValidatorInterface&Validator;
Expand Down
43 changes: 43 additions & 0 deletions tests/IsTrueTest.php
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'
];
}
}
Loading