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

Commit b312608

Browse files
authored
Merge pull request #19 from programmatordev/YAPV-41-revert-from-using-an-array-of-options-and-spread-them-instead
Revert from using an array of options and spread them instead
2 parents 9aabedf + aae3642 commit b312608

18 files changed

+112
-163
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=8.1",
16-
"symfony/options-resolver": "^6.3"
15+
"php": ">=8.1"
1716
},
1817
"require-dev": {
1918
"phpunit/phpunit": "^10.0",

src/Rule/AbstractComparisonRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function assert(mixed $value, string $name): void
2020

2121
if (!$this->compareValues($value, $this->constraint)) {
2222
throw new ($this->getException())(
23-
message: $this->options['message'],
23+
message: $this->message,
2424
parameters: [
2525
'value' => $value,
2626
'name' => $name,

src/Rule/All.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,22 @@
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
88
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ValidatableTrait;
9-
use Symfony\Component\OptionsResolver\OptionsResolver;
109

1110
class All extends AbstractRule implements RuleInterface
1211
{
1312
use ValidatableTrait;
1413

15-
private array $options;
14+
private string $message;
1615

1716
/**
1817
* @param RuleInterface[] $constraints
1918
*/
2019
public function __construct(
2120
private readonly array $constraints,
22-
array $options = []
21+
?string $message = null
2322
)
2423
{
25-
$resolver = new OptionsResolver();
26-
27-
$resolver->setDefaults(['message' => 'At "{{ key }}": {{ message }}']);
28-
29-
$resolver->setAllowedTypes('message', 'string');
30-
31-
$this->options = $resolver->resolve($options);
24+
$this->message = $message ?? 'At "{{ key }}": {{ message }}';
3225
}
3326

3427
public function assert(mixed $value, string $name): void
@@ -45,23 +38,23 @@ public function assert(mixed $value, string $name): void
4538
);
4639
}
4740

48-
foreach ($value as $key => $input) {
49-
foreach ($this->constraints as $constraint) {
50-
try {
41+
try {
42+
foreach ($value as $key => $input) {
43+
foreach ($this->constraints as $constraint) {
5144
$constraint->assert($input, $name);
5245
}
53-
catch (ValidationException $exception) {
54-
throw new AllException(
55-
message: $this->options['message'],
56-
parameters: [
57-
'value' => $value,
58-
'name' => $name,
59-
'key' => $key,
60-
'message' => $exception->getMessage()
61-
]
62-
);
63-
}
6446
}
6547
}
48+
catch (ValidationException $exception) {
49+
throw new AllException(
50+
message: $this->message,
51+
parameters: [
52+
'value' => $value,
53+
'name' => $name,
54+
'key' => $key,
55+
'message' => $exception->getMessage()
56+
]
57+
);
58+
}
6659
}
6760
}

src/Rule/Choice.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,32 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ChoiceException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7-
use Symfony\Component\OptionsResolver\OptionsResolver;
87

98
class Choice extends AbstractRule implements RuleInterface
109
{
11-
private array $options;
10+
private string $message;
11+
12+
private string $multipleMessage;
13+
14+
private string $minMessage;
15+
16+
private string $maxMessage;
1217

1318
public function __construct(
1419
private readonly array $constraints,
1520
private readonly bool $multiple = false,
1621
private readonly ?int $minConstraint = null,
1722
private readonly ?int $maxConstraint = null,
18-
array $options = []
23+
?string $message = null,
24+
?string $multipleMessage = null,
25+
?string $minMessage = null,
26+
?string $maxMessage = null
1927
)
2028
{
21-
$resolver = new OptionsResolver();
22-
23-
$resolver->setDefaults([
24-
'message' => 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
25-
'multipleMessage' => 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
26-
'minMessage' => 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
27-
'maxMessage' => 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
28-
]);
29-
30-
$resolver->setAllowedTypes('message', 'string');
31-
$resolver->setAllowedTypes('multipleMessage', 'string');
32-
$resolver->setAllowedTypes('minMessage', 'string');
33-
$resolver->setAllowedTypes('maxMessage', 'string');
34-
35-
$this->options = $resolver->resolve($options);
29+
$this->message = $message ?? 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".';
30+
$this->multipleMessage = $multipleMessage ?? 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".';
31+
$this->minMessage = $minMessage ?? 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.';
32+
$this->maxMessage = $maxMessage ?? 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.';
3633
}
3734

3835
public function assert(mixed $value, string $name): void
@@ -58,7 +55,7 @@ public function assert(mixed $value, string $name): void
5855
foreach ($value as $input) {
5956
if (!\in_array($input, $this->constraints, true)) {
6057
throw new ChoiceException(
61-
message: $this->options['multipleMessage'],
58+
message: $this->multipleMessage,
6259
parameters: [
6360
'value' => $value,
6461
'name' => $name,
@@ -72,7 +69,7 @@ public function assert(mixed $value, string $name): void
7269

7370
if ($this->minConstraint !== null && $numValues < $this->minConstraint) {
7471
throw new ChoiceException(
75-
message: $this->options['minMessage'],
72+
message: $this->minMessage,
7673
parameters: [
7774
'value' => $value,
7875
'numValues' => $numValues,
@@ -86,7 +83,7 @@ public function assert(mixed $value, string $name): void
8683

8784
if ($this->maxConstraint !== null && $numValues > $this->maxConstraint) {
8885
throw new ChoiceException(
89-
message: $this->options['maxMessage'],
86+
message: $this->maxMessage,
9087
parameters: [
9188
'value' => $value,
9289
'numValues' => $numValues,
@@ -100,7 +97,7 @@ public function assert(mixed $value, string $name): void
10097
}
10198
else if (!\in_array($value, $this->constraints, true)) {
10299
throw new ChoiceException(
103-
message: $this->options['message'],
100+
message: $this->message,
104101
parameters: [
105102
'value' => $value,
106103
'name' => $name,

src/Rule/GreaterThan.php

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

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

87
class GreaterThan extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

src/Rule/GreaterThanOrEqual.php

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

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

87
class GreaterThanOrEqual extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

src/Rule/LessThan.php

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

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

87
class LessThan extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

src/Rule/LessThanOrEqual.php

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

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

87
class LessThanOrEqual extends AbstractComparisonRule implements RuleInterface
98
{
10-
protected array $options;
9+
protected string $message;
1110

1211
public function __construct(
1312
protected readonly mixed $constraint,
14-
array $options = []
13+
?string $message = null
1514
)
1615
{
17-
$resolver = new OptionsResolver();
18-
19-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.']);
20-
21-
$resolver->setAllowedTypes('message', 'string');
22-
23-
$this->options = $resolver->resolve($options);
16+
$this->message = $message ?? 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.';
2417
}
2518

2619
protected function compareValues(mixed $value1, mixed $value2): bool

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;
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

src/Rule/Range.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,20 @@
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
88
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
99
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
10-
use Symfony\Component\OptionsResolver\OptionsResolver;
1110

1211
class Range extends AbstractRule implements RuleInterface
1312
{
1413
use ComparableTrait;
1514

16-
private array $options;
15+
private string $message;
1716

1817
public function __construct(
1918
private readonly mixed $minConstraint,
2019
private readonly mixed $maxConstraint,
21-
array $options = []
20+
?string $message = null
2221
)
2322
{
24-
$resolver = new OptionsResolver();
25-
26-
$resolver->setDefaults(['message' => 'The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.']);
27-
28-
$resolver->setAllowedTypes('message', 'string');
29-
30-
$this->options = $resolver->resolve($options);
23+
$this->message = $message ?? 'The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.';
3124
}
3225

3326
public function assert(mixed $value, string $name): void
@@ -54,7 +47,7 @@ public function assert(mixed $value, string $name): void
5447
->validate($value)
5548
) {
5649
throw new RangeException(
57-
message: $this->options['message'],
50+
message: $this->message,
5851
parameters: [
5952
'value' => $value,
6053
'name' => $name,

0 commit comments

Comments
 (0)