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

Commit 05c6794

Browse files
committed
Enhance type validation in StrictTypes and InvalidTypesException; improve error handling for array parameters
1 parent 8f3750f commit 05c6794

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/Utils/Routes/Exception/InvalidTypesException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function catchInvalidStrictTypes (array|string $type, ?Closure $me
5050
{
5151
foreach ($type as $t)
5252
{
53-
if (!in_array($t, self::$types))
53+
if (!in_array($t, self::$types) && !preg_match('/<(.+)>/', (string) $t))
5454
{
5555
if (!$message)
5656
{
@@ -65,7 +65,7 @@ public static function catchInvalidStrictTypes (array|string $type, ?Closure $me
6565
}
6666
else
6767
{
68-
if (!in_array($type, self::$types))
68+
if (!in_array($type, self::$types) && !preg_match('/<(.+)>/', (string) $type))
6969
{
7070
if (!$message)
7171
{

src/Utils/Routes/StrictTypes.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpSlides\Core\Utils\Routes;
44

5+
use PhpSlides\Exception;
56
use PhpSlides\Core\Utils\Routes\Exception\InvalidTypesException;
67

78
/**
@@ -66,13 +67,14 @@ protected static function matchStrictType (
6667
return match ($typeOfNeedle)
6768
{
6869
'INT' => (int) $needle,
69-
'BOOL' => (bool) $needle,
70+
'BOOL' => filter_var($needle, FILTER_VALIDATE_BOOLEAN),
7071
'FLOAT' => (float) $needle,
7172
'ARRAY' => json_decode($needle, true),
7273
default => $needle,
7374
};
7475
}
7576

77+
InvalidTypesException::catchInvalidStrictTypes($haystack);
7678
throw InvalidTypesException::catchInvalidParameterTypes($types, $typeOfNeedle);
7779
}
7880

@@ -103,10 +105,20 @@ private static function matches (string $needle, string $haystack): bool
103105
)
104106
{
105107
$needle = json_decode($needle, true);
106-
$eachArrayTypes = explode(',', $matches[1]);
108+
$eachArrayTypes = preg_split('/,(?![^<]*>)/', $matches[1]);
109+
110+
if (!is_array($needle))
111+
{
112+
throw new Exception("Invalid request parameter type. {ARRAY} requested, but got {{$typeOfNeedle}}");
113+
}
107114

108115
foreach ($eachArrayTypes as $key => $eachArrayType)
109116
{
117+
if (!isset($needle[$key]))
118+
{
119+
throw new Exception("Array index $key not found in the request parameter");
120+
}
121+
110122
$needle2 = is_array($needle[$key])
111123
? json_encode($needle[$key])
112124
: (string) $needle[$key];
@@ -152,20 +164,13 @@ private static function matches (string $needle, string $haystack): bool
152164
*/
153165
protected static function typeOfString (string $string): string
154166
{
155-
$jd = json_decode($string, false);
167+
$decoded = json_decode($string, false);
156168

157169
if (is_numeric($string))
158170
{
159-
if (strpos($string, '.') !== false)
160-
{
161-
return 'FLOAT';
162-
}
163-
else
164-
{
165-
return 'INT';
166-
}
171+
return strpos($string, '.') !== false ? 'FLOAT' : 'INT';
167172
}
168-
elseif (is_bool($string) || $string === 'true' || $string === 'false')
173+
elseif (filter_var($string, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null)
169174
{
170175
return 'BOOL';
171176
}
@@ -179,16 +184,14 @@ protected static function typeOfString (string $string): string
179184
}
180185
elseif (json_last_error() === JSON_ERROR_NONE)
181186
{
182-
return match (gettype($jd))
187+
return match (gettype($decoded))
183188
{
184189
'object' => 'JSON',
185190
'array' => 'ARRAY',
186191
default => 'STRING',
187192
};
188193
}
189-
else
190-
{
191-
return 'STRING';
192-
}
194+
195+
return 'STRING';
193196
}
194197
}

tests/manualTests/Router/RouteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
);
1919

20-
Route::map(GET, "$dir/user/{id: int|bool|array<array<int>, string>|alnum}")
20+
Route::map(GET, "$dir/user/{id: int|bool|array<array<int, string>, string>|alnum}")
2121
->action(function (Request $req)
2222
{
2323
echo '<br>';

0 commit comments

Comments
 (0)