Skip to content

Commit

Permalink
Merge pull request #69 from koenpunt/unicode-regex
Browse files Browse the repository at this point in the history
Add support for unicode regular expressions
  • Loading branch information
dannyvankooten committed Apr 16, 2014
2 parents b749633 + fc6d743 commit 09d9d94
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AltoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function match($requestUrl = null, $requestMethod = null) {
if ($_route === '*') {
$match = true;
} elseif (isset($_route[0]) && $_route[0] === '@') {
$match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params);
$match = preg_match('`' . substr($_route, 1) . '`u', $requestUrl, $params);
} else {
$route = null;
$regex = false;
Expand Down Expand Up @@ -265,6 +265,6 @@ private function compileRoute($route) {
}

}
return "`^$route$`";
return "`^$route$`u";
}
}
54 changes: 54 additions & 0 deletions AltoRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,36 @@ public function testMatchWithCustomRegexp()

}

public function testMatchWithUnicodeRegex()
{
$pattern = '/(?<path>[^';
// Arabic characters
$pattern .= '\x{0600}-\x{06FF}';
$pattern .= '\x{FB50}-\x{FDFD}';
$pattern .= '\x{FE70}-\x{FEFF}';
$pattern .= '\x{0750}-\x{077F}';
// Alphanumeric, /, _, - and space characters
$pattern .= 'a-zA-Z0-9\/_-\s';
// 'ZERO WIDTH NON-JOINER'
$pattern .= '\x{200C}';
$pattern .= ']+)';

$this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route');

$this->assertEquals(array(
'target' => 'unicode_action',
'name' => 'unicode_route',
'params' => array(
'path' => '大家好'
)
), $this->router->match('/大家好', 'GET'));

$this->assertFalse($this->router->match('/﷽‎', 'GET'));
}

/**
* @covers AltoRouter::addMatchTypes
*/
public function testMatchWithCustomNamedRegex()
{
$this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
Expand All @@ -366,4 +396,28 @@ public function testMatchWithCustomNamedRegex()
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));

}

public function testMatchWithCustomNamedUnicodeRegex()
{
$pattern = '[^';
// Arabic characters
$pattern .= '\x{0600}-\x{06FF}';
$pattern .= '\x{FB50}-\x{FDFD}';
$pattern .= '\x{FE70}-\x{FEFF}';
$pattern .= '\x{0750}-\x{077F}';
$pattern .= ']+';

$this->router->addMatchTypes(array('nonArabic' => $pattern));
$this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route');

$this->assertEquals(array(
'target' => 'non_arabic_action',
'name' => 'non_arabic_route',
'params' => array(
'string' => 'some-path'
)
), $this->router->match('/bar/some-path', 'GET'));

$this->assertFalse($this->router->match('/﷽‎', 'GET'));
}
}

0 comments on commit 09d9d94

Please sign in to comment.