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

Commit c46e239

Browse files
committed
feat: added Rule for custom rules
1 parent a9093fc commit c46e239

File tree

6 files changed

+87
-24
lines changed

6 files changed

+87
-24
lines changed

src/ChainedValidatorInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
67

78
interface ChainedValidatorInterface
89
{
@@ -30,4 +31,6 @@ public function lessThanOrEqual(mixed $constraint, array $options = []): Chained
3031
public function notBlank(array $options = []): ChainedValidatorInterface;
3132

3233
public function range(mixed $minConstraint, mixed $maxConstraint, array $options = []): ChainedValidatorInterface;
34+
35+
public function rule(RuleInterface $constraint): ChainedValidatorInterface;
3336
}

src/Rule/Rule.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
class Rule extends AbstractRule implements RuleInterface
6+
{
7+
public function __construct(private readonly RuleInterface $constraint) {}
8+
9+
public function assert(mixed $value, string $name): void
10+
{
11+
$this->constraint->assert($value, $name);
12+
}
13+
}

src/StaticValidatorInterface.php

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

33
namespace ProgrammatorDev\YetAnotherPhpValidator;
44

5+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
6+
57
interface StaticValidatorInterface
68
{
79
public static function all(array $constraints, array $options = null): ChainedValidatorInterface;
@@ -19,4 +21,6 @@ public static function lessThanOrEqual(mixed $constraint, array $options = []):
1921
public static function notBlank(array $options = []): ChainedValidatorInterface;
2022

2123
public static function range(mixed $minConstraint, mixed $maxConstraint, array $options = []): ChainedValidatorInterface;
24+
25+
public static function rule(RuleInterface $constraint): ChainedValidatorInterface;
2226
}

src/Test/Dummy/DummyRule.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Test/Fixture/DummyRule.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Test\Fixture;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\AbstractRule;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
9+
10+
class DummyRule extends AbstractRule implements RuleInterface
11+
{
12+
public function assert(mixed $value, string $name): void
13+
{
14+
if (!\is_bool($value)) {
15+
throw new UnexpectedValueException('Dummy unexpected value.');
16+
}
17+
18+
if ($value === false) {
19+
throw new ValidationException('Dummy exception.');
20+
}
21+
}
22+
}

tests/RuleTest.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\Test;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Rule;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Fixture\DummyRule;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleFailureConditionTrait;
9+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleSuccessConditionTrait;
10+
use ProgrammatorDev\YetAnotherPhpValidator\Test\Util\TestRuleUnexpectedValueTrait;
11+
12+
class RuleTest extends AbstractTest
13+
{
14+
use TestRuleUnexpectedValueTrait;
15+
use TestRuleFailureConditionTrait;
16+
use TestRuleSuccessConditionTrait;
17+
18+
public static function provideRuleUnexpectedValueData(): \Generator
19+
{
20+
yield 'invalid value type' => [
21+
new Rule(new DummyRule()),
22+
'invalid',
23+
'/Dummy unexpected value./'
24+
];
25+
}
26+
27+
public static function provideRuleFailureConditionData(): \Generator
28+
{
29+
yield 'invalid value' => [
30+
new Rule(new DummyRule()),
31+
false,
32+
ValidationException::class,
33+
'/Dummy exception./'
34+
];
35+
}
36+
37+
public static function provideRuleSuccessConditionData(): \Generator
38+
{
39+
yield 'valid value' => [
40+
new Rule(new DummyRule()),
41+
true
42+
];
43+
}
44+
45+
}

0 commit comments

Comments
 (0)