From df0f2295c3d9fabba6c5bcbdf46e93be6e398def Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 2 Jun 2015 23:06:06 -0400 Subject: [PATCH] Fix issue with overlapping irregular inflections. When irregular inflections overlap we should choose the longest match, not the shortest. Refs #6659 --- lib/Cake/Test/Case/Utility/InflectorTest.php | 46 ++++++++++++++++++++ lib/Cake/Utility/Inflector.php | 12 +++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/InflectorTest.php b/lib/Cake/Test/Case/Utility/InflectorTest.php index c1abaf15a..8635f39f4 100644 --- a/lib/Cake/Test/Case/Utility/InflectorTest.php +++ b/lib/Cake/Test/Case/Utility/InflectorTest.php @@ -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 * diff --git a/lib/Cake/Utility/Inflector.php b/lib/Cake/Utility/Inflector.php index 61a63c2a1..d06d4baff 100644 --- a/lib/Cake/Utility/Inflector.php +++ b/lib/Cake/Utility/Inflector.php @@ -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]; }