Skip to content

Commit

Permalink
Adding optional $reset parameter to Inflector::rules(). Fixes #91.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jperras committed Jan 14, 2010
1 parent eb7e10d commit 259b59c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cake/libs/inflector.php
Expand Up @@ -273,24 +273,30 @@ function _cache($type, $key, $value = false) {
*
* @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']);
Expand Down
23 changes: 23 additions & 0 deletions cake/tests/cases/libs/inflector.test.php
Expand Up @@ -381,6 +381,29 @@ function testCustomSingularRule() {
$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
*
Expand Down

0 comments on commit 259b59c

Please sign in to comment.