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

Commit 8191cc6

Browse files
committed
updated and fixes error on how URL parameter type works
1 parent 5f6e7f3 commit 8191cc6

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
lines changed

src/Utils/Routes/Exception/InvalidTypesException.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class InvalidTypesException extends \PhpSlides\Exception
1414
* - INT: Integer
1515
* - BOOL: Boolean
1616
* - JSON: JSON string
17-
* - ALPHA: Alphabetic characters
18-
* - ALNUM: Alphanumeric characters
1917
* - ARRAY: Array
2018
* - FLOAT: Floating point number
2119
* - STRING: String
@@ -26,8 +24,6 @@ class InvalidTypesException extends \PhpSlides\Exception
2624
'INT',
2725
'BOOL',
2826
'JSON',
29-
'ALPHA',
30-
'ALNUM',
3127
'ARRAY',
3228
'FLOAT',
3329
'STRING',
@@ -48,6 +44,8 @@ public static function catchInvalidStrictTypes (array|string $type, ?Closure $me
4844
{
4945
if (is_array($type))
5046
{
47+
$type = array_map(fn($t) => strtoupper($t), $type);
48+
5149
foreach ($type as $t)
5250
{
5351
if (!in_array($t, self::$types) && !preg_match('/<(.+)>/', (string) $t))
@@ -65,6 +63,7 @@ public static function catchInvalidStrictTypes (array|string $type, ?Closure $me
6563
}
6664
else
6765
{
66+
$type = strtoupper($type);
6867
if (!in_array($type, self::$types) && !preg_match('/<(.+)>/', (string) $type))
6968
{
7069
if (!$message)
@@ -105,13 +104,13 @@ public static function catchInvalidParameterTypes (array $typeRequested, string
105104
if (!$message)
106105
{
107106
$requested = implode(', ', $typeRequested);
108-
return new self(
109-
"Invalid request parameter type. {{$requested}} requested, but got {{$typeGotten}}",
110-
);
107+
$requested = preg_replace('/<[^<>]*>/', '', $requested);
108+
109+
return new self(htmlspecialchars("Invalid request parameter type. {{$requested}} requested, but got {{$typeGotten}}"));
111110
}
112111
else
113112
{
114-
return new self($message);
113+
return new self(htmlspecialchars($message));
115114
}
116115
}
117116
}

src/Utils/Routes/StrictTypes.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static function matches (string $needle, string $haystack): bool
123123
? json_encode($needle[$key])
124124
: (string) $needle[$key];
125125

126-
$eachTypes = preg_split('/\|(?![^<]*>)/', trim($eachArrayType));
126+
$eachTypes = preg_split('/\|(?![^<]*>)/', trim(strtoupper($eachArrayType)));
127127
$typeOfNeedle2 = self::typeOfString($needle2);
128128

129129
if (!self::matchType($needle2, $eachTypes))
@@ -149,7 +149,7 @@ private static function matches (string $needle, string $haystack): bool
149149
$max = (int) $matches[2] ?? null;
150150
$needle = (int) $needle;
151151

152-
if ((!$max && $min < $needle) || $max && ($needle < $min || $needle > $max))
152+
if ((!$max && $needle < $min) || $max && ($needle < $min || $needle > $max))
153153
{
154154
$requested = !$max ? "INT min ($min)" : "INT min ($min), max($max)";
155155
throw new Exception("Invalid request parameter type. {{$requested}} requested, but got {{$needle}}");
@@ -169,9 +169,7 @@ private static function matches (string $needle, string $haystack): bool
169169
* The possible return values are:
170170
* - 'FLOAT' if the string represents a floating-point number.
171171
* - 'INT' if the string represents an integer.
172-
* - 'BOOL' if the string represents a boolean value ('true' or 'false').
173-
* - 'ALPHA' if the string contains only alphabetic characters.
174-
* - 'ALNUM' if the string contains only alphanumeric characters.
172+
* - 'BOOL' if the string represents a boolean value.
175173
* - 'JSON' if the string is a valid JSON object.
176174
* - 'ARRAY' if the string is a valid JSON array.
177175
* - 'STRING' if the string does not match any of the above types.
@@ -191,14 +189,6 @@ protected static function typeOfString (string $string): string
191189
{
192190
return 'BOOL';
193191
}
194-
elseif (ctype_alpha($string))
195-
{
196-
return 'ALPHA';
197-
}
198-
elseif (ctype_alnum($string))
199-
{
200-
return 'ALNUM';
201-
}
202192
elseif (json_last_error() === JSON_ERROR_NONE)
203193
{
204194
return match (gettype($decoded))

tests/manualTests/Router/RouteTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
$dir = '/tests/manualTests/Router/RouteTest.php';
1111

1212
Route::get(
13-
route: $dir,
14-
callback: function ()
15-
{
16-
return 'Hello World';
17-
},
13+
route: $dir,
14+
callback: function () {
15+
return 'Hello World';
16+
},
1817
);
1918

20-
Route::map(GET, "$dir/user/{id: int<6>|bool|array<array<int, string>, string>|alnum}")
21-
->action(function (Request $req)
22-
{
23-
echo '<br>';
24-
return $req->urlParam('id');
25-
})
26-
->route('/posts/{id: int}', function (Request $req, Closure $accept)
27-
{
28-
$accept('POST');
29-
});
19+
Route::map(
20+
GET,
21+
"$dir/user/{id: int<6, 10>|bool|array<array<int<5,5>|bool>, string>}",
22+
)
23+
->action(function (Request $req) {
24+
echo '<br>';
25+
return $req->urlParam('id');
26+
})
27+
->route('/posts/{id: int}', function (Request $req, Closure $accept) {
28+
$accept('POST');
29+
});
3030

31-
Render::WebRoute();
31+
Render::WebRoute();

0 commit comments

Comments
 (0)