Skip to content

Commit 00ae95b

Browse files
committed
Enhance StringRule to support alpha numeric validation and improve RegexRule to accept numeric values
1 parent 8d33d3b commit 00ae95b

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/Validation/Rules/RegexRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class RegexRule implements ValidationRule
2121

2222
public function evaluate(mixed $value): bool
2323
{
24-
return is_string($value)
24+
return (is_string($value) || is_numeric($value))
2525
&& preg_match(
2626
pattern: $this->pattern,
27-
subject: $value,
27+
subject: (string) $value,
2828
flags: $this->flags,
2929
offset: $this->offset
3030
);

src/Validation/Rules/StringRule.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ class StringRule extends RegexRule
1111
{
1212
use Concerns\MinMaxValues;
1313

14+
protected const TYPE_STRING = 'string';
15+
protected const TYPE_ALPHA = 'alpha';
16+
17+
/** @var list<string> */
18+
protected const TYPES = [
19+
self::TYPE_STRING,
20+
self::TYPE_ALPHA,
21+
];
22+
1423
public string $name {
1524
get {
1625
return 'string';
@@ -27,12 +36,24 @@ public static function make(?array $parameters = null): self
2736
{
2837
$instance = new self();
2938

39+
$strType = Arr::getString($parameters ?? [], 'type', self::TYPE_STRING);
40+
41+
if (!in_array($strType, self::TYPES)) {
42+
throw ValidationRuleException::invalidParameters();
43+
}
44+
45+
/** @var 'string' | 'alpha' $strType */
46+
$strPattern = match ($strType) {
47+
self::TYPE_STRING => '[a-zA-Z]',
48+
self::TYPE_ALPHA => '[a-zA-Z0-9]'
49+
};
50+
3051
[$minLen, $maxLen] = self::getMinMaxValues($parameters, 'minLen', 'maxLen');
3152

32-
$lengthPattern = is_null($minLen) ? '.{0,' : '.{' . $minLen . ',';
53+
$lengthPattern = is_null($minLen) ? '{0,' : '{' . $minLen . ',';
3354
$lengthPattern .= is_null($maxLen) ? '}' : $maxLen . '}';
3455

35-
$instance->pattern = '/^' . $lengthPattern . '$/u';
56+
$instance->pattern = '/^' . $strPattern . $lengthPattern . '$/';
3657

3758
return $instance;
3859
}

tests/Unit/Validation/StringRuleTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function data_provider(): array
4343
'validationRuleClassString' => StringRule::class,
4444
'makeParams' => null,
4545
'expectedMakeException' => null,
46-
'valueToBeEvaluated' => 123,
46+
'valueToBeEvaluated' => 'test1234',
4747
'expectedResult' => false
4848
],
4949
'Will evaluate true when a string value is provided' => [
@@ -109,6 +109,20 @@ public static function data_provider(): array
109109
'valueToBeEvaluated' => 'test',
110110
'expectedResult' => false
111111
],
112+
'Will evaluate alpha numeric strings as true when a param is passed' => [
113+
'validationRuleClassString' => StringRule::class,
114+
'makeParams' => ['type' => 'alpha'],
115+
'expectedMakeException' => null,
116+
'valueToBeEvaluated' => 'test123',
117+
'expectedResult' => true
118+
],
119+
'Will throw a validation exception when an invalid type is passed' => [
120+
'validationRuleClassString' => StringRule::class,
121+
'makeParams' => ['type' => 'invalid'],
122+
'expectedMakeException' => ValidationRuleException::class,
123+
'valueToBeEvaluated' => 'test',
124+
'expectedResult' => false
125+
],
112126
];
113127
}
114128
}

0 commit comments

Comments
 (0)