mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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.
This commit is contained in:
parent
eb7e10db50
commit
259b59cae1
2 changed files with 38 additions and 9 deletions
|
@ -273,24 +273,30 @@ class Inflector {
|
||||||
*
|
*
|
||||||
* @param string $type The type of inflection, either 'singular' or 'plural'
|
* @param string $type The type of inflection, either 'singular' or 'plural'
|
||||||
* @param array $rules Array of rules to be added. Example usage:
|
* @param array $rules Array of rules to be added. Example usage:
|
||||||
* Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
|
* Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
|
||||||
* Inflector::rules('plural', array(
|
* Inflector::rules('plural', array(
|
||||||
* 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
|
* 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
|
||||||
* 'uninflected' => array('dontinflectme'),
|
* 'uninflected' => array('dontinflectme'),
|
||||||
* 'irregular' => array('red' => 'redlings')
|
* '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
|
* @access public
|
||||||
* @return void
|
* @return void
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
function rules($type, $rules = array()) {
|
function rules($type, $rules, $reset = false) {
|
||||||
$_this =& Inflector::getInstance();
|
$_this =& Inflector::getInstance();
|
||||||
$type = '_'.$type;
|
$type = '_'.$type;
|
||||||
|
|
||||||
foreach ($rules as $rule => $pattern) {
|
foreach ($rules as $rule => $pattern) {
|
||||||
if (is_array($pattern)) {
|
if (is_array($pattern)) {
|
||||||
$_this->{$type}[$rule] = array_merge($pattern, $_this->{$type}[$rule]);
|
if ($reset) {
|
||||||
unset($rules[$rule], $_this->{$type}['cache' . ucfirst($rule)], $_this->{$type}['merged'][$rule]);
|
$_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']);
|
$_this->{$type}['rules'] = array_merge($rules, $_this->{$type}['rules']);
|
||||||
|
|
|
@ -381,6 +381,29 @@ class InflectorTest extends CakeTestCase {
|
||||||
$this->assertEqual(Inflector::singularize('singulars'), 'singulars');
|
$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
|
* tearDown method
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue