PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/kohana/inflector.php

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