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

Commit ee12cf3

Browse files
committed
chore(unrelated): improved validation naming and NotBlank rule
1 parent 59df963 commit ee12cf3

7 files changed

Lines changed: 104 additions & 82 deletions

File tree

src/ChainedValidatorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ interface ChainedValidatorInterface
88
{
99
// --- Common ---
1010

11-
public function validate(mixed $input): bool;
11+
public function validate(mixed $value): bool;
1212

1313
/**
1414
* @throws ValidationException
1515
*/
16-
public function assert(mixed $input, string $name): void;
16+
public function assert(mixed $value, string $name): void;
1717

1818
// --- Rules ---
1919

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception\Util;
4+
5+
trait FormatValueTrait
6+
{
7+
private function formatValue(mixed $value): string
8+
{
9+
if ($value instanceof \DateTimeInterface) {
10+
return $value->format('Y-m-d H:i:s');
11+
}
12+
13+
if (\is_object($value)) {
14+
if ($value instanceof \Stringable) {
15+
return $value->__toString();
16+
}
17+
18+
return 'object';
19+
}
20+
21+
if (\is_array($value)) {
22+
return $this->formatValues($value);
23+
}
24+
25+
if (\is_string($value)) {
26+
return $value;
27+
}
28+
29+
if (\is_resource($value)) {
30+
return 'resource';
31+
}
32+
33+
if ($value === null) {
34+
return 'null';
35+
}
36+
37+
if ($value === false) {
38+
return 'false';
39+
}
40+
41+
if ($value === true) {
42+
return 'true';
43+
}
44+
45+
return (string) $value;
46+
}
47+
48+
private function formatValues(array $values): string
49+
{
50+
foreach ($values as $key => $value) {
51+
$values[$key] = $this->formatValue($value);
52+
}
53+
54+
return \sprintf('[%s]', \implode(', ', $values));
55+
}
56+
}

src/Exception/ValidationException.php

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
44

5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\Util\FormatValueTrait;
6+
57
class ValidationException extends \Exception
68
{
9+
use FormatValueTrait;
10+
711
public function __construct(string $message, array $parameters = [])
812
{
913
$message = $this->formatMessage($message, $parameters);
@@ -19,53 +23,5 @@ private function formatMessage(string $message, array $parameters = []): string
1923
return $message;
2024
}
2125

22-
private function formatValue(mixed $value): string
23-
{
24-
if ($value instanceof \DateTimeInterface) {
25-
return $value->format('Y-m-d H:i:s');
26-
}
27-
28-
if (\is_object($value)) {
29-
if ($value instanceof \Stringable) {
30-
return $value->__toString();
31-
}
32-
33-
return 'object';
34-
}
35-
36-
if (\is_array($value)) {
37-
return $this->formatValues($value);
38-
}
39-
40-
if (\is_string($value)) {
41-
return \sprintf('"%s"', $value);
42-
}
43-
44-
if (\is_resource($value)) {
45-
return 'resource';
46-
}
4726

48-
if ($value === null) {
49-
return 'null';
50-
}
51-
52-
if ($value === false) {
53-
return 'false';
54-
}
55-
56-
if ($value === true) {
57-
return 'true';
58-
}
59-
60-
return (string) $value;
61-
}
62-
63-
private function formatValues(array $values): string
64-
{
65-
foreach ($values as $key => $value) {
66-
$values[$key] = $this->formatValue($value);
67-
}
68-
69-
return \implode(', ', $values);
70-
}
7127
}

src/Rule/NotBlank.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,23 @@ class NotBlank extends AbstractRule implements RuleInterface
88
{
99
public function __construct(private ?string $message = null)
1010
{
11-
$this->message ??= 'The {{ name }} value should not be blank.';
11+
$this->message ??= 'The "{{ name }}" value should not be blank, "{{ value }}" given.';
1212
}
1313

1414
/**
1515
* @throws NotBlankException
1616
*/
17-
public function validate(mixed $input): void
17+
public function validate(mixed $value): void
1818
{
19-
// Strip whitespace in case of a string
20-
if (\is_string($input)) {
21-
$input = trim($input);
22-
}
19+
// Keep value unchanged for parameters
20+
$input = $value;
2321

2422
// Do not allow null, false, [] and ''
2523
if ($input === false || (empty($input) && $input != '0')) {
2624
throw new NotBlankException(
2725
message: $this->message,
2826
parameters: [
29-
'input' => $input,
27+
'value' => $value,
3028
'name' => $this->getName()
3129
]
3230
);

src/Rule/RuleInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface RuleInterface
99
/**
1010
* @throws ValidationException
1111
*/
12-
public function validate(mixed $input): void;
12+
public function validate(mixed $value): void;
1313

1414
public function getName(): string;
1515

src/Validator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ public function __call(string $ruleName, array $arguments = []): self
4545
/**
4646
* @throws ValidationException
4747
*/
48-
public function assert(mixed $input, string $name): void
48+
public function assert(mixed $value, string $name): void
4949
{
5050
foreach ($this->getRules() as $rule) {
51-
$rule->setName($name)->validate($input);
51+
$rule->setName($name)->validate($value);
5252
}
5353
}
5454

55-
public function validate(mixed $input): bool
55+
public function validate(mixed $value): bool
5656
{
5757
try {
58-
$this->assert($input, 'null');
58+
$this->assert($value, 'null');
5959
}
6060
catch (ValidationException) {
6161
return false;

tests/NotBlankTest.php

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,68 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\Util\FormatValueTrait;
78
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
89

910
class NotBlankTest extends AbstractTest
1011
{
11-
#[DataProvider('provideInvalidInputData')]
12-
public function testNotBlankInvalidInput(mixed $input)
12+
use FormatValueTrait;
13+
14+
#[DataProvider('provideInvalidValueData')]
15+
public function testNotBlankInvalidValue(mixed $value)
1316
{
1417
$validator = Validator::notBlank();
1518

16-
$this->assertFalse($validator->validate($input));
19+
$this->assertFalse($validator->validate($value));
1720

1821
$this->expectException(NotBlankException::class);
19-
$this->expectExceptionMessage('The "test" value should not be blank.');
20-
$validator->assert($input, 'test');
22+
$this->expectExceptionMessage(
23+
\sprintf('The "test" value should not be blank, "%s" given.', $this->formatValue($value))
24+
);
25+
$validator->assert($value, 'test');
2126
}
2227

23-
public static function provideInvalidInputData(): \Generator
28+
public static function provideInvalidValueData(): \Generator
2429
{
2530
yield 'null' => [null];
2631
yield 'false' => [false];
27-
yield 'blank array' => [[]];
2832
yield 'blank string' => [''];
29-
yield 'whitespace' => [' '];
33+
yield 'blank array' => [[]];
3034
}
3135

32-
#[DataProvider('provideValidInputData')]
33-
public function testNotBlankValidInput(mixed $input)
36+
#[DataProvider('provideValidValueData')]
37+
public function testNotBlankValidValue(mixed $value)
3438
{
3539
$validator = Validator::notBlank();
3640

37-
$this->assertTrue($validator->validate($input));
41+
$this->assertTrue($validator->validate($value));
3842

39-
Validator::notBlank()->assert($input, 'test');
43+
$validator->assert($value, 'test');
4044
}
4145

42-
public static function provideValidInputData(): \Generator
46+
public static function provideValidValueData(): \Generator
4347
{
4448
yield 'true' => [true];
45-
yield 'zero number' => [0];
46-
yield 'zero string' => ['0'];
47-
yield 'array' => [[0]];
49+
4850
yield 'string' => ['string'];
51+
yield 'whitespace string' => [' '];
52+
yield 'zero string' => ['0'];
53+
54+
yield 'array' => [['string']];
55+
yield 'blank string array' => [['']];
56+
yield 'whitespace array' => [[' ']];
57+
yield 'zero array' => [[0]];
58+
59+
yield 'number' => [10];
60+
yield 'zero number' => [0];
4961
}
5062

5163
public function testNotBlankExceptionMessageParameters()
5264
{
53-
$this->expectExceptionMessage('The "test" value false is invalid. Must not be blank.');
65+
$this->expectExceptionMessage('The "test" value "false" is invalid. Must not be blank.');
5466

55-
Validator::notBlank(
56-
message: 'The {{ name }} value {{ input }} is invalid. Must not be blank.'
57-
)->assert(false, 'test');
67+
Validator
68+
::notBlank(message: 'The "{{ name }}" value "{{ value }}" is invalid. Must not be blank.')
69+
->assert(false, 'test');
5870
}
5971
}

0 commit comments

Comments
 (0)