Skip to content

Commit

Permalink
updated and fixes error on how URL parameter type works
Browse files Browse the repository at this point in the history
  • Loading branch information
dconco committed Dec 25, 2024
1 parent 5f6e7f3 commit 8191cc6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
15 changes: 7 additions & 8 deletions src/Utils/Routes/Exception/InvalidTypesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class InvalidTypesException extends \PhpSlides\Exception
* - INT: Integer
* - BOOL: Boolean
* - JSON: JSON string
* - ALPHA: Alphabetic characters
* - ALNUM: Alphanumeric characters
* - ARRAY: Array
* - FLOAT: Floating point number
* - STRING: String
Expand All @@ -26,8 +24,6 @@ class InvalidTypesException extends \PhpSlides\Exception
'INT',
'BOOL',
'JSON',
'ALPHA',
'ALNUM',
'ARRAY',
'FLOAT',
'STRING',
Expand All @@ -48,6 +44,8 @@ public static function catchInvalidStrictTypes (array|string $type, ?Closure $me
{
if (is_array($type))
{
$type = array_map(fn($t) => strtoupper($t), $type);

foreach ($type as $t)
{
if (!in_array($t, self::$types) && !preg_match('/<(.+)>/', (string) $t))
Expand All @@ -65,6 +63,7 @@ public static function catchInvalidStrictTypes (array|string $type, ?Closure $me
}
else
{
$type = strtoupper($type);
if (!in_array($type, self::$types) && !preg_match('/<(.+)>/', (string) $type))
{
if (!$message)
Expand Down Expand Up @@ -105,13 +104,13 @@ public static function catchInvalidParameterTypes (array $typeRequested, string
if (!$message)
{
$requested = implode(', ', $typeRequested);
return new self(
"Invalid request parameter type. {{$requested}} requested, but got {{$typeGotten}}",
);
$requested = preg_replace('/<[^<>]*>/', '', $requested);

return new self(htmlspecialchars("Invalid request parameter type. {{$requested}} requested, but got {{$typeGotten}}"));
}
else
{
return new self($message);
return new self(htmlspecialchars($message));
}
}
}
Expand Down
16 changes: 3 additions & 13 deletions src/Utils/Routes/StrictTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static function matches (string $needle, string $haystack): bool
? json_encode($needle[$key])
: (string) $needle[$key];

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

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

if ((!$max && $min < $needle) || $max && ($needle < $min || $needle > $max))
if ((!$max && $needle < $min) || $max && ($needle < $min || $needle > $max))
{
$requested = !$max ? "INT min ($min)" : "INT min ($min), max($max)";
throw new Exception("Invalid request parameter type. {{$requested}} requested, but got {{$needle}}");
Expand All @@ -169,9 +169,7 @@ private static function matches (string $needle, string $haystack): bool
* The possible return values are:
* - 'FLOAT' if the string represents a floating-point number.
* - 'INT' if the string represents an integer.
* - 'BOOL' if the string represents a boolean value ('true' or 'false').
* - 'ALPHA' if the string contains only alphabetic characters.
* - 'ALNUM' if the string contains only alphanumeric characters.
* - 'BOOL' if the string represents a boolean value.
* - 'JSON' if the string is a valid JSON object.
* - 'ARRAY' if the string is a valid JSON array.
* - 'STRING' if the string does not match any of the above types.
Expand All @@ -191,14 +189,6 @@ protected static function typeOfString (string $string): string
{
return 'BOOL';
}
elseif (ctype_alpha($string))
{
return 'ALPHA';
}
elseif (ctype_alnum($string))
{
return 'ALNUM';
}
elseif (json_last_error() === JSON_ERROR_NONE)
{
return match (gettype($decoded))
Expand Down
32 changes: 16 additions & 16 deletions tests/manualTests/Router/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
$dir = '/tests/manualTests/Router/RouteTest.php';

Route::get(
route: $dir,
callback: function ()
{
return 'Hello World';
},
route: $dir,
callback: function () {
return 'Hello World';
},
);

Route::map(GET, "$dir/user/{id: int<6>|bool|array<array<int, string>, string>|alnum}")
->action(function (Request $req)
{
echo '<br>';
return $req->urlParam('id');
})
->route('/posts/{id: int}', function (Request $req, Closure $accept)
{
$accept('POST');
});
Route::map(
GET,
"$dir/user/{id: int<6, 10>|bool|array<array<int<5,5>|bool>, string>}",
)
->action(function (Request $req) {
echo '<br>';
return $req->urlParam('id');
})
->route('/posts/{id: int}', function (Request $req, Closure $accept) {
$accept('POST');
});

Render::WebRoute();
Render::WebRoute();

0 comments on commit 8191cc6

Please sign in to comment.