inflector.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php namespace Laravel;
  2. class Inflector {
  3. /**
  4. * The words that have been converted to singular.
  5. *
  6. * @var array
  7. */
  8. protected static $singular_cache = array();
  9. /**
  10. * The words that have been converted to plural.
  11. *
  12. * @var array
  13. */
  14. protected 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. 'child' => 'children',
  84. 'foot' => 'feet',
  85. 'goose' => 'geese',
  86. 'man' => 'men',
  87. 'move' => 'moves',
  88. 'person' => 'people',
  89. 'sex' => 'sexes',
  90. 'tooth' => 'teeth',
  91. );
  92. /**
  93. * Uncountable word forms.
  94. *
  95. * @var array
  96. */
  97. private static $uncountable = array(
  98. 'audio',
  99. 'equipment',
  100. 'deer',
  101. 'fish',
  102. 'gold',
  103. 'information',
  104. 'money',
  105. 'rice',
  106. 'police',
  107. 'series',
  108. 'sheep',
  109. 'species',
  110. );
  111. /**
  112. * Convert a word to its plural form.
  113. *
  114. * Optionally, a count argument may be provided. If the count is greater than
  115. * one, the word will be pluralized, otherwise the word will be returned from
  116. * the method unchanged.
  117. *
  118. * <code>
  119. * // Get the plural form of the word "child"
  120. * $children = Inflector::plural('child');
  121. *
  122. * // Returns "comments"
  123. * $comments = Inflector::plural('comment', 10);
  124. *
  125. * // Returns "comment"
  126. * $comment = Inflector::plural('comment', 1);
  127. * </code>
  128. *
  129. * @param string $value
  130. * @param int $count
  131. * @return string
  132. */
  133. public static function plural($value, $count = null)
  134. {
  135. if ( ! is_null($count) and $count == 1) return $value;
  136. $irregular = array_flip(static::$irregular);
  137. $plural = static::inflect($value, static::$plural_cache, $irregular, static::$plural);
  138. return static::$plural_cache[$value] = $plural;
  139. }
  140. /**
  141. * Convert a word to its singular form.
  142. *
  143. * @param string $value
  144. * @return string
  145. */
  146. public static function singular($value)
  147. {
  148. $singular = static::inflect($value, static::$singular_cache, static::$irregular, static::$singular);
  149. return static::$singular_cache[$value] = $singular;
  150. }
  151. /**
  152. * Convert a word to its singular or plural form.
  153. *
  154. * @param string $value
  155. * @param array $cache
  156. * @param array $irregular
  157. * @param array $source
  158. * @return string
  159. */
  160. protected static function inflect($value, $cache, $irregular, $source)
  161. {
  162. if (array_key_exists($value, $cache))
  163. {
  164. return $cache[$value];
  165. }
  166. if (in_array(strtolower($value), static::$uncountable))
  167. {
  168. return $value;
  169. }
  170. foreach ($irregular as $irregular => $pattern)
  171. {
  172. if (preg_match($pattern = '/'.$pattern.'$/i', $value))
  173. {
  174. return preg_replace($pattern, $irregular, $value);
  175. }
  176. }
  177. foreach ($source as $pattern => $inflected)
  178. {
  179. if (preg_match($pattern, $value))
  180. {
  181. return preg_replace($pattern, $inflected, $value);
  182. }
  183. }
  184. return $value;
  185. }
  186. }