Skip to content

Commit

Permalink
Preserve numeric segments in path by filtering only empty strings
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ebenezerdon committed Sep 9, 2024
1 parent cc880ec commit 12d1d2d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/.idea/
*.cache
*.cache
.history
2 changes: 1 addition & 1 deletion src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static function match(string $method, string $path): Route|null
return null;
}

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

Expand Down
1 change: 1 addition & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testCanMatchUrlWithPlaceholder(): void
$this->assertEquals($routeBlogAuthorsComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/authors/comments'));
$this->assertEquals($routeBlogPost, Router::match(Http::REQUEST_METHOD_GET, '/blog/test'));
$this->assertEquals($routeBlogPostComments, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments'));
$this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/0'));
$this->assertEquals($routeBlogPostCommentsSingle, Router::match(Http::REQUEST_METHOD_GET, '/blog/test/comments/:comment'));
}

Expand Down

0 comments on commit 12d1d2d

Please sign in to comment.