Created
October 7, 2012 14:30
-
-
Save mcfdn/3848540 to your computer and use it in GitHub Desktop.
Manage Singular and Plural Words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author James McFadden <[email protected]> | |
*/ | |
final class PluralHelper | |
{ | |
private static $irregularPluralForms = array( | |
'alumnus' => 'alumni', | |
'analysis' => 'analyses', | |
'appendix' => 'appendices', | |
'axis' => 'axes', | |
'barracks' => 'barracks', | |
'cactus' => 'cacti', | |
'child' => 'children', | |
'criterion' => 'criteria', | |
'deer' => 'deer', | |
'echo' => 'echoes', | |
'elf' => 'elves', | |
'embargo' => 'embargoes', | |
'focus' => 'foci', | |
'fungus' => 'fungi', | |
'goose' => 'geese', | |
'hero' => 'heroes', | |
'hoof' => 'hooves', | |
'index' => 'indices', | |
'knife' => 'knives', | |
'leaf' => 'leaves', | |
'life' => 'lives', | |
'man' => 'men', | |
'mouse' => 'mice', | |
'nucleus' => 'nuclei', | |
'person' => 'people', | |
'phenomenon' => 'phenomena', | |
'potato' => 'potatoes', | |
'radius' => 'radii', | |
'self' => 'selves', | |
'stimulus' => 'stimuli', | |
'syllabus' => 'syllabi', | |
'tomato' => 'tomatoes', | |
'torpedo' => 'torpedoes', | |
'veto' => 'vetoes', | |
'woman' => 'women' | |
); | |
/** | |
* Work out whether a word should be plural or singular | |
* | |
* This is based on the count given, and checks against | |
* some irregular plural forms | |
* | |
* @param int $count | |
* @param string $word | |
* @return string | |
*/ | |
public static function managePlural($count, $word) | |
{ | |
$lcWord = trim(strtolower($word)); | |
if($count == 1) { | |
if(in_array($lcWord, self::$irregularPluralForms)) { | |
return $count . ' ' . array_search($word, self::$irregularPluralForms); | |
} | |
return $count . ' ' . $word; | |
} else { | |
if(array_key_exists($lcWord, self::$irregularPluralForms)) { | |
return $count . ' ' . self::$irregularPluralForms[$lcWord]; | |
} else if(in_array($lcWord, self::$irregularPluralForms)) { | |
return $count . ' ' . $word; | |
} | |
return $count . ' ' . $word . 's'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment