Skip to content
Permalink
Browse files

Fix issue with overlapping irregular inflections.

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

Refs #6659
  • Loading branch information...
markstory committed Jun 3, 2015
1 parent 523597d commit df0f2295c3d9fabba6c5bcbdf46e93be6e398def
Showing with 54 additions and 4 deletions.
  1. +46 −0 lib/Cake/Test/Case/Utility/InflectorTest.php
  2. +8 −4 lib/Cake/Utility/Inflector.php
@@ -183,6 +183,29 @@ public function testInflectingSingulars() {
$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 @@ public function testInflectingPlurals() {
$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
*
@@ -386,8 +386,10 @@ public static function pluralize($word) {
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 @@ public static function singularize($word) {
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];
}

0 comments on commit df0f229

Please sign in to comment.
You can’t perform that action at this time.