Skip to content

Commit 5172f22

Browse files
committed
refactor(router): optional marker is now in the front
1 parent 873a47c commit 5172f22

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

packages/router/src/Routing/Construction/DiscoveredRoute.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ final class DiscoveredRoute implements Route
1212
{
1313
public const string DEFAULT_MATCHING_GROUP = '[^/]++';
1414

15-
public const string ROUTE_PARAM_NAME_REGEX = '(\w*)';
16-
1715
public const string ROUTE_PARAM_OPTIONAL_REGEX = '(\??)';
1816

17+
public const string ROUTE_PARAM_NAME_REGEX = '(\w*)';
18+
1919
public const string ROUTE_PARAM_CUSTOM_REGEX = '(?::([^{}]*(?:\{(?-1)\}[^{}]*)*))?';
2020

2121
/** @param \Tempest\Router\RouteDecorator[] $decorators */
@@ -62,12 +62,12 @@ private function __construct(
6262
*/
6363
private static function getRouteParams(string $uriPart): array
6464
{
65-
$regex = '#\{' . self::ROUTE_PARAM_NAME_REGEX . self::ROUTE_PARAM_OPTIONAL_REGEX . self::ROUTE_PARAM_CUSTOM_REGEX . '\}#';
65+
$regex = '#\{' . self::ROUTE_PARAM_OPTIONAL_REGEX . self::ROUTE_PARAM_NAME_REGEX . self::ROUTE_PARAM_CUSTOM_REGEX . '\}#';
6666

6767
preg_match_all($regex, $uriPart, $matches);
6868

69-
$names = $matches[1] ?? [];
70-
$optionalMarkers = $matches[2] ?? [];
69+
$optionalMarkers = $matches[1] ?? [];
70+
$names = $matches[2] ?? [];
7171

7272
$optional = [];
7373
foreach ($names as $i => $name) {

packages/router/src/Routing/Construction/RouteTreeNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setTargetRoute(MarkedRoute $markedRoute, bool $allowDuplicate =
6363

6464
private static function convertDynamicSegmentToRegex(string $uriPart): string
6565
{
66-
$regex = '#\{' . DiscoveredRoute::ROUTE_PARAM_NAME_REGEX . DiscoveredRoute::ROUTE_PARAM_OPTIONAL_REGEX . DiscoveredRoute::ROUTE_PARAM_CUSTOM_REGEX . '\}#';
66+
$regex = '#\{' . DiscoveredRoute::ROUTE_PARAM_OPTIONAL_REGEX . DiscoveredRoute::ROUTE_PARAM_NAME_REGEX . DiscoveredRoute::ROUTE_PARAM_CUSTOM_REGEX . '\}#';
6767

6868
return preg_replace_callback(
6969
$regex,

packages/router/tests/OptionalParametersTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function test_route_with_optional_parameter_matches_both_paths(): void
2222
$tree = new RoutingTree();
2323

2424
$markedRoute = $routeBuilder
25-
->withUri('/users/{id?}')
25+
->withUri('/users/{?id}')
2626
->withHandler('handlerWithOptionalId')
2727
->asMarkedRoute('a');
2828

@@ -43,12 +43,12 @@ public function test_route_with_optional_parameter_matches_both_paths(): void
4343

4444
$matchedWithoutParam = $matcher->match(new GenericRequest(Method::GET, '/users'));
4545
$this->assertNotNull($matchedWithoutParam);
46-
$this->assertEquals('/users/{id?}', $matchedWithoutParam->route->uri);
46+
$this->assertEquals('/users/{?id}', $matchedWithoutParam->route->uri);
4747
$this->assertEquals(['id' => 'default-id'], $matchedWithoutParam->params);
4848

4949
$matchedWithParam = $matcher->match(new GenericRequest(Method::GET, '/users/123'));
5050
$this->assertNotNull($matchedWithParam);
51-
$this->assertEquals('/users/{id?}', $matchedWithParam->route->uri);
51+
$this->assertEquals('/users/{?id}', $matchedWithParam->route->uri);
5252
$this->assertEquals(['id' => '123'], $matchedWithParam->params);
5353
}
5454

@@ -58,7 +58,7 @@ public function test_route_with_multiple_optional_parameters(): void
5858
$tree = new RoutingTree();
5959

6060
$markedRoute = $routeBuilder
61-
->withUri('/posts/{id?}/{slug?}')
61+
->withUri('/posts/{?id}/{?slug}')
6262
->withHandler('handlerWithTwoOptionalParams')
6363
->asMarkedRoute('a');
6464

@@ -96,7 +96,7 @@ public function test_route_with_required_and_optional_parameters(): void
9696
$tree = new RoutingTree();
9797

9898
$markedRoute = $routeBuilder
99-
->withUri('/posts/{id}/{slug?}')
99+
->withUri('/posts/{id}/{?slug}')
100100
->withHandler('handlerWithRequiredAndOptional')
101101
->asMarkedRoute('a');
102102

@@ -130,7 +130,7 @@ public function test_route_with_optional_parameter_and_custom_regex(): void
130130
$tree = new RoutingTree();
131131

132132
$markedRoute = $routeBuilder
133-
->withUri('/users/{id?:\d+}')
133+
->withUri('/users/{?id:\d+}')
134134
->withHandler('handlerWithOptionalId')
135135
->asMarkedRoute('a');
136136

@@ -166,7 +166,7 @@ public function test_optional_parameters_are_parsed_correctly(): void
166166
$routeBuilder = new FakeRouteBuilderWithOptionalParams();
167167

168168
$route = $routeBuilder
169-
->withUri('/users/{id?}')
169+
->withUri('/users/{?id}')
170170
->withHandler('handlerWithOptionalId')
171171
->asDiscoveredRoute();
172172

@@ -179,7 +179,7 @@ public function test_mixed_optional_and_required_parameters_are_parsed_correctly
179179
$routeBuilder = new FakeRouteBuilderWithOptionalParams();
180180

181181
$route = $routeBuilder
182-
->withUri('/posts/{id}/{slug?}')
182+
->withUri('/posts/{id}/{?slug}')
183183
->withHandler('handlerWithRequiredAndOptional')
184184
->asDiscoveredRoute();
185185

0 commit comments

Comments
 (0)