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

Commit 59df963

Browse files
authored
Merge pull request #2 from programmatordev/YAPV-6-create-notblank-rule
Create notBlank rule
2 parents 8c4ab65 + cdcfe06 commit 59df963

7 files changed

Lines changed: 133 additions & 4 deletions

File tree

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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
}
Lines changed: 5 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
public function __construct(private ?string $message = null)
10+
{
11+
$this->message ??= 'The {{ name }} value should not be blank.';
12+
}
13+
14+
/**
15+
* @throws NotBlankException
16+
*/
17+
public function validate(mixed $input): void
18+
{
19+
// Strip whitespace in case of a string
20+
if (\is_string($input)) {
21+
$input = trim($input);
22+
}
23+
24+
// Do not allow null, false, [] and ''
25+
if ($input === false || (empty($input) && $input != '0')) {
26+
throw new NotBlankException(
27+
message: $this->message,
28+
parameters: [
29+
'input' => $input,
30+
'name' => $this->getName()
31+
]
32+
);
33+
}
34+
}
35+
}

src/StaticValidatorInterface.php

Lines changed: 2 additions & 2 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 59 additions & 0 deletions
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)