inflector.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. '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. 'equipment',
  99. 'data',
  100. 'deer',
  101. 'fish',
  102. 'information',
  103. 'money',
  104. 'rice',
  105. 'series',
  106. 'sheep',
  107. 'species',
  108. );
  109. /**
  110. * Get the plural form of a word if the specified count is greater than one.
  111. *
  112. * @param string $value
  113. * @param int $count
  114. * @return string
  115. */
  116. public static function plural_if($value, $count)
  117. {
  118. return ($count > 1) ? static::plural($value) : $value;
  119. }
  120. /**
  121. * Convert a word to its plural form.
  122. *
  123. * @param string $value
  124. * @return string
  125. */
  126. public static function plural($value)
  127. {
  128. $irregular = array_flip(static::$irregular);
  129. return static::$plural_cache[$value] = static::inflect($value, static::$plural_cache, $irregular, static::$plural);
  130. }
  131. /**
  132. * Convert a word to its singular form.
  133. *
  134. * @param string $value
  135. * @return string
  136. */
  137. public static function singular($value)
  138. {
  139. return static::$singular_cache[$value] = static::inflect($value, static::$singular_cache, static::$irregular, static::$singular);
  140. }
  141. /**
  142. * Convert a word to its singular or plural form.
  143. *
  144. * @param string $value
  145. * @param array $cache
  146. * @param array $irregular
  147. * @param array $source
  148. * @return string
  149. */
  150. private static function inflect($value, $cache, $irregular, $source)
  151. {
  152. if (array_key_exists($value, $cache))
  153. {
  154. return $cache[$value];
  155. }
  156. if (in_array(strtolower($value), static::$uncountable))
  157. {
  158. return $value;
  159. }
  160. foreach ($irregular as $irregular => $pattern)
  161. {
  162. if (preg_match($pattern = '/'.$pattern.'$/i', $value))
  163. {
  164. return preg_replace($pattern, $irregular, $value);
  165. }
  166. }
  167. foreach ($source as $pattern => $inflected)
  168. {
  169. if (preg_match($pattern, $value))
  170. {
  171. return preg_replace($pattern, $inflected, $value);
  172. }
  173. }
  174. return $value;
  175. }
  176. }