diff --git a/cake/libs/inflector.php b/cake/libs/inflector.php index de50a8230..4d18c2eff 100644 --- a/cake/libs/inflector.php +++ b/cake/libs/inflector.php @@ -273,24 +273,30 @@ class Inflector { * * @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') + * )); + * @param boolean $reset If true, will unset default inflections for all + * new rules that are being defined in $rules. * @access public * @return void * @static */ - function rules($type, $rules = array()) { + function rules($type, $rules, $reset = false) { $_this =& Inflector::getInstance(); $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]); + if ($reset) { + $_this->{$type}[$rule] = $pattern; + } else { + $_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']); diff --git a/cake/tests/cases/libs/inflector.test.php b/cake/tests/cases/libs/inflector.test.php index 585133eec..ac32d3385 100644 --- a/cake/tests/cases/libs/inflector.test.php +++ b/cake/tests/cases/libs/inflector.test.php @@ -381,6 +381,29 @@ class InflectorTest extends CakeTestCase { $this->assertEqual(Inflector::singularize('singulars'), 'singulars'); } + function testCustomRuleWithReset() { + $uninflected = array('atlas', 'lapis', 'onibus', 'pires', 'virus', '.*x'); + $pluralIrregular = array('as' => 'ases'); + + Inflector::rules('singular', array( + 'rules' => array('/^(.*)(a|e|o|u)is$/i' => '\1\2l'), + 'uninflected' => $uninflected, + ), true); + + Inflector::rules('plural', array( + 'rules' => array( + '/^(.*)(a|e|o|u)l$/i' => '\1\2is', + ), + 'uninflected' => $uninflected, + 'irregular' => $pluralIrregular + ), true); + + $this->assertEqual(Inflector::pluralize('Alcool'), 'Alcoois'); + $this->assertEqual(Inflector::pluralize('Atlas'), 'Atlas'); + $this->assertEqual(Inflector::singularize('Alcoois'), 'Alcool'); + $this->assertEqual(Inflector::singularize('Atlas'), 'Atlas'); + } + /** * tearDown method *