From 934ad43915b47de63b89199d537b2b8137a54ba9 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 3 Jun 2015 22:16:12 -0400 Subject: [PATCH] Port changes from #6723 to 3.0 Fix overlapping irregular inflections. --- src/Utility/Inflector.php | 4 +-- tests/TestCase/Utility/InflectorTest.php | 44 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Utility/Inflector.php b/src/Utility/Inflector.php index eb3b340a141..e41bd748ba2 100644 --- a/src/Utility/Inflector.php +++ b/src/Utility/Inflector.php @@ -511,7 +511,7 @@ public static function pluralize($word) static::$_cache['irregular']['pluralize'] = '(?:' . implode('|', array_keys(static::$_irregular)) . ')'; } - if (preg_match('/(.*(?:\\b|_))(' . static::$_cache['irregular']['pluralize'] . ')$/i', $word, $regs)) { + if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['pluralize'] . ')$/i', $word, $regs)) { static::$_cache['pluralize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(static::$_irregular[strtolower($regs[2])], 1); return static::$_cache['pluralize'][$word]; @@ -551,7 +551,7 @@ public static function singularize($word) static::$_cache['irregular']['singular'] = '(?:' . implode('|', static::$_irregular) . ')'; } - if (preg_match('/(.*(?:\\b|_))(' . static::$_cache['irregular']['singular'] . ')$/i', $word, $regs)) { + if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['singular'] . ')$/i', $word, $regs)) { static::$_cache['singularize'][$word] = $regs[1] . substr($regs[2], 0, 1) . substr(array_search(strtolower($regs[2]), static::$_irregular), 1); return static::$_cache['singularize'][$word]; diff --git a/tests/TestCase/Utility/InflectorTest.php b/tests/TestCase/Utility/InflectorTest.php index 2ccfe76bafa..bc4b123aa11 100644 --- a/tests/TestCase/Utility/InflectorTest.php +++ b/tests/TestCase/Utility/InflectorTest.php @@ -179,6 +179,28 @@ public function testInflectingSingulars() $this->assertEquals('', Inflector::singularize('')); } + /** + * Test that overlapping irregulars don't collide. + * + * @return void + */ + public function testSingularizeMultiWordIrregular() + { + Inflector::rules('irregular', [ + 'pregunta_frecuente' => 'preguntas_frecuentes', + 'categoria_pregunta_frecuente' => 'categorias_preguntas_frecuentes', + ]); + $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 * @@ -258,6 +280,28 @@ public function testInflectingPlurals() $this->assertEquals('', Inflector::pluralize('')); } + /** + * Test that overlapping irregulars don't collide. + * + * @return void + */ + public function testPluralizeMultiWordIrregular() + { + Inflector::rules('irregular', [ + '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 *