Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Create IsFalse rule #74

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 @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions docs/03-rules_is-false.md
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
2 changes: 2 additions & 0 deletions docs/03-rules_is-true.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/ChainedValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/IsFalseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ProgrammatorDev\Validator\Exception;

class IsFalseException extends ValidationException {}
30 changes: 30 additions & 0 deletions src/Rule/IsFalse.php
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
]
);
}
}
}
4 changes: 4 additions & 0 deletions src/StaticValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions tests/IsFalseTest.php
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'
];
}
}
Loading