Fix issue with overlapping irregular inflections.

When irregular inflections overlap we should choose the longest match,
not the shortest.

Refs #6659
This commit is contained in:
mark_story 2015-06-02 23:06:06 -04:00
parent 523597df43
commit df0f2295c3
2 changed files with 54 additions and 4 deletions

View file

@ -183,6 +183,29 @@ class InflectorTest extends CakeTestCase {
$this->assertEquals(Inflector::singularize(''), '');
}
/**
* Test that overlapping irregulars don't collide.
*
* @return void
*/
public function testSingularizeMultiWordIrregular() {
Inflector::rules('singular', array(
'irregular' => array(
'preguntas_frecuentes' => 'pregunta_frecuente',
'categorias_preguntas_frecuentes' => 'categoria_pregunta_frecuente',
)
));
$this->assertEquals('pregunta_frecuente', Inflector::singularize('preguntas_frecuentes'));
$this->assertEquals(
'categoria_pregunta_frecuente',
Inflector::singularize('categorias_preguntas_frecuentes')
);
$this->assertEquals(
'faq_categoria_pregunta_frecuente',
Inflector::singularize('faq_categorias_preguntas_frecuentes')
);
}
/**
* testInflectingPlurals method
*
@ -256,6 +279,29 @@ class InflectorTest extends CakeTestCase {
$this->assertEquals(Inflector::pluralize(''), '');
}
/**
* Test that overlapping irregulars don't collide.
*
* @return void
*/
public function testPluralizeMultiWordIrregular() {
Inflector::rules('plural', array(
'irregular' => array(
'pregunta_frecuente' => 'preguntas_frecuentes',
'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes',
)
));
$this->assertEquals('preguntas_frecuentes', Inflector::pluralize('pregunta_frecuente'));
$this->assertEquals(
'categorias_preguntas_frecuentes',
Inflector::pluralize('categoria_pregunta_frecuente')
);
$this->assertEquals(
'faq_categorias_preguntas_frecuentes',
Inflector::pluralize('faq_categoria_pregunta_frecuente')
);
}
/**
* testInflectingMultiWordIrregulars
*

View file

@ -386,8 +386,10 @@ class Inflector {
self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
}
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);
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);
return self::$_cache['pluralize'][$word];
}
@ -435,8 +437,10 @@ class Inflector {
self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
}
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);
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);
return self::$_cache['singularize'][$word];
}