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

Create NotNull rule #72

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 @@ -14,6 +14,7 @@
- [Count](03-rules_count.md)
- [IsNull](03-rules_is-null.md)
- [NotBlank](03-rules_not-blank.md)
- [NotNull](03-rules_not-null.md)
- [Type](03-rules_type.md)

## String Rules
Expand Down
2 changes: 2 additions & 0 deletions docs/03-rules_is-null.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Validates that a value is `null`.

Check the [NotNull](03-rules_not-null.md) rule for the opposite validation.

```php
IsNull(
?string $message = null
Expand Down
36 changes: 36 additions & 0 deletions docs/03-rules_not-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# NotNull

Validates that a value is not `null`.

Check the [IsNull](03-rules_is-null.md) rule for the opposite validation.

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

## Basic Usage

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

## Options

### `message`

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

Message that will be shown if the value is not null.

The following parameters are available:

| Parameter | Description |
|---------------|---------------------------|
| `{{ 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 @@ -121,6 +121,10 @@ public function notBlank(
?string $message = null
): ChainedValidatorInterface&Validator;

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

public function optional(
Validator $validator
): ChainedValidatorInterface&Validator;
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/NotNullException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ProgrammatorDev\Validator\Exception;

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

namespace ProgrammatorDev\Validator\Rule;

use ProgrammatorDev\Validator\Exception\NotNullException;
use ProgrammatorDev\Validator\Validator;

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

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

public function assert(mixed $value, ?string $name = null): void
{
if (Validator::isNull()->validate($value) === true) {
throw new NotNullException(
message: $this->message,
parameters: [
'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 notNull(
?string $message = null
): ChainedValidatorInterface&Validator;

public static function optional(
Validator $validator
): ChainedValidatorInterface&Validator;
Expand Down
43 changes: 43 additions & 0 deletions tests/NotNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace ProgrammatorDev\Validator\Test;

use ProgrammatorDev\Validator\Exception\NotNullException;
use ProgrammatorDev\Validator\Rule\NotNull;
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait;
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait;
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait;

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

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

yield 'null' => [new NotNull(), null, $exception, $message];
}

public static function provideRuleSuccessConditionData(): \Generator
{
yield 'int' => [new NotNull(), 1];
yield 'string' => [new NotNull(), 'string'];
yield 'bool' => [new NotNull(), true];
yield 'array' => [new NotNull(), []];
}

public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new NotNull(
message: '{{ name }}'
),
null,
'test'
];
}
}
Loading