Skip to content

Commit 331b3a1

Browse files
committed
Refactor RegexRule properties to protected visibility and introduce StringRule for string validation with length constraints and corresponding tests
1 parent 5c8e7b5 commit 331b3a1

File tree

3 files changed

+177
-3
lines changed

3 files changed

+177
-3
lines changed

src/Validation/Rules/RegexRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
class RegexRule implements ValidationRule
88
{
9-
public string $pattern;
9+
protected string $pattern;
1010

1111
/** @var 0 | 256 | 512 | 768 */
12-
public int $flags = 0;
12+
protected int $flags = 0;
1313

14-
public int $offset = 0;
14+
protected int $offset = 0;
1515

1616
public string $name {
1717
get {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Validation\Rules;
4+
5+
use Nuxtifyts\PhpDto\Exceptions\ValidationRuleException;
6+
use Nuxtifyts\PhpDto\Support\Arr;
7+
use Override;
8+
use Nuxtifyts\PhpDto\Validation\Rules\RegexRule;
9+
10+
class StringRule extends RegexRule
11+
{
12+
public string $name {
13+
get {
14+
return 'string';
15+
}
16+
}
17+
18+
/**
19+
* @param ?array<string, mixed> $parameters
20+
*
21+
* @throws ValidationRuleException
22+
*/
23+
#[Override]
24+
public static function make(?array $parameters = null): self
25+
{
26+
$instance = new self();
27+
28+
if (
29+
(!is_null($minLen = Arr::getIntegerOrNull($parameters ?? [], 'minLen'))
30+
&& $minLen < 0)
31+
|| (!is_null($maxLen = Arr::getIntegerOrNull($parameters ?? [], 'maxLen'))
32+
&& $maxLen < $minLen)
33+
) {
34+
throw ValidationRuleException::invalidParameters();
35+
}
36+
37+
$lengthPattern = '';
38+
if ($minLen !== null) {
39+
$lengthPattern .= '.{' . $minLen . ',';
40+
} else {
41+
$lengthPattern .= '.{0,';
42+
}
43+
44+
if ($maxLen !== null) {
45+
$lengthPattern .= $maxLen . '}';
46+
} else {
47+
$lengthPattern .= '}';
48+
}
49+
50+
$instance->pattern = '/^' . $lengthPattern . '$/u';
51+
52+
return $instance;
53+
}
54+
55+
#[Override]
56+
public function validationMessage(): string
57+
{
58+
return 'The :attribute field must be a valid string.';
59+
}
60+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Validation;
4+
5+
use Throwable;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use Nuxtifyts\PhpDto\Validation\Rules\StringRule;
9+
use Nuxtifyts\PhpDto\Exceptions\ValidationRuleException;
10+
use Nuxtifyts\PhpDto\Tests\Unit\Validation\ValidationRuleTestCase;
11+
12+
#[CoversClass(StringRule::class)]
13+
#[CoversClass(ValidationRuleException::class)]
14+
final class StringRuleTest extends ValidationRuleTestCase
15+
{
16+
/**
17+
* @throws Throwable
18+
*/
19+
#[Test]
20+
public function validate_validation_message(): void
21+
{
22+
$rule = StringRule::make();
23+
24+
self::assertEquals(
25+
'The :attribute field must be a valid string.',
26+
$rule->validationMessage()
27+
);
28+
}
29+
30+
/**
31+
* @return array<string, array{
32+
* validationRuleClassString: class-string<StringRule>,
33+
* makeParams: ?array<string, mixed>,
34+
* expectedMakeException: ?class-string<ValidationRuleException>,
35+
* valueToBeEvaluated: mixed,
36+
* expectedResult: bool
37+
* }>
38+
*/
39+
public static function data_provider(): array
40+
{
41+
return [
42+
'Will evaluate false when value is not a string' => [
43+
'validationRuleClassString' => StringRule::class,
44+
'makeParams' => null,
45+
'expectedMakeException' => null,
46+
'valueToBeEvaluated' => 123,
47+
'expectedResult' => false
48+
],
49+
'Will evaluate true when a string value is provided' => [
50+
'validationRuleClassString' => StringRule::class,
51+
'makeParams' => null,
52+
'expectedMakeException' => null,
53+
'valueToBeEvaluated' => 'test',
54+
'expectedResult' => true
55+
],
56+
'Will evaluate false when a string value is provided but minLen is greater than the length of the string' => [
57+
'validationRuleClassString' => StringRule::class,
58+
'makeParams' => ['minLen' => 5],
59+
'expectedMakeException' => null,
60+
'valueToBeEvaluated' => 'test',
61+
'expectedResult' => false
62+
],
63+
'Will evaluate true when a string value is provided and minLen is less than the length of the string' => [
64+
'validationRuleClassString' => StringRule::class,
65+
'makeParams' => ['minLen' => 3],
66+
'expectedMakeException' => null,
67+
'valueToBeEvaluated' => 'test',
68+
'expectedResult' => true
69+
],
70+
'Will evaluate false when a string value is provided but maxLen is less than the length of the string' => [
71+
'validationRuleClassString' => StringRule::class,
72+
'makeParams' => ['maxLen' => 3],
73+
'expectedMakeException' => null,
74+
'valueToBeEvaluated' => 'test',
75+
'expectedResult' => false
76+
],
77+
'Will evaluate true when a string value is provided and maxLen is greater than the length of the string' => [
78+
'validationRuleClassString' => StringRule::class,
79+
'makeParams' => ['maxLen' => 5],
80+
'expectedMakeException' => null,
81+
'valueToBeEvaluated' => 'test',
82+
'expectedResult' => true
83+
],
84+
'Will evaluate false when a string value is provided but minLen is greater than the length of the string and maxLen is less than the length of the string' => [
85+
'validationRuleClassString' => StringRule::class,
86+
'makeParams' => ['minLen' => 5, 'maxLen' => 10],
87+
'expectedMakeException' => null,
88+
'valueToBeEvaluated' => 'test',
89+
'expectedResult' => false
90+
],
91+
'Will evaluate true when a string value is provided and minLen is less than the length of the string and maxLen is greater than the length of the string' => [
92+
'validationRuleClassString' => StringRule::class,
93+
'makeParams' => ['minLen' => 3, 'maxLen' => 5],
94+
'expectedMakeException' => null,
95+
'valueToBeEvaluated' => 'test',
96+
'expectedResult' => true
97+
],
98+
'Will throw an exception if minLen is less than 0' => [
99+
'validationRuleClassString' => StringRule::class,
100+
'makeParams' => ['minLen' => -1],
101+
'expectedMakeException' => ValidationRuleException::class,
102+
'valueToBeEvaluated' => 'test',
103+
'expectedResult' => false
104+
],
105+
'Will throw an exception if maxLen is less than minLen' => [
106+
'validationRuleClassString' => StringRule::class,
107+
'makeParams' => ['minLen' => 5, 'maxLen' => 3],
108+
'expectedMakeException' => ValidationRuleException::class,
109+
'valueToBeEvaluated' => 'test',
110+
'expectedResult' => false
111+
],
112+
];
113+
}
114+
}

0 commit comments

Comments
 (0)