From eb7e10db5033c77f98ee3e6f3812d0e5c4a8182d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Perras?= Date: Thu, 14 Jan 2010 17:47:22 -0500 Subject: [PATCH] Updating Inflector::slug() handling of $map argument. Fixes #18. When passing a $map to Inflector::slug(), the $map values will now overwrite any default mappings that Inflector::slug defines. Modified pre-existing test-case for Inflector::slug() when using a $map. --- cake/libs/inflector.php | 3 +-- cake/tests/cases/libs/inflector.test.php | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cake/libs/inflector.php b/cake/libs/inflector.php index d847bbd9d..de50a8230 100644 --- a/cake/libs/inflector.php +++ b/cake/libs/inflector.php @@ -521,7 +521,6 @@ class Inflector { $map = $replacement; $replacement = '_'; } - $quotedReplacement = preg_quote($replacement, '/'); $default = array( @@ -544,7 +543,7 @@ class Inflector { sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '', ); - $map = array_merge($default, $map); + $map = array_merge($map, $default); return preg_replace(array_keys($map), array_values($map), $string); } } diff --git a/cake/tests/cases/libs/inflector.test.php b/cake/tests/cases/libs/inflector.test.php index f83cb8389..585133eec 100644 --- a/cake/tests/cases/libs/inflector.test.php +++ b/cake/tests/cases/libs/inflector.test.php @@ -236,12 +236,24 @@ class InflectorTest extends CakeTestCase { * @return void */ function testInflectorSlugWithMap() { - $result = Inflector::slug('replace every r', array('/r/' => '_')); - $expected = '_eplace_eve_y__'; + $result = Inflector::slug('replace every r', array('/r/' => '1')); + $expected = '1eplace_eve1y_1'; $this->assertEqual($result, $expected); - $result = Inflector::slug('replace every r', '_', array('/r/' => '_')); - $expected = '_eplace_eve_y__'; + $result = Inflector::slug('replace every r', '_', array('/r/' => '1')); + $expected = '1eplace_eve1y_1'; + $this->assertEqual($result, $expected); + } + +/** + * testInflectorSlugWithMapOverridingDefault method + * + * @access public + * @return void + */ + function testInflectorSlugWithMapOverridingDefault() { + $result = Inflector::slug('Testing æ ø å', '-', array('/å/' => 'aa', '/ø/' => 'oe')); + $expected = 'Testing-ae-oe-aa'; $this->assertEqual($result, $expected); }