From 259b59cae159dbd98d65a1be88b853df1060c89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Perras?= Date: Thu, 14 Jan 2010 18:14:20 -0500 Subject: [PATCH] Adding optional $reset parameter to Inflector::rules(). Fixes #91. Inflector::rules() now has an optional 3rd parameter, $reset, which can be set to true if you desire to remove the default inflections defined in Inflector. Note that the reset will only affect those inflection types which you have explicitly re-defined in the $rules parameter. --- cake/libs/inflector.php | 24 +++++++++++++++--------- cake/tests/cases/libs/inflector.test.php | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) 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 *