inflector.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php namespace Laravel;
  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. * Get the plural form of a word if the specified count is greater than one.
  110. *
  111. * <code>
  112. * // Returns "friend"
  113. * Inflector::plural_if('friend', 1);
  114. *
  115. * // Returns "friends"
  116. * Inflector::plural_if('friend', 2);
  117. * </code>
  118. *
  119. * @param string $value
  120. * @param int $count
  121. * @return string
  122. */
  123. public static function plural_if($value, $count)
  124. {
  125. return ($count > 1) ? static::plural($value) : $value;
  126. }
  127. /**
  128. * Convert a word to its plural form.
  129. *
  130. * <code>
  131. * // Returns "friends"
  132. * Inflector::plural('friend');
  133. *
  134. * // Returns "children"
  135. * Inflector::plural('child');
  136. * </code>
  137. *
  138. * @param string $value
  139. * @return string
  140. */
  141. public static function plural($value)
  142. {
  143. $irregular = array_flip(static::$irregular);
  144. return static::$plural_cache[$value] = static::inflect($value, static::$plural_cache, $irregular, static::$plural);
  145. }
  146. /**
  147. * Convert a word to its singular form.
  148. *
  149. * <code>
  150. * // Returns "friend"
  151. * Inflector::singular('friends');
  152. *
  153. * // Returns "child"
  154. * Inflector::singular('children');
  155. * </code>
  156. *
  157. * @param string $value
  158. * @return string
  159. */
  160. public static function singular($value)
  161. {
  162. return static::$singular_cache[$value] = static::inflect($value, static::$singular_cache, static::$irregular, static::$singular);
  163. }
  164. /**
  165. * Convert a word to its singular or plural form.
  166. *
  167. * @param string $value
  168. * @param array $cache
  169. * @param array $irregular
  170. * @param array $source
  171. * @return string
  172. */
  173. private static function inflect($value, $cache, $irregular, $source)
  174. {
  175. if (array_key_exists($value, $cache))
  176. {
  177. return $cache[$value];
  178. }
  179. if (in_array(strtolower($value), static::$uncountable))
  180. {
  181. return $value;
  182. }
  183. foreach ($irregular as $irregular => $pattern)
  184. {
  185. if (preg_match($pattern = '/'.$pattern.'$/i', $value))
  186. {
  187. return preg_replace($pattern, $irregular, $value);
  188. }
  189. }
  190. foreach ($source as $pattern => $inflected)
  191. {
  192. if (preg_match($pattern, $value))
  193. {
  194. return preg_replace($pattern, $inflected, $value);
  195. }
  196. }
  197. return $value;
  198. }
  199. }