/classes/cyclone/Inflector.php

https://github.com/cyclonephp/cyclone · PHP · 238 lines · 102 code · 30 blank · 106 comment · 13 complexity · 56e5060b2bf326e306916984cd84e909 MD5 · raw file

  1. <?php
  2. namespace cyclone;
  3. /**
  4. * Inflector helper class. Inflection is changing the form of a word based on
  5. * the context it is used in. For example, changing a word into a plural form.
  6. *
  7. * [!!] Inflection is only tested with English, and is will not work with other languages.
  8. *
  9. * @package cyclone
  10. * @category Helpers
  11. * @author Kohana Team
  12. * @copyright (c) 2007-2009 Kohana Team
  13. * @license http://kohanaphp.com/license
  14. */
  15. class Inflector {
  16. // Cached inflections
  17. protected static $cache = array();
  18. // Uncountable and irregular words
  19. protected static $uncountable;
  20. protected static $irregular;
  21. /**
  22. * Checks if a word is defined as uncountable. An uncountable word has a
  23. * single form. For instance, one "fish" and many "fish", not "fishes".
  24. *
  25. * Inflector::uncountable('fish'); // TRUE
  26. * Inflector::uncountable('cat'); // FALSE
  27. *
  28. * If you find a word is being pluralized improperly, it has probably not
  29. * been defined as uncountable in `config/inflector.php`. If this is the
  30. * case, please report [an issue](http://dev.kohanaphp.com/projects/kohana3/issues).
  31. *
  32. * @param string word to check
  33. * @return boolean
  34. */
  35. public static function uncountable($str)
  36. {
  37. if (Inflector::$uncountable === NULL)
  38. {
  39. // Cache uncountables
  40. Inflector::$uncountable = Kohana::config('inflector')->uncountable;
  41. // Make uncountables mirrored
  42. Inflector::$uncountable = array_combine(Inflector::$uncountable, Inflector::$uncountable);
  43. }
  44. return isset(Inflector::$uncountable[strtolower($str)]);
  45. }
  46. /**
  47. * Makes a plural word singular.
  48. *
  49. * echo Inflector::singular('cats'); // "cat"
  50. * echo Inflector::singular('fish'); // "fish", uncountable
  51. *
  52. * You can also provide the count to make inflection more intelligent.
  53. * In this case, it will only return the singular value if the count is
  54. * greater than one and not zero.
  55. *
  56. * echo Inflector::singular('cats', 2); // "cats"
  57. *
  58. * [!!] Special inflections are defined in `config/inflector.php`.
  59. *
  60. * @param string word to singularize
  61. * @param integer number of things
  62. * @return string
  63. * @uses Inflector::uncountable
  64. */
  65. public static function singular($str, $count = NULL)
  66. {
  67. // Remove garbage
  68. $str = strtolower(trim($str));
  69. if (is_string($count))
  70. {
  71. // Convert to integer when using a digit string
  72. $count = (int) $count;
  73. }
  74. // Do nothing with a single count
  75. if ($count === 0 OR $count > 1)
  76. return $str;
  77. // Cache key name
  78. $key = 'singular_'.$str.$count;
  79. if (isset(Inflector::$cache[$key]))
  80. return Inflector::$cache[$key];
  81. if (Inflector::uncountable($str))
  82. return Inflector::$cache[$key] = $str;
  83. if (empty(Inflector::$irregular))
  84. {
  85. // Cache irregular words
  86. Inflector::$irregular = Kohana::config('inflector')->irregular;
  87. }
  88. if ($irregular = array_search($str, Inflector::$irregular))
  89. {
  90. $str = $irregular;
  91. }
  92. elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
  93. {
  94. // Remove "es"
  95. $str = substr($str, 0, -2);
  96. }
  97. elseif (preg_match('/[^aeiou]ies$/', $str))
  98. {
  99. $str = substr($str, 0, -3).'y';
  100. }
  101. elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
  102. {
  103. $str = substr($str, 0, -1);
  104. }
  105. return Inflector::$cache[$key] = $str;
  106. }
  107. /**
  108. * Makes a singular word plural.
  109. *
  110. * echo Inflector::plural('fish'); // "fish", uncountable
  111. * echo Inflector::plural('cat'); // "cats"
  112. *
  113. * You can also provide the count to make inflection more intelligent.
  114. * In this case, it will only return the plural value if the count is
  115. * not one.
  116. *
  117. * echo Inflector::singular('cats', 3); // "cats"
  118. *
  119. * [!!] Special inflections are defined in `config/inflector.php`.
  120. *
  121. * @param string word to pluralize
  122. * @return string
  123. * @uses Inflector::uncountable
  124. */
  125. public static function plural($str, $count = NULL)
  126. {
  127. // Remove garbage
  128. $str = strtolower(trim($str));
  129. if (is_string($count))
  130. {
  131. // Convert to integer when using a digit string
  132. $count = (int) $count;
  133. }
  134. // Do nothing with singular
  135. if ($count === 1)
  136. return $str;
  137. // Cache key name
  138. $key = 'plural_'.$str.$count;
  139. if (isset(Inflector::$cache[$key]))
  140. return Inflector::$cache[$key];
  141. if (Inflector::uncountable($str))
  142. return Inflector::$cache[$key] = $str;
  143. if (empty(Inflector::$irregular))
  144. {
  145. // Cache irregular words
  146. Inflector::$irregular = Kohana::config('inflector')->irregular;
  147. }
  148. if (isset(Inflector::$irregular[$str]))
  149. {
  150. $str = Inflector::$irregular[$str];
  151. }
  152. elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str))
  153. {
  154. $str .= 'es';
  155. }
  156. elseif (preg_match('/[^aeiou]y$/', $str))
  157. {
  158. // Change "y" to "ies"
  159. $str = substr_replace($str, 'ies', -1);
  160. }
  161. else
  162. {
  163. $str .= 's';
  164. }
  165. // Set the cache and return
  166. return Inflector::$cache[$key] = $str;
  167. }
  168. /**
  169. * Makes a phrase camel case. Spaces and underscores will be removed.
  170. *
  171. * $str = Inflector::camelize('mother cat'); // "motherCat"
  172. * $str = Inflector::camelize('kittens in bed'); // "kittensInBed"
  173. *
  174. * @param string phrase to camelize
  175. * @return string
  176. */
  177. public static function camelize($str)
  178. {
  179. $str = 'x'.strtolower(trim($str));
  180. $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
  181. return substr(str_replace(' ', '', $str), 1);
  182. }
  183. /**
  184. * Makes a phrase underscored instead of spaced.
  185. *
  186. * $str = Inflector::underscore('five cats'); // "five_cats";
  187. *
  188. * @param string phrase to underscore
  189. * @return string
  190. */
  191. public static function underscore($str)
  192. {
  193. return preg_replace('/\s+/', '_', trim($str));
  194. }
  195. /**
  196. * Makes an underscored or dashed phrase human-readable.
  197. *
  198. * $str = Inflector::humanize('kittens-are-cats'); // "kittens are cats"
  199. * $str = Inflector::humanize('dogs_as_well'); // "dogs as well"
  200. *
  201. * @param string phrase to make human-readable
  202. * @return string
  203. */
  204. public static function humanize($str)
  205. {
  206. return preg_replace('/[_-]+/', ' ', trim($str));
  207. }
  208. } // End Inflector