Skip to content

Commit

Permalink
Add support for unicode regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
koenpunt committed Apr 16, 2014
1 parent fb41766 commit fc6d743
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 @@ -170,7 +170,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 @@ -249,6 +249,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 @@ -266,6 +266,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 @@ -290,4 +320,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 fc6d743

Please sign in to comment.