inflector.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php namespace System;
  2. class Inflector {
  3. /**
  4. * The words that have been converted to singular.
  5. *
  6. * @var array
  7. */
  8. private static $singular_cache = array();
  9. /**
  10. * The words that have been converted to plural.
  11. *
  12. * @var array
  13. */
  14. private static $plural_cache = array();
  15. /**
  16. * Plural word forms.
  17. *
  18. * @var array
  19. */
  20. private static $plural = array(
  21. '/(quiz)$/i' => "$1zes",
  22. '/^(ox)$/i' => "$1en",
  23. '/([m|l])ouse$/i' => "$1ice",
  24. '/(matr|vert|ind)ix|ex$/i' => "$1ices",
  25. '/(x|ch|ss|sh)$/i' => "$1es",
  26. '/([^aeiouy]|qu)y$/i' => "$1ies",
  27. '/(hive)$/i' => "$1s",
  28. '/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
  29. '/(shea|lea|loa|thie)f$/i' => "$1ves",
  30. '/sis$/i' => "ses",
  31. '/([ti])um$/i' => "$1a",
  32. '/(tomat|potat|ech|her|vet)o$/i' => "$1oes",
  33. '/(bu)s$/i' => "$1ses",
  34. '/(alias)$/i' => "$1es",
  35. '/(octop)us$/i' => "$1i",
  36. '/(ax|test)is$/i' => "$1es",
  37. '/(us)$/i' => "$1es",
  38. '/s$/i' => "s",
  39. '/$/' => "s"
  40. );
  41. /**
  42. * Singular word forms.
  43. *
  44. * @var array
  45. */
  46. private static $singular = array(
  47. '/(quiz)zes$/i' => "$1",
  48. '/(matr)ices$/i' => "$1ix",
  49. '/(vert|ind)ices$/i' => "$1ex",
  50. '/^(ox)en$/i' => "$1",
  51. '/(alias)es$/i' => "$1",
  52. '/(octop|vir)i$/i' => "$1us",
  53. '/(cris|ax|test)es$/i' => "$1is",
  54. '/(shoe)s$/i' => "$1",
  55. '/(o)es$/i' => "$1",
  56. '/(bus)es$/i' => "$1",
  57. '/([m|l])ice$/i' => "$1ouse",
  58. '/(x|ch|ss|sh)es$/i' => "$1",
  59. '/(m)ovies$/i' => "$1ovie",
  60. '/(s)eries$/i' => "$1eries",
  61. '/([^aeiouy]|qu)ies$/i' => "$1y",
  62. '/([lr])ves$/i' => "$1f",
  63. '/(tive)s$/i' => "$1",
  64. '/(hive)s$/i' => "$1",
  65. '/(li|wi|kni)ves$/i' => "$1fe",
  66. '/(shea|loa|lea|thie)ves$/i' => "$1f",
  67. '/(^analy)ses$/i' => "$1sis",
  68. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
  69. '/([ti])a$/i' => "$1um",
  70. '/(n)ews$/i' => "$1ews",
  71. '/(h|bl)ouses$/i' => "$1ouse",
  72. '/(corpse)s$/i' => "$1",
  73. '/(us)es$/i' => "$1",
  74. '/(us|ss)$/i' => "$1",
  75. '/s$/i' => "",
  76. );
  77. /**
  78. * Irregular word forms.
  79. *
  80. * @var array
  81. */
  82. private static $irregular = array(
  83. 'move' => 'moves',
  84. 'foot' => 'feet',
  85. 'goose' => 'geese',
  86. 'sex' => 'sexes',
  87. 'child' => 'children',
  88. 'man' => 'men',
  89. 'tooth' => 'teeth',
  90. 'person' => 'people',
  91. );
  92. /**
  93. * Uncountable word forms.
  94. *
  95. * @var array
  96. */
  97. private static $uncountable = array(
  98. 'sheep',
  99. 'fish',
  100. 'deer',
  101. 'series',
  102. 'species',
  103. 'money',
  104. 'rice',
  105. 'information',
  106. 'equipment',
  107. );
  108. /**
  109. * Convert a word to its plural form.
  110. *
  111. * @param string $value
  112. * @return string
  113. */
  114. public static function plural($value)
  115. {
  116. if (array_key_exists($value, static::$plural_cache))
  117. {
  118. return static::$plural_cache[$value];
  119. }
  120. // -----------------------------------------------------
  121. // Are the singular and plural forms the same?
  122. // -----------------------------------------------------
  123. if (in_array(Str::lower($value), static::$uncountable))
  124. {
  125. return static::$plural_cache[$value] = $value;
  126. }
  127. // -----------------------------------------------------
  128. // Is the plural form irregular?
  129. // -----------------------------------------------------
  130. foreach (static::$irregular as $pattern => $irregular)
  131. {
  132. $pattern = '/'.$pattern.'$/i';
  133. if (preg_match($pattern, $value))
  134. {
  135. return static::$plural_cache[$value] = preg_replace($pattern, $irregular, $value);
  136. }
  137. }
  138. // -----------------------------------------------------
  139. // Check the plural forms for matches.
  140. // -----------------------------------------------------
  141. foreach (static::$plural as $pattern => $plural)
  142. {
  143. if (preg_match($pattern, $value))
  144. {
  145. return static::$plural_cache[$value] = preg_replace($pattern, $plural, $value);
  146. }
  147. }
  148. return static::$plural_cache[$value] = $value;
  149. }
  150. /**
  151. * Convert a word to its singular form.
  152. *
  153. * @param string $value
  154. * @return string
  155. */
  156. public static function singular($value)
  157. {
  158. if (array_key_exists($value, static::$singular_cache))
  159. {
  160. return static::$singular_cache[$value];
  161. }
  162. // -----------------------------------------------------
  163. // Are the singular and plural forms the same?
  164. // -----------------------------------------------------
  165. if (in_array(Str::lower($value), static::$uncountable))
  166. {
  167. return static::$singular_cache[$value] = $value;
  168. }
  169. // -----------------------------------------------------
  170. // Is the plural form irregular?
  171. // -----------------------------------------------------
  172. foreach (static::$irregular as $irregular => $pattern)
  173. {
  174. $pattern = '/'.$pattern.'$/i';
  175. if (preg_match($pattern, $value))
  176. {
  177. return static::$singular_cache[$value] = preg_replace($pattern, $irregular, $value);
  178. }
  179. }
  180. // -----------------------------------------------------
  181. // Check the singular forms for matches.
  182. // -----------------------------------------------------
  183. foreach (static::$singular as $pattern => $singular)
  184. {
  185. if (preg_match($pattern, $value))
  186. {
  187. return static::$singular_cache[$value] = preg_replace($pattern, $singular, $value);
  188. }
  189. }
  190. return static::$singular_cache[$value] = $value;
  191. }
  192. /**
  193. * Get the plural form of a word if the count is greater than zero.
  194. *
  195. * @param string $value
  196. * @param int $count
  197. * @return string
  198. */
  199. public static function plural_if($value, $count)
  200. {
  201. return ($count > 1) ? static::plural($value) : $value;
  202. }
  203. }