mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-08-02 14:37:43 +02:00
Merge pull request #69 from koenpunt/unicode-regex
Add support for unicode regular expressions
This commit is contained in:
@@ -186,7 +186,7 @@ class AltoRouter {
|
|||||||
if ($_route === '*') {
|
if ($_route === '*') {
|
||||||
$match = true;
|
$match = true;
|
||||||
} elseif (isset($_route[0]) && $_route[0] === '@') {
|
} elseif (isset($_route[0]) && $_route[0] === '@') {
|
||||||
$match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params);
|
$match = preg_match('`' . substr($_route, 1) . '`u', $requestUrl, $params);
|
||||||
} else {
|
} else {
|
||||||
$route = null;
|
$route = null;
|
||||||
$regex = false;
|
$regex = false;
|
||||||
@@ -265,6 +265,6 @@ class AltoRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return "`^$route$`";
|
return "`^$route$`u";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -342,6 +342,36 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
public function testMatchWithCustomNamedRegex()
|
||||||
{
|
{
|
||||||
$this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
|
$this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
|
||||||
@@ -366,4 +396,28 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
|
$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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user