PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/inflector.php

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