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

Commit 23d3a2a

Browse files
authored
Merge pull request #4 from programmatordev/YAPV-11-create-lessthan-rule
Create LessThan rule
2 parents fd10fad + 599fc42 commit 23d3a2a

12 files changed

Lines changed: 156 additions & 24 deletions

composer.json

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

src/ChainedValidatorInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public function validate(mixed $value): bool;
1515

1616
// --- Rules ---
1717

18-
public function notBlank(string $message = null): ChainedValidatorInterface;
18+
public function notBlank(array $options = []): ChainedValidatorInterface;
1919

20-
public function greaterThan(mixed $constraint, string $message = null): ChainedValidatorInterface;
20+
public function greaterThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
21+
22+
public function lessThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
2123
}
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 LessThanException extends ValidationException {}

src/Factory.php renamed to src/Factory/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace ProgrammatorDev\YetAnotherPhpValidator;
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Factory;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RuleNotFoundException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;

src/Rule/GreaterThan.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertComparableTrait;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
78

89
class GreaterThan extends AbstractRule implements RuleInterface
910
{
1011
use AssertComparableTrait;
1112

12-
private string $message;
13+
private array $options;
1314

14-
public function __construct(
15-
private readonly mixed $constraint,
16-
string $message = null
17-
)
15+
public function __construct(private readonly mixed $constraint, array $options = [])
1816
{
19-
$this->message = $message ?? 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
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);
2024
}
2125

2226
/**
@@ -29,7 +33,7 @@ public function assert(mixed $value, string $name): void
2933

3034
if (!($value > $this->constraint)) {
3135
throw new GreaterThanException(
32-
message: $this->message,
36+
message: $this->options['message'],
3337
parameters: [
3438
'name' => $name,
3539
'constraint' => $this->constraint,

src/Rule/LessThan.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertComparableTrait;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
9+
class LessThan extends AbstractRule implements RuleInterface
10+
{
11+
use AssertComparableTrait;
12+
13+
private array $options;
14+
15+
public function __construct(private readonly mixed $constraint, array $options = [])
16+
{
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);
24+
}
25+
26+
/**
27+
* @throws LessThanException
28+
*/
29+
public function assert(mixed $value, string $name): void
30+
{
31+
// Assert if constraint and value can be compared
32+
$this->assertComparable($this->constraint, $value, LessThanException::class);
33+
34+
if (!($value < $this->constraint)) {
35+
throw new LessThanException(
36+
message: $this->options['message'],
37+
parameters: [
38+
'name' => $name,
39+
'constraint' => $this->constraint,
40+
'value' => $value
41+
]
42+
);
43+
}
44+
}
45+
}

src/Rule/NotBlank.php

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

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\NotBlankException;
6+
use Symfony\Component\OptionsResolver\OptionsResolver;
67

78
class NotBlank extends AbstractRule implements RuleInterface
89
{
9-
private string $message;
10+
private array $options;
1011

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

1623
/**
@@ -21,7 +28,7 @@ public function assert(mixed $value, string $name): void
2128
// Do not allow null, false, [] and ''
2229
if ($value === false || (empty($value) && $value != '0')) {
2330
throw new NotBlankException(
24-
message: $this->message,
31+
message: $this->options['message'],
2532
parameters: [
2633
'name' => $name,
2734
'value' => $value

src/StaticValidatorInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
interface StaticValidatorInterface
66
{
7-
public static function notBlank(string $message = null): ChainedValidatorInterface;
7+
public static function notBlank(array $options = []): ChainedValidatorInterface;
88

9-
public static function greaterThan(mixed $constraint, string $message = null): ChainedValidatorInterface;
9+
public static function greaterThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
10+
11+
public static function lessThan(mixed $constraint, array $options = []): ChainedValidatorInterface;
1012
}

src/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RuleNotFoundException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Factory\Factory;
78
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
89

910
/**

tests/GreaterThanTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public static function provideRuleSuccessConditionData(): \Generator
5252
public static function provideRuleCustomMessageData(): \Generator
5353
{
5454
yield 'message' => [
55-
new GreaterThan(
56-
constraint: 10,
57-
message: 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
58-
), 1, 'The "test" value "1" is not greater than "10".'
55+
new GreaterThan(10, [
56+
'message' => 'The "{{ name }}" value "{{ value }}" is not greater than "{{ constraint }}".'
57+
]),
58+
1,
59+
'The "test" value "1" is not greater than "10".'
5960
];
6061
}
6162
}

0 commit comments

Comments
 (0)