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

Commit 10933d0

Browse files
committed
chore: spread NotBlank options
1 parent 9aabedf commit 10933d0

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

src/Rule/NotBlank.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
6-
use Symfony\Component\OptionsResolver\OptionsResolver;
76

87
class NotBlank extends AbstractRule implements RuleInterface
98
{
10-
private array $options;
9+
// Using array to bypass unallowed callable type in properties
10+
private ?array $normalizer = null;
1111

12-
public function __construct(array $options = [])
13-
{
14-
$resolver = new OptionsResolver();
15-
16-
$resolver->setDefaults([
17-
'normalizer' => null,
18-
'message' => 'The "{{ name }}" value should not be blank, "{{ value }}" given.'
19-
]);
12+
private ?string $message;
2013

21-
$resolver->setAllowedTypes('normalizer', ['null', 'callable']);
22-
$resolver->setAllowedTypes('message', 'string');
23-
24-
$this->options = $resolver->resolve($options);
14+
public function __construct(
15+
?callable $normalizer = null,
16+
?string $message = null
17+
)
18+
{
19+
$this->normalizer['callable'] = $normalizer;
20+
$this->message = $message ?? 'The "{{ name }}" value should not be blank, "{{ value }}" given.';
2521
}
2622

2723
/**
@@ -33,14 +29,14 @@ public function assert(mixed $value, string $name): void
3329
$input = $value;
3430

3531
// Call normalizer if provided
36-
if ($this->options['normalizer'] !== null) {
37-
$input = ($this->options['normalizer'])($input);
32+
if ($this->normalizer['callable'] !== null) {
33+
$input = ($this->normalizer['callable'])($input);
3834
}
3935

4036
// Do not allow null, false, [] and ''
4137
if ($input === false || (empty($input) && $input != '0')) {
4238
throw new NotBlankException(
43-
message: $this->options['message'],
39+
message: $this->message,
4440
parameters: [
4541
'value' => $value,
4642
'name' => $name

tests/NotBlankTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public static function provideRuleFailureConditionData(): \Generator
2424
yield 'blank string' => [new NotBlank(), '', $exception, $exceptionMessage];
2525
yield 'blank array' => [new NotBlank(), [], $exception, $exceptionMessage];
2626

27-
yield 'normalizer whitespace' => [new NotBlank(['normalizer' => 'trim']), ' ', $exception, $exceptionMessage];
27+
yield 'normalizer whitespace' => [new NotBlank(normalizer: 'trim'), ' ', $exception, $exceptionMessage];
28+
yield 'normalizer whitespace function' => [new NotBlank(normalizer: fn($value) => trim($value)), ' ', $exception, $exceptionMessage];
2829
}
2930

3031
public static function provideRuleSuccessConditionData(): \Generator
@@ -47,11 +48,9 @@ public static function provideRuleSuccessConditionData(): \Generator
4748
public static function provideRuleMessageOptionData(): \Generator
4849
{
4950
yield 'message' => [
50-
new NotBlank(
51-
options: [
52-
'message' => 'The "{{ name }}" value "{{ value }}" is not blank.'
53-
]
54-
), '', 'The "test" value "" is not blank.'
51+
new NotBlank(message: 'The "{{ name }}" value "{{ value }}" is not blank.'),
52+
'',
53+
'The "test" value "" is not blank.'
5554
];
5655
}
5756
}

0 commit comments

Comments
 (0)