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

Commit 5f6e7f3

Browse files
committed
Add INT range validation in StrictTypes; update route parameter mapping in RouteTest
1 parent 05c6794 commit 5f6e7f3

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/Utils/Routes/StrictTypes.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ protected static function matchStrictType (
6666
{
6767
return match ($typeOfNeedle)
6868
{
69-
'INT' => (int) $needle,
70-
'BOOL' => filter_var($needle, FILTER_VALIDATE_BOOLEAN),
71-
'FLOAT' => (float) $needle,
72-
'ARRAY' => json_decode($needle, true),
73-
default => $needle,
69+
'INT' => (int) $needle,
70+
'BOOL' => filter_var($needle, FILTER_VALIDATE_BOOLEAN),
71+
'FLOAT' => (float) $needle,
72+
'ARRAY' => json_decode($needle, true),
73+
default => $needle,
7474
};
7575
}
7676

@@ -140,6 +140,23 @@ private static function matches (string $needle, string $haystack): bool
140140
return true;
141141
}
142142

143+
/**
144+
* MATCH INT<MIN, MAX>
145+
*/
146+
if (preg_match('/INT<(\d+)(?:,\s*(\d+))?>/', $haystack, $matches) && $typeOfNeedle === 'INT')
147+
{
148+
$min = (int) $matches[1];
149+
$max = (int) $matches[2] ?? null;
150+
$needle = (int) $needle;
151+
152+
if ((!$max && $min < $needle) || $max && ($needle < $min || $needle > $max))
153+
{
154+
$requested = !$max ? "INT min ($min)" : "INT min ($min), max($max)";
155+
throw new Exception("Invalid request parameter type. {{$requested}} requested, but got {{$needle}}");
156+
}
157+
return true;
158+
}
159+
143160
InvalidTypesException::catchInvalidStrictTypes($haystack);
144161
return false;
145162
}
@@ -186,9 +203,9 @@ protected static function typeOfString (string $string): string
186203
{
187204
return match (gettype($decoded))
188205
{
189-
'object' => 'JSON',
190-
'array' => 'ARRAY',
191-
default => 'STRING',
206+
'object' => 'JSON',
207+
'array' => 'ARRAY',
208+
default => 'STRING',
192209
};
193210
}
194211

tests/manualTests/Router/RouteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
},
1818
);
1919

20-
Route::map(GET, "$dir/user/{id: int|bool|array<array<int, string>, string>|alnum}")
20+
Route::map(GET, "$dir/user/{id: int<6>|bool|array<array<int, string>, string>|alnum}")
2121
->action(function (Request $req)
2222
{
2323
echo '<br>';
24-
return $req->urlParam();
24+
return $req->urlParam('id');
2525
})
2626
->route('/posts/{id: int}', function (Request $req, Closure $accept)
2727
{

0 commit comments

Comments
 (0)