Permalink
Browse files

Fixing #6, grouping expressions with wildcard args in pattern.

  • Loading branch information...
1 parent 182e424 commit 463b67acc15006185c5ca4dcc9f296d629ab63d1 @deceze committed Jun 7, 2013
Showing with 19 additions and 7 deletions.
  1. +7 −7 Route.php
  2. +4 −0 readme.md
  3. +8 −0 tests/RouteTest.php
View
@@ -62,8 +62,10 @@ public function matchUrl($url) {
$route = clone $this;
$route->url = $url;
$route->mergeNamedMatches($matches);
- if ($route->wildcard) {
- $route->wildcardArgs = $route->parseWildcardArgs(reset($matches));
+
+ // wildcard args, if present, should be the last element of the array and uniquely start with a slash
+ if ($route->wildcard && strpos($wildcards = end($matches), '/') === 0) {
+ $route->wildcardArgs = $route->parseWildcardArgs($wildcards);
}
return $route;
@@ -379,12 +381,11 @@ protected function buildRegex() {
/**
* Merges named values matched from a URL into the dispatch array.
- * Modifies the matches in place to remove processed items, leaving unprocessed wildcard args.
*
- * @param array &$matches
+ * @param array $matches
+ * @throws \LogicException
*/
- protected function mergeNamedMatches(array &$matches) {
- $i = 0;
+ protected function mergeNamedMatches(array $matches) {
foreach ($matches as $key => $value) {
if (!is_string($key)) {
continue;
@@ -393,7 +394,6 @@ protected function mergeNamedMatches(array &$matches) {
throw new LogicException("Route has no dispatch key '$key', should not have matched");
}
$this->dispatch[$key] = $value;
- unset($matches[$key], $matches[$i++]);
}
}
View
@@ -610,6 +610,10 @@ Web: http://kunststube.net, https://github.com/deceze/Kunststube-Router
Version history
---------------
+### 0.2.2
+
+Fix to allow grouping expressions in pattern together with wildcards, e.g.: `/foo/(bar|baz):id/*`
+
### 0.2.1
Changed thrown exception to specialized NotFoundException in case no route matched.
View
@@ -73,5 +73,13 @@ public function testTrailingSlash() {
$r = new Route('/foo');
$this->assertInstanceOf('Kunststube\Router\Route', $r->matchUrl('/foo/'));
}
+
+ public function testGroupedExpressionWithWildcards() {
+ $r = new Route('/foo/(bar|baz):id/*');
+ $m = $r->matchUrl('/foo/bar/42');
+ $this->assertInstanceOf('Kunststube\Router\Route', $m);
+ $this->assertEquals('bar', $m->id);
+ $this->assertEquals(array(42), $m->wildcardArgs());
+ }
}

0 comments on commit 463b67a

Please sign in to comment.