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

Commit 53670db

Browse files
authored
Merge pull request #72 from programmatordev/YAPV-55-create-notnull-rule
Create NotNull rule
2 parents 73119bc + c85eeef commit 53670db

8 files changed

+125
-0
lines changed

docs/03-rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Count](03-rules_count.md)
1515
- [IsNull](03-rules_is-null.md)
1616
- [NotBlank](03-rules_not-blank.md)
17+
- [NotNull](03-rules_not-null.md)
1718
- [Type](03-rules_type.md)
1819

1920
## String Rules

docs/03-rules_is-null.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Validates that a value is `null`.
44

5+
Check the [NotNull](03-rules_not-null.md) rule for the opposite validation.
6+
57
```php
68
IsNull(
79
?string $message = null

docs/03-rules_not-null.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# NotNull
2+
3+
Validates that a value is not `null`.
4+
5+
Check the [IsNull](03-rules_is-null.md) rule for the opposite validation.
6+
7+
```php
8+
NotNull(
9+
?string $message = null
10+
);
11+
```
12+
13+
## Basic Usage
14+
15+
```php
16+
// anything else will be true
17+
Validator::notNull()->validate(null); // false
18+
```
19+
20+
## Options
21+
22+
### `message`
23+
24+
type: `?string` default: `The {{ name }} value should not be null.`
25+
26+
Message that will be shown if the value is not null.
27+
28+
The following parameters are available:
29+
30+
| Parameter | Description |
31+
|---------------|---------------------------|
32+
| `{{ name }}` | Name of the invalid value |
33+
34+
## Changelog
35+
36+
- `1.3.0` Created

src/ChainedValidatorInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public function notBlank(
121121
?string $message = null
122122
): ChainedValidatorInterface&Validator;
123123

124+
public function notNull(
125+
?string $message = null
126+
): ChainedValidatorInterface&Validator;
127+
124128
public function optional(
125129
Validator $validator
126130
): ChainedValidatorInterface&Validator;

src/Exception/NotNullException.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class NotNullException extends ValidationException {}

src/Rule/NotNull.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Rule;
4+
5+
use ProgrammatorDev\Validator\Exception\NotNullException;
6+
use ProgrammatorDev\Validator\Validator;
7+
8+
class NotNull extends AbstractRule implements RuleInterface
9+
{
10+
private string $message = 'The {{ name }} value should not be null.';
11+
12+
public function __construct(
13+
?string $message = null
14+
)
15+
{
16+
$this->message = $message ?? $this->message;
17+
}
18+
19+
public function assert(mixed $value, ?string $name = null): void
20+
{
21+
if (Validator::isNull()->validate($value) === true) {
22+
throw new NotNullException(
23+
message: $this->message,
24+
parameters: [
25+
'name' => $name
26+
]
27+
);
28+
}
29+
}
30+
}

src/StaticValidatorInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public static function notBlank(
120120
?string $message = null
121121
): ChainedValidatorInterface&Validator;
122122

123+
public static function notNull(
124+
?string $message = null
125+
): ChainedValidatorInterface&Validator;
126+
123127
public static function optional(
124128
Validator $validator
125129
): ChainedValidatorInterface&Validator;

tests/NotNullTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Test;
4+
5+
use ProgrammatorDev\Validator\Exception\NotNullException;
6+
use ProgrammatorDev\Validator\Rule\NotNull;
7+
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait;
8+
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait;
9+
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait;
10+
11+
class NotNullTest extends AbstractTest
12+
{
13+
use TestRuleFailureConditionTrait;
14+
use TestRuleSuccessConditionTrait;
15+
use TestRuleMessageOptionTrait;
16+
17+
public static function provideRuleFailureConditionData(): \Generator
18+
{
19+
$exception = NotNullException::class;
20+
$message = '/The (.*) value should not be null\./';
21+
22+
yield 'null' => [new NotNull(), null, $exception, $message];
23+
}
24+
25+
public static function provideRuleSuccessConditionData(): \Generator
26+
{
27+
yield 'int' => [new NotNull(), 1];
28+
yield 'string' => [new NotNull(), 'string'];
29+
yield 'bool' => [new NotNull(), true];
30+
yield 'array' => [new NotNull(), []];
31+
}
32+
33+
public static function provideRuleMessageOptionData(): \Generator
34+
{
35+
yield 'message' => [
36+
new NotNull(
37+
message: '{{ name }}'
38+
),
39+
null,
40+
'test'
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)