Skip to content

Commit

Permalink
Port changes from #6723 to 3.0
Browse files Browse the repository at this point in the history
Fix overlapping irregular inflections.
  • Loading branch information
markstory committed Jun 4, 2015
1 parent aa0907c commit 934ad43
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Utility/Inflector.php
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase/Utility/InflectorTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 934ad43

Please sign in to comment.