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.
This commit is contained in:
Joël Perras 2010-01-14 17:47:22 -05:00
parent adb0c809a0
commit eb7e10db50
2 changed files with 17 additions and 6 deletions

View file

@ -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);
}
}

View file

@ -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);
}