Permalink
Browse files

adding tests for map param on Inflector::slug. cleaning up.

  • Loading branch information...
gwoo
gwoo committed May 1, 2009
1 parent 67d45cb commit 54a3118d3444f4adf5898314316eb3583627dc5b
Showing with 66 additions and 47 deletions.
  1. +51 −47 cake/libs/inflector.php
  2. +15 −0 cake/tests/cases/libs/inflector.test.php
View
@@ -31,7 +31,6 @@
*
* Inflector pluralizes and singularizes English nouns.
* Used by Cake's naming conventions throughout the framework.
- * Test with $i = new Inflector(); $i->test();
*
* @package cake
* @subpackage cake.cake.libs
@@ -91,7 +90,7 @@ class Inflector extends Object {
'mongoose' => 'mongooses',
'move' => 'moves',
'mythos' => 'mythoi',
- 'niche' => 'niches',
+ 'niche' => 'niches',
'numen' => 'numina',
'occiput' => 'occiputs',
'octopus' => 'octopuses',
@@ -150,8 +149,8 @@ class Inflector extends Object {
'/s$/i' => ''
),
'uninflected' => array(
- '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
- ),
+ '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
+ ),
'irregular' => array()
);
@@ -161,19 +160,19 @@ class Inflector extends Object {
* @var array
* @access protected
**/
- var $_uninflected = array(
- 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
- 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
- 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
- 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
- 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
- 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media',
- 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
- 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
- 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
- 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
- 'trousers', 'trout','tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
- 'Yengeese'
+ var $_uninflected = array(
+ 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
+ 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
+ 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
+ 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
+ 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
+ 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media',
+ 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
+ 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
+ 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
+ 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
+ 'trousers', 'trout','tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
+ 'Yengeese'
);
/**
@@ -212,27 +211,27 @@ function &getInstance() {
*
* @param string $type The type of inflection, either 'singular' or 'plural'
* @param array $rules Array of rules to be added. Example usage:
- * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
- * Inflector::rules('plural', array(
- * 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
- * 'uninflected' => array('dontinflectme'),
- * 'irregular' => array('red' => 'redlings')
- * ));
+ * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
+ * Inflector::rules('plural', array(
+ * 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
+ * 'uninflected' => array('dontinflectme'),
+ * 'irregular' => array('red' => 'redlings')
+ * ));
* @access public
* @return void
* @static
*/
function rules($type, $rules = array()) {
$_this =& Inflector::getInstance();
- $type = '_'.$type;
+ $type = '_'.$type;
- foreach ($rules as $rule => $pattern) {
- if (is_array($pattern)) {
- $_this->{$type}[$rule] = array_merge($pattern, $_this->{$type}[$rule]);
- unset($rules[$rule], $_this->{$type}['cache' . ucfirst($rule)], $_this->{$type}['merged'][$rule]);
- }
- }
- $_this->{$type}['rules'] = array_merge($rules, $_this->{$type}['rules']);
+ foreach ($rules as $rule => $pattern) {
+ if (is_array($pattern)) {
+ $_this->{$type}[$rule] = array_merge($pattern, $_this->{$type}[$rule]);
+ unset($rules[$rule], $_this->{$type}['cache' . ucfirst($rule)], $_this->{$type}['merged'][$rule]);
+ }
+ }
+ $_this->{$type}['rules'] = array_merge($rules, $_this->{$type}['rules']);
}
@@ -252,15 +251,15 @@ function pluralize($word) {
return $_this->_pluralized[$word];
}
- if (!isset($_this->_plural['merged']['irregular'])) {
- $_this->_plural['merged']['irregular'] = $_this->_plural['irregular'];
- }
+ if (!isset($_this->_plural['merged']['irregular'])) {
+ $_this->_plural['merged']['irregular'] = $_this->_plural['irregular'];
+ }
- if (!isset($_this->plural['merged']['uninflected'])) {
- $_this->_plural['merged']['uninflected'] = array_merge($_this->_plural['uninflected'], $_this->_uninflected);
- }
+ if (!isset($_this->plural['merged']['uninflected'])) {
+ $_this->_plural['merged']['uninflected'] = array_merge($_this->_plural['uninflected'], $_this->_uninflected);
+ }
- if (!isset($_this->_plural['cacheUninflected']) || !isset($_this->_plural['cacheIrregular'])) {
+ if (!isset($_this->_plural['cacheUninflected']) || !isset($_this->_plural['cacheIrregular'])) {
$_this->_plural['cacheUninflected'] = '(?:' . join( '|', $_this->_plural['merged']['uninflected']) . ')';
$_this->_plural['cacheIrregular'] = '(?:' . join( '|', array_keys($_this->_plural['merged']['irregular'])) . ')';
}
@@ -299,17 +298,17 @@ function singularize($word) {
return $_this->_singularized[$word];
}
- if (!isset($_this->_singular['merged']['uninflected'])) {
- $_this->_singular['merged']['uninflected'] = array_merge($_this->_singular['uninflected'], $_this->_uninflected);
- }
+ if (!isset($_this->_singular['merged']['uninflected'])) {
+ $_this->_singular['merged']['uninflected'] = array_merge($_this->_singular['uninflected'], $_this->_uninflected);
+ }
- if (!isset($_this->_singular['merged']['irregular'])) {
- $_this->_singular['merged']['irregular'] = array_merge($_this->_singular['irregular'], array_flip($_this->_plural['irregular']));
- }
+ if (!isset($_this->_singular['merged']['irregular'])) {
+ $_this->_singular['merged']['irregular'] = array_merge($_this->_singular['irregular'], array_flip($_this->_plural['irregular']));
+ }
if (!isset($_this->_singular['cacheUninflected']) || !isset($_this->_singular['cacheIrregular'])) {
$_this->_singular['cacheUninflected'] = '(?:' . join( '|', $_this->_singular['merged']['uninflected']) . ')';
- $_this->_singular['cacheIrregular'] = '(?:' . join( '|', array_keys($_this->_singular['merged']['irregular'])) . ')';
+ $_this->_singular['cacheIrregular'] = '(?:' . join( '|', array_keys($_this->_singular['merged']['irregular'])) . ')';
}
if (preg_match('/(.*)\\b(' . $_this->_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
@@ -426,7 +425,12 @@ function variable($string) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function slug($string, $replacement = '_', $map = array()) {
- $quotedReplacement = preg_quote($replacement, '/');
+ if (is_array($replacement)) {
+ $map = $replacement;
+ $replacement = '_';
+ }
+
+ $quotedReplacement = preg_quote($replacement, '/');
$default = array(
'/à|á|å|â/' => 'a',
@@ -445,7 +449,7 @@ function slug($string, $replacement = '_', $map = array()) {
'/ß/' => 'ss',
'/[^\w\s]/' => ' ',
'/\\s+/' => $replacement,
- sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
+ sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
);
$map = array_merge($default, $map);
return preg_replace(array_keys($map), array_values($map), $string);
@@ -201,6 +201,21 @@ function testInflectorSlug() {
$expected = 'this-melts-your-face1-2-3';
$this->assertEqual($result, $expected);
}
+/**
+ * testInflectorSlugWithMap method
+ *
+ * @access public
+ * @return void
+ */
+ function testInflectorSlugWithMap() {
+ $result = Inflector::slug('replace every r', array('/r/' => '_'));
+ $expected = '_eplace_eve_y__';
+ $this->assertEqual($result, $expected);
+
+ $result = Inflector::slug('replace every r', '_', array('/r/' => '_'));
+ $expected = '_eplace_eve_y__';
+ $this->assertEqual($result, $expected);
+ }
/**
* testVariableNaming method
*

0 comments on commit 54a3118

Please sign in to comment.