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

Commit be82cf0

Browse files
committed
feat: added blank rule
1 parent d2a1175 commit be82cf0

File tree

6 files changed

+111
-9
lines changed

6 files changed

+111
-9
lines changed

src/ChainedValidatorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
interface ChainedValidatorInterface
99
{
10+
public function blank(
11+
?callable $normalizer = null,
12+
?string $message = null
13+
): ChainedValidatorInterface&Validator;
14+
1015
public function choice(
1116
array $constraints,
1217
bool $multiple = false,

src/Exception/BlankException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class BlankException extends ValidationException {}

src/Rule/Blank.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Rule;
4+
5+
use ProgrammatorDev\Validator\Exception\BlankException;
6+
7+
class Blank extends AbstractRule implements RuleInterface
8+
{
9+
/** @var ?callable */
10+
private $normalizer;
11+
private string $message = 'The {{ name }} value should be blank, {{ value }} given.';
12+
13+
public function __construct(
14+
?callable $normalizer = null,
15+
?string $message = null
16+
)
17+
{
18+
$this->normalizer = $normalizer;
19+
$this->message = $message ?? $this->message;
20+
}
21+
22+
public function assert(mixed $value, ?string $name = null): void
23+
{
24+
if ($this->normalizer !== null) {
25+
$value = ($this->normalizer)($value);
26+
}
27+
28+
if ($value !== null && $value !== false && $value !== '' && $value !== []) {
29+
throw new BlankException(
30+
message: $this->message,
31+
parameters: [
32+
'value' => $value,
33+
'name' => $name
34+
]
35+
);
36+
}
37+
}
38+
}

src/Rule/NotBlank.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProgrammatorDev\Validator\Rule;
44

55
use ProgrammatorDev\Validator\Exception\NotBlankException;
6+
use ProgrammatorDev\Validator\Validator;
67

78
class NotBlank extends AbstractRule implements RuleInterface
89
{
@@ -19,17 +20,9 @@ public function __construct(
1920
$this->message = $message ?? $this->message;
2021
}
2122

22-
/**
23-
* @throws NotBlankException
24-
*/
2523
public function assert(mixed $value, ?string $name = null): void
2624
{
27-
if ($this->normalizer !== null) {
28-
$value = ($this->normalizer)($value);
29-
}
30-
31-
// Do not allow null, false, [] and ''
32-
if ($value === false || (empty($value) && $value != '0')) {
25+
if (Validator::blank(normalizer: $this->normalizer)->validate($value) === true) {
3326
throw new NotBlankException(
3427
message: $this->message,
3528
parameters: [

src/StaticValidatorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
interface StaticValidatorInterface
88
{
9+
public static function blank(
10+
?callable $normalizer = null,
11+
?string $message = null
12+
): ChainedValidatorInterface&Validator;
13+
914
public static function choice(
1015
array $constraints,
1116
bool $multiple = false,

tests/BlankTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Test;
4+
5+
use ProgrammatorDev\Validator\Exception\BlankException;
6+
use ProgrammatorDev\Validator\Rule\Blank;
7+
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait;
8+
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait;
9+
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait;
10+
11+
class BlankTest extends AbstractTest
12+
{
13+
use TestRuleFailureConditionTrait;
14+
use TestRuleSuccessConditionTrait;
15+
use TestRuleMessageOptionTrait;
16+
17+
public static function provideRuleFailureConditionData(): \Generator
18+
{
19+
$exception = BlankException::class;
20+
$message = '/The (.*) value should be blank, (.*) given\./';
21+
22+
yield 'true' => [new Blank(), true, $exception, $message];
23+
24+
yield 'string' => [new Blank(), 'string', $exception, $message];
25+
yield 'whitespace string' => [new Blank(), ' ', $exception, $message];
26+
yield 'zero string' => [new Blank(), '0', $exception, $message];
27+
28+
yield 'array' => [new Blank(), ['string'], $exception, $message];
29+
yield 'blank string array' => [new Blank(), [''], $exception, $message];
30+
yield 'whitespace array' => [new Blank(), [' '], $exception, $message];
31+
yield 'zero array' => [new Blank(), [0], $exception, $message];
32+
33+
yield 'number' => [new Blank(), 10, $exception, $message];
34+
yield 'zero number' => [new Blank(), 0, $exception, $message];
35+
}
36+
37+
public static function provideRuleSuccessConditionData(): \Generator
38+
{
39+
yield 'null' => [new Blank(), null];
40+
yield 'false' => [new Blank(), false];
41+
yield 'blank string' => [new Blank(), ''];
42+
yield 'blank array' => [new Blank(), []];
43+
44+
yield 'normalizer whitespace' => [new Blank(normalizer: 'trim'), ' '];
45+
yield 'normalizer whitespace function' => [new Blank(normalizer: fn($value) => trim($value)), ' '];
46+
}
47+
48+
public static function provideRuleMessageOptionData(): \Generator
49+
{
50+
yield 'message' => [
51+
new Blank(message: '{{ name }} | {{ value }}'),
52+
'string',
53+
'test | "string"'
54+
];
55+
}
56+
}

0 commit comments

Comments
 (0)