Skip to content

Commit 12d1d2d

Browse files
committed
Preserve numeric segments in path by filtering only empty strings
- Update array_filter to use a lambda function that filters out only empty strings. - Ensures numeric segments like '0' are not removed from the path. - Add test to ensure numeric segments are preserved in path matching
1 parent cc880ec commit 12d1d2d

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
/.idea/
3-
*.cache
3+
*.cache
4+
.history

src/Http/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static function match(string $method, string $path): Route|null
123123
return null;
124124
}
125125

126-
$parts = array_values(array_filter(explode('/', $path)));
126+
$parts = array_values(array_filter(explode('/', $path), fn($segment) => $segment !== ''));
127127
$length = count($parts) - 1;
128128
$filteredParams = array_filter(self::$params, fn ($i) => $i <= $length);
129129

tests/RouterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testCanMatchUrlWithPlaceholder(): void
4747
$this->assertEquals($routeBlogAuthorsComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/authors/comments'));
4848
$this->assertEquals($routeBlogPost, Router::match(Http::REQUEST_METHOD_GET, '/blog/test'));
4949
$this->assertEquals($routeBlogPostComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments'));
50+
$this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/0'));
5051
$this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/:comment'));
5152
}
5253

0 commit comments

Comments
 (0)