Skip to content

Commit

Permalink
Updating Inflector::slug() handling of $map argument. Fixes #18.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jperras committed Jan 14, 2010
1 parent adb0c80 commit eb7e10d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cake/libs/inflector.php
Expand Up @@ -521,7 +521,6 @@ function slug($string, $replacement = '_', $map = array()) {
$map = $replacement;
$replacement = '_';
}

$quotedReplacement = preg_quote($replacement, '/');

$default = array(
Expand All @@ -544,7 +543,7 @@ function slug($string, $replacement = '_', $map = array()) {
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);
}
}
Expand Down
20 changes: 16 additions & 4 deletions cake/tests/cases/libs/inflector.test.php
Expand Up @@ -236,12 +236,24 @@ function testInflectorSlug() {
* @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);
}

Expand Down

0 comments on commit eb7e10d

Please sign in to comment.