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

Commit 73119bc

Browse files
authored
Merge pull request #71 from programmatordev/YAPV-56-create-isnull-rule
Create IsNull rule
2 parents 1818f7e + 89ea05d commit 73119bc

8 files changed

+124
-13
lines changed

docs/03-rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
- [Blank](03-rules_blank.md)
1414
- [Count](03-rules_count.md)
15+
- [IsNull](03-rules_is-null.md)
1516
- [NotBlank](03-rules_not-blank.md)
1617
- [Type](03-rules_type.md)
1718

docs/03-rules_is-null.md

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

src/ChainedValidatorInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public function greaterThanOrEqual(
8080
?string $message = null
8181
): ChainedValidatorInterface&Validator;
8282

83+
public function isNull(
84+
?string $message = null
85+
): ChainedValidatorInterface&Validator;
86+
8387
public function language(
8488
string $code = 'alpha-2',
8589
?string $message = null

src/Exception/IsNullException.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 IsNullException extends ValidationException {}

src/Rule/IsNull.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\IsNullException;
6+
7+
class IsNull extends AbstractRule implements RuleInterface
8+
{
9+
private string $message = 'The {{ name }} value should be null, {{ value }} given.';
10+
11+
public function __construct(
12+
?string $message = null
13+
)
14+
{
15+
$this->message = $message ?? $this->message;
16+
}
17+
18+
public function assert(mixed $value, ?string $name = null): void
19+
{
20+
if (!\is_null($value)) {
21+
throw new IsNullException(
22+
message: $this->message,
23+
parameters: [
24+
'value' => $value,
25+
'name' => $name
26+
]
27+
);
28+
}
29+
}
30+
}

src/StaticValidatorInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public static function greaterThanOrEqual(
7979
?string $message = null
8080
): ChainedValidatorInterface&Validator;
8181

82+
public static function isNull(
83+
?string $message = null
84+
): ChainedValidatorInterface&Validator;
85+
8286
public static function language(
8387
string $code = 'alpha-2',
8488
?string $message = null

src/Validator.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
use ProgrammatorDev\Validator\Exception\UnexpectedValueException;
77
use ProgrammatorDev\Validator\Exception\ValidationException;
88
use ProgrammatorDev\Validator\Factory\Factory;
9+
use ProgrammatorDev\Validator\Rule\AbstractRule;
910
use ProgrammatorDev\Validator\Rule\RuleInterface;
1011

1112
/**
1213
* @mixin StaticValidatorInterface
1314
*/
14-
class Validator implements RuleInterface
15+
class Validator extends AbstractRule implements RuleInterface
1516
{
1617
/** @var RuleInterface[] */
1718
private array $rules;
@@ -59,18 +60,6 @@ public function assert(mixed $value, ?string $name = null): void
5960
}
6061
}
6162

62-
public function validate(mixed $value): bool
63-
{
64-
try {
65-
$this->assert($value);
66-
}
67-
catch (ValidationException) {
68-
return false;
69-
}
70-
71-
return true;
72-
}
73-
7463
public function getRules(): array
7564
{
7665
return $this->rules;

tests/IsNullTest.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\IsNullException;
6+
use ProgrammatorDev\Validator\Rule\IsNull;
7+
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait;
8+
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait;
9+
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait;
10+
11+
class IsNullTest extends AbstractTest
12+
{
13+
use TestRuleFailureConditionTrait;
14+
use TestRuleSuccessConditionTrait;
15+
use TestRuleMessageOptionTrait;
16+
17+
public static function provideRuleFailureConditionData(): \Generator
18+
{
19+
$exception = IsNullException::class;
20+
$message = '/The (.*) value should be null, (.*) given\./';
21+
22+
yield 'int' => [new IsNull(), 1, $exception, $message];
23+
yield 'string' => [new IsNull(), 'string', $exception, $message];
24+
yield 'bool' => [new IsNull(), true, $exception, $message];
25+
yield 'array' => [new IsNull(), [], $exception, $message];
26+
}
27+
28+
public static function provideRuleSuccessConditionData(): \Generator
29+
{
30+
yield 'null' => [new IsNull(), null];
31+
}
32+
33+
public static function provideRuleMessageOptionData(): \Generator
34+
{
35+
yield 'message' => [
36+
new IsNull(
37+
message: '{{ name }} | {{ value }}'
38+
),
39+
'string',
40+
'test | "string"'
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)