Merge pull request #6544 from ndm2/2.6-multi-word-irregulars

Add underscore support for multi word irregulars.
This commit is contained in:
Mark Story 2015-05-12 12:16:10 -04:00
commit 918c20c4cd
2 changed files with 27 additions and 2 deletions

View file

@ -256,6 +256,31 @@ class InflectorTest extends CakeTestCase {
$this->assertEquals(Inflector::pluralize(''), ''); $this->assertEquals(Inflector::pluralize(''), '');
} }
/**
* testInflectingMultiWordIrregulars
*
* @return void
*/
public function testInflectingMultiWordIrregulars() {
// unset the default rules in order to avoid them possibly matching
// the words in case the irregular regex won't match, the tests
// should fail in that case
Inflector::rules('plural', array(
'rules' => array(),
));
Inflector::rules('singular', array(
'rules' => array(),
));
$this->assertEquals(Inflector::singularize('wisdom teeth'), 'wisdom tooth');
$this->assertEquals(Inflector::singularize('wisdom-teeth'), 'wisdom-tooth');
$this->assertEquals(Inflector::singularize('wisdom_teeth'), 'wisdom_tooth');
$this->assertEquals(Inflector::pluralize('sweet potato'), 'sweet potatoes');
$this->assertEquals(Inflector::pluralize('sweet-potato'), 'sweet-potatoes');
$this->assertEquals(Inflector::pluralize('sweet_potato'), 'sweet_potatoes');
}
/** /**
* testInflectorSlug method * testInflectorSlug method
* *

View file

@ -386,7 +386,7 @@ class Inflector {
self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')'; self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
} }
if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) { if (preg_match('/(.*(?:\\b|_))(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
self::$_cache['pluralize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1); self::$_cache['pluralize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
return self::$_cache['pluralize'][$word]; return self::$_cache['pluralize'][$word];
} }
@ -435,7 +435,7 @@ class Inflector {
self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')'; self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
} }
if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) { if (preg_match('/(.*(?:\\b|_))(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
self::$_cache['singularize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1); self::$_cache['singularize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
return self::$_cache['singularize'][$word]; return self::$_cache['singularize'][$word];
} }