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

Commit 98eb0ff

Browse files
committed
feat: added notBlank rule
1 parent 8c4ab65 commit 98eb0ff

7 files changed

+135
-4
lines changed

phpunit.xml.dist

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
colors="true"
6+
bootstrap="vendor/autoload.php"
7+
stopOnError="true"
8+
>
9+
<php>
10+
<ini name="memory_limit" value="-1" />
11+
<ini name="error_reporting" value="-1" />
12+
<ini name="log_errors_max_len" value="0" />
13+
<ini name="zend.assertions" value="1" />
14+
<ini name="assert.exception" value="1" />
15+
<ini name="xdebug.show_exception_trace" value="0" />
16+
</php>
17+
18+
<testsuites>
19+
<testsuite name="Project Test Suite">
20+
<directory>tests</directory>
21+
</testsuite>
22+
</testsuites>
23+
</phpunit>

src/ChainedValidatorInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function assert(mixed $input, string $name): void;
1717

1818
// --- Rules ---
1919

20-
// public function notBlank(): ChainedValidatorInterface;
21-
//
20+
public function notBlank(?string $message = null): ChainedValidatorInterface;
21+
2222
// public function greaterThan(mixed $constraint): ChainedValidatorInterface;
2323
}

src/Exception/NotBlankException.php

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

src/Rule/NotBlank.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
6+
7+
class NotBlank extends AbstractRule implements RuleInterface
8+
{
9+
private string $message;
10+
11+
public function __construct(?string $message = null)
12+
{
13+
$this->message = $message ?? 'The {{ name }} value should not be blank.';
14+
}
15+
16+
/**
17+
* @throws NotBlankException
18+
*/
19+
public function validate(mixed $input): void
20+
{
21+
// Strip whitespace in case of a string
22+
if (\is_string($input)) {
23+
$input = trim($input);
24+
}
25+
26+
// Does not allow null, false, [] and ''
27+
if ($input === false || (empty($input) && $input != '0')) {
28+
throw new NotBlankException(
29+
message: $this->message,
30+
parameters: [
31+
'input' => $input,
32+
'name' => $this->getName()
33+
]
34+
);
35+
}
36+
}
37+
}

src/StaticValidatorInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
interface StaticValidatorInterface
66
{
7-
// public static function notBlank(): ChainedValidatorInterface;
8-
//
7+
public static function notBlank(?string $message = null): ChainedValidatorInterface;
8+
99
// public static function greaterThan(mixed $constraint): ChainedValidatorInterface;
1010
}

src/Test/AbstractTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class AbstractTest extends TestCase {}

tests/NotBlankTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
8+
9+
class NotBlankTest extends AbstractTest
10+
{
11+
#[DataProvider('provideInvalidInputData')]
12+
public function testNotBlankInvalidInput(mixed $input)
13+
{
14+
$validator = Validator::notBlank();
15+
16+
$this->assertFalse($validator->validate($input));
17+
18+
$this->expectException(NotBlankException::class);
19+
$this->expectExceptionMessage('The "test" value should not be blank.');
20+
$validator->assert($input, 'test');
21+
}
22+
23+
public static function provideInvalidInputData(): \Generator
24+
{
25+
yield 'null' => [null];
26+
yield 'false' => [false];
27+
yield 'blank array' => [[]];
28+
yield 'blank string' => [''];
29+
yield 'whitespace' => [' '];
30+
}
31+
32+
#[DataProvider('provideValidInputData')]
33+
public function testNotBlankValidInput(mixed $input)
34+
{
35+
$validator = Validator::notBlank();
36+
37+
$this->assertTrue($validator->validate($input));
38+
39+
Validator::notBlank()->assert($input, 'test');
40+
}
41+
42+
public static function provideValidInputData(): \Generator
43+
{
44+
yield 'true' => [true];
45+
yield 'zero number' => [0];
46+
yield 'zero string' => ['0'];
47+
yield 'array' => [[0]];
48+
yield 'string' => ['string'];
49+
}
50+
51+
public function testNotBlankExceptionMessageParameters()
52+
{
53+
$this->expectExceptionMessage('The "test" value false is invalid. Must not be blank.');
54+
55+
Validator::notBlank(
56+
message: 'The {{ name }} value {{ input }} is invalid. Must not be blank.'
57+
)->assert(false, 'test');
58+
}
59+
}

0 commit comments

Comments
 (0)