/** * @dataProvider fixLetterCaseProvider() * @depends testPluralize() * @depends testSingularize() */ public function testFixLetterCase($a, $b, $pluralise) { # We disable simplification for this test to ensure that we do not split # as camel case when testing with silly capitalisation. if ($pluralise) { $this->assertSame($b, Inflector::pluralize($a, false)); } else { $this->assertSame($b, Inflector::singularize($a, false)); } }
/** * Takes a string or phrase, and pluralizes the last word. If there is a * number in the phrase, conditionally pluralize the last word based on the * number. * * @see http://thecodingway.com/blog/robs-tips-and-tricks/php-pluralizing-with-style/ * * @param string $string The string to pluralize. * * @return string */ public static function pluralize($string) { # Regular expression for matching textual number. static $regex0 = '/(?:^|)((?<!point )(?:minus )?one(?! point)).*?(\\pL+)\\PL*$/'; # Regular expression for matching normal number. static $regex1 = '/(?:^|)(-?(?<!(?:\\.|\\d))1(?:\\.0+)?)?\\PN*?(\\pL+)\\PL*$/'; # Reguular expression for word replacement. static $regex2 = '/(\\PL|one |^)(\\pL+)(\\PL*)$/'; if (!preg_match($regex0, $string, $match)) { if (!preg_match($regex1, $string, $match)) { # We had an error matching the phrase, assume we cannot pluralize it. return $string; } } array_shift($match); list($amount, $word) = $match; if ($word && !$amount) { $word = Inflector::pluralize($word); $string = preg_replace($regex2, "\$1{$word}\$3", $string); } return $string; }