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

Create IsNull rule #71

Merged
merged 2 commits 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)
- [IsNull](03-rules_is-null.md)
- [NotBlank](03-rules_not-blank.md)
- [Type](03-rules_type.md)

Expand Down
35 changes: 35 additions & 0 deletions docs/03-rules_is-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# IsNull

Validates that a value is `null`.

```php
IsNull(
?string $message = null
);
```

## Basic Usage

```php
// anything else will be false
Validator::isNull()->validate(null); // true
```

## Options

### `message`

type: `?string` default: `The {{ name }} value should be null, {{ value }} given.`

Message that will be shown if the value is null.

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 @@ -80,6 +80,10 @@ public function greaterThanOrEqual(
?string $message = null
): ChainedValidatorInterface&Validator;

public function isNull(
?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/IsNullException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ProgrammatorDev\Validator\Exception;

class IsNullException extends ValidationException {}
30 changes: 30 additions & 0 deletions src/Rule/IsNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ProgrammatorDev\Validator\Rule;

use ProgrammatorDev\Validator\Exception\IsNullException;

class IsNull extends AbstractRule implements RuleInterface
{
private string $message = 'The {{ name }} value should be null, {{ value }} given.';

public function __construct(
?string $message = null
)
{
$this->message = $message ?? $this->message;
}

public function assert(mixed $value, ?string $name = null): void
{
if (!\is_null($value)) {
throw new IsNullException(
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 @@ -79,6 +79,10 @@ public static function greaterThanOrEqual(
?string $message = null
): ChainedValidatorInterface&Validator;

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

public static function language(
string $code = 'alpha-2',
?string $message = null
Expand Down
15 changes: 2 additions & 13 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use ProgrammatorDev\Validator\Exception\UnexpectedValueException;
use ProgrammatorDev\Validator\Exception\ValidationException;
use ProgrammatorDev\Validator\Factory\Factory;
use ProgrammatorDev\Validator\Rule\AbstractRule;
use ProgrammatorDev\Validator\Rule\RuleInterface;

/**
* @mixin StaticValidatorInterface
*/
class Validator implements RuleInterface
class Validator extends AbstractRule implements RuleInterface
{
/** @var RuleInterface[] */
private array $rules;
Expand Down Expand Up @@ -59,18 +60,6 @@ public function assert(mixed $value, ?string $name = null): void
}
}

public function validate(mixed $value): bool
{
try {
$this->assert($value);
}
catch (ValidationException) {
return false;
}

return true;
}

public function getRules(): array
{
return $this->rules;
Expand Down
43 changes: 43 additions & 0 deletions tests/IsNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace ProgrammatorDev\Validator\Test;

use ProgrammatorDev\Validator\Exception\IsNullException;
use ProgrammatorDev\Validator\Rule\IsNull;
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait;
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait;
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait;

class IsNullTest extends AbstractTest
{
use TestRuleFailureConditionTrait;
use TestRuleSuccessConditionTrait;
use TestRuleMessageOptionTrait;

public static function provideRuleFailureConditionData(): \Generator
{
$exception = IsNullException::class;
$message = '/The (.*) value should be null, (.*) given\./';

yield 'int' => [new IsNull(), 1, $exception, $message];
yield 'string' => [new IsNull(), 'string', $exception, $message];
yield 'bool' => [new IsNull(), true, $exception, $message];
yield 'array' => [new IsNull(), [], $exception, $message];
}

public static function provideRuleSuccessConditionData(): \Generator
{
yield 'null' => [new IsNull(), null];
}

public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new IsNull(
message: '{{ name }} | {{ value }}'
),
'string',
'test | "string"'
];
}
}
Loading