Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for optional middle segments #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions src/RouteParser/Std.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,62 @@ class Std implements RouteParser

public function parse($route)
{
$routeWithoutClosingOptionals = rtrim($route, ']');
$numOptionals = strlen($route) - strlen($routeWithoutClosingOptionals);

// Split on [ while skipping placeholders
$segments = preg_split('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \[~x', $routeWithoutClosingOptionals);
if ($numOptionals !== count($segments) - 1) {
// If there are any ] in the middle of the route, throw a more specific error message
if (preg_match('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \]~x', $routeWithoutClosingOptionals)) {
throw new BadRouteException('Optional segments can only occur at the end of a route');
}
throw new BadRouteException("Number of opening '[' and closing ']' does not match");
if (strcspn($route, '[]') === strlen($route)) {
return [$this->parsePlaceholders($route)];
}

$currentRoute = '';
$routeDatas = [];
foreach ($segments as $n => $segment) {
if ($segment === '' && $n !== 0) {
throw new BadRouteException('Empty optional part');
$routeDatas = $this->consume($route);

return array_map([$this, 'parsePlaceholders'], $routeDatas);
}

private function consume(& $route, $recursion = false)
{
$routeDatas = [''];

do {
$segments = preg_split(
'~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | ( \[ | ] )~x',
$route,
2
);

foreach ($routeDatas as $key => $data) {
$routeDatas[$key] = $data . $segments[0];
}

$currentRoute .= $segment;
$routeDatas[] = $this->parsePlaceholders($currentRoute);
if (isset($segments[1])) {
$delimiter = $route[strlen($segments[0])];
$route = $segments[1];

if ($delimiter === ']') {
if (!$recursion) {
throw new BadRouteException("Number of opening '[' and closing ']' does not match");
}

if (in_array('', $routeDatas, true)) {
throw new BadRouteException('Empty optional part');
}

return $routeDatas;
}

$forks = $this->consume($route, true);

foreach ($routeDatas as $data) {
foreach ($forks as $fork) {
$routeDatas[] = $data . $fork;
}
}
} else {
$route = '';
}
} while (isset($segments[1]));

if ($recursion) {
throw new BadRouteException("Number of opening '[' and closing ']' does not match");
}

return $routeDatas;
}

Expand Down
48 changes: 44 additions & 4 deletions test/RouteParser/StdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,46 @@ public function provideTestParse()
['/', ['_foo', '.*']]
]
],
[
'/test[/opt]/required',
[
['/test/required'],
['/test/opt/required'],
]
],
[
'/test[/opt[/sub1][/sub2]]/required[/end]',
[
['/test/required'],
['/test/opt/required'],
['/test/opt/sub1/required'],
['/test/opt/sub2/required'],
['/test/opt/sub1/sub2/required'],
['/test/required/end'],
['/test/opt/required/end'],
['/test/opt/sub1/required/end'],
['/test/opt/sub2/required/end'],
['/test/opt/sub1/sub2/required/end'],
]
],
[
'/test[/[opt[/]]]',
[
['/test'],
['/test/'],
['/test/opt'],
['/test/opt/'],
]
],
[
'/test[/opt][/]',
[
['/test'],
['/test/opt'],
['/test/'],
['/test/opt/'],
]
]
];
}

Expand All @@ -133,6 +173,10 @@ public function provideTestParseError()
'/testopt]',
"Number of opening '[' and closing ']' does not match"
],
[
'/test]opt',
"Number of opening '[' and closing ']' does not match"
],
[
'/test[]',
'Empty optional part'
Expand All @@ -145,10 +189,6 @@ public function provideTestParseError()
'[[test]]',
'Empty optional part'
],
[
'/test[/opt]/required',
'Optional segments can only occur at the end of a route'
],
];
}
}