Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ignaszak committed May 1, 2016
1 parent 59ff354 commit 489fc6c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Matcher/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private function transformToRegex()
$route['tokens'][$token] ?? $tokenArray[$token]
);
$subpatterns[$token] = "(?P<{$token}>{$tokens[$token]})";
$tokens[$token] = $this->preparePattern($tokens[$token]);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function match(
}

foreach ($this->route->getRouteArray() as $name => $route) {
//echo $route['path'];exit;
if (preg_match(
$route['path'],
$query,
Expand Down
16 changes: 9 additions & 7 deletions src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ public function url(string $name, array $replacement): string
$search = [];
$replace = [];
foreach ($route['tokens'] as $token => $pattern) {
$value = (string)$replacement[$token];
if (! preg_match("/^{$pattern}$/", $value)) {
throw new RouterException(
"Value '{$value}' don't match token {{$token}} `{$pattern}` in route '{$name}'"
);
if (array_key_exists($token, $replacement)) {
$value = (string) $replacement[$token];
if (! preg_match($pattern, $value)) {
throw new RouterException(
"Value '{$value}' don't match token {{$token}} `{$pattern}` in route '{$name}'"
);
}
$search[] = "{{$token}}";
$replace[] = $value;
}
$search[] = "{{$token}}";
$replace[] = $value;
}
$link = str_replace(
['\\', '?', '(', ')'],
Expand Down
8 changes: 4 additions & 4 deletions tests/Matcher/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public function testTransformToRegex()
'name' => [
'path' => '/^\/test\/(?P<token1>(\w+))\/(\d+)\/(?P<globaltoken>([\w-]+))$/',
'tokens' => [
'token1' => '(\w+)',
'globaltoken' => '([\w-]+)'
'token1' => '/^(\w+)$/',
'globaltoken' => '/^([\w-]+)$/'
],
'route' => '/test/{token1}/@digit/{globaltoken}'
]
Expand Down Expand Up @@ -180,8 +180,8 @@ public function testConvert()
'name' => [
'path' => '/^\/(?P<token>test1)\/(?P<globalToken>test2)\/test3$/',
'tokens' => [
'token' => 'test1',
'globalToken' => 'test2'
'token' => '/^test1$/',
'globalToken' => '/^test2$/'
],
'route' => '/{token}/{globalToken}/@pattern'
]
Expand Down

0 comments on commit 489fc6c

Please sign in to comment.