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

Commit 7161be0

Browse files
authored
Merge pull request #5 from programmatordev/YAPV-15-add-notblank-normalizer
Add NotBlank normalizer
2 parents 23d3a2a + e7a7242 commit 7161be0

5 files changed

Lines changed: 29 additions & 15 deletions

File tree

src/Rule/NotBlank.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ public function __construct(array $options = [])
1313
{
1414
$resolver = new OptionsResolver();
1515

16-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should not be blank, "{{ value }}" given.']);
16+
$resolver->setDefaults([
17+
'message' => 'The "{{ name }}" value should not be blank, "{{ value }}" given.',
18+
'normalizer' => null
19+
]);
1720

1821
$resolver->setAllowedTypes('message', 'string');
22+
$resolver->setAllowedTypes('normalizer', ['null', 'string', 'callable']);
1923

2024
$this->options = $resolver->resolve($options);
2125
}
@@ -25,8 +29,16 @@ public function __construct(array $options = [])
2529
*/
2630
public function assert(mixed $value, string $name): void
2731
{
32+
// Keep original value for parameter
33+
$input = $value;
34+
35+
// Call normalizer if provided
36+
if ($this->options['normalizer'] !== null) {
37+
$input = ($this->options['normalizer'])($input);
38+
}
39+
2840
// Do not allow null, false, [] and ''
29-
if ($value === false || (empty($value) && $value != '0')) {
41+
if ($input === false || (empty($input) && $input != '0')) {
3042
throw new NotBlankException(
3143
message: $this->options['message'],
3244
parameters: [

src/Test/Util/TestRuleCustomMessageTrait.php renamed to src/Test/Util/TestRuleMessageOptionTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use PHPUnit\Framework\Attributes\DataProvider;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
77

8-
trait TestRuleCustomMessageTrait
8+
trait TestRuleMessageOptionTrait
99
{
10-
public static abstract function provideRuleCustomMessageData(): \Generator;
10+
public static abstract function provideRuleMessageOptionData(): \Generator;
1111

12-
#[DataProvider('provideRuleCustomMessageData')]
13-
public function testRuleCustomMessage(RuleInterface $rule, mixed $value, string $exceptionMessage): void
12+
#[DataProvider('provideRuleMessageOptionData')]
13+
public function testRuleMessageOption(RuleInterface $rule, mixed $value, string $exceptionMessage): void
1414
{
1515
$this->expectExceptionMessage($exceptionMessage);
1616
$rule->assert($value, 'test');

tests/GreaterThanTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleCustomMessageTrait;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
88
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
99
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
1010

1111
class GreaterThanTest extends AbstractTest
1212
{
1313
use TestRuleFailureConditionTrait;
1414
use TestRuleSuccessConditionTrait;
15-
use TestRuleCustomMessageTrait;
15+
use TestRuleMessageOptionTrait;
1616

1717
public static function provideRuleFailureConditionData(): \Generator
1818
{
@@ -49,7 +49,7 @@ public static function provideRuleSuccessConditionData(): \Generator
4949
yield 'string' => [new GreaterThan('a'), 'z'];
5050
}
5151

52-
public static function provideRuleCustomMessageData(): \Generator
52+
public static function provideRuleMessageOptionData(): \Generator
5353
{
5454
yield 'message' => [
5555
new GreaterThan(10, [

tests/LessThanTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\LessThan;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleCustomMessageTrait;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
88
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
99
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
1010

1111
class LessThanTest extends AbstractTest
1212
{
1313
use TestRuleFailureConditionTrait;
1414
use TestRuleSuccessConditionTrait;
15-
use TestRuleCustomMessageTrait;
15+
use TestRuleMessageOptionTrait;
1616

1717
public static function provideRuleFailureConditionData(): \Generator
1818
{
@@ -49,7 +49,7 @@ public static function provideRuleSuccessConditionData(): \Generator
4949
yield 'string' => [new LessThan('z'), 'a'];
5050
}
5151

52-
public static function provideRuleCustomMessageData(): \Generator
52+
public static function provideRuleMessageOptionData(): \Generator
5353
{
5454
yield 'message' => [
5555
new LessThan(10, [

tests/NotBlankTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\NotBlank;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleCustomMessageTrait;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleMessageOptionTrait;
88
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
99
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
1010

1111
class NotBlankTest extends AbstractTest
1212
{
1313
use TestRuleFailureConditionTrait;
1414
use TestRuleSuccessConditionTrait;
15-
use TestRuleCustomMessageTrait;
15+
use TestRuleMessageOptionTrait;
1616

1717
public static function provideRuleFailureConditionData(): \Generator
1818
{
@@ -23,6 +23,8 @@ public static function provideRuleFailureConditionData(): \Generator
2323
yield 'false' => [new NotBlank(), false, $exception, $exceptionMessage];
2424
yield 'blank string' => [new NotBlank(), '', $exception, $exceptionMessage];
2525
yield 'blank array' => [new NotBlank(), [], $exception, $exceptionMessage];
26+
27+
yield 'normalizer whitespace' => [new NotBlank(['normalizer' => 'trim']), ' ', $exception, $exceptionMessage];
2628
}
2729

2830
public static function provideRuleSuccessConditionData(): \Generator
@@ -42,7 +44,7 @@ public static function provideRuleSuccessConditionData(): \Generator
4244
yield 'zero number' => [new NotBlank(), 0];
4345
}
4446

45-
public static function provideRuleCustomMessageData(): \Generator
47+
public static function provideRuleMessageOptionData(): \Generator
4648
{
4749
yield 'message' => [
4850
new NotBlank([

0 commit comments

Comments
 (0)