PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/inflector.php

https://github.com/Toushi/flow
PHP | 193 lines | 101 code | 28 blank | 64 comment | 13 complexity | c546732163f5aa5e2f46f932dc39a47b MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Inflector helper class.
  4. *
  5. * $Id: inflector.php 3160 2008-07-20 16:03:48Z Shadowhand $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class inflector_Core {
  13. // Cached inflections
  14. protected static $cache = array();
  15. // Uncountable and irregular words
  16. protected static $uncountable;
  17. protected static $irregular;
  18. /**
  19. * Checks if a word is defined as uncountable.
  20. *
  21. * @param string word to check
  22. * @return boolean
  23. */
  24. public static function uncountable($str)
  25. {
  26. if (self::$uncountable === NULL)
  27. {
  28. // Cache uncountables
  29. self::$uncountable = Kohana::config('inflector.uncountable');
  30. // Make uncountables mirroed
  31. self::$uncountable = array_combine(self::$uncountable, self::$uncountable);
  32. }
  33. return isset(self::$uncountable[strtolower($str)]);
  34. }
  35. /**
  36. * Makes a plural word singular.
  37. *
  38. * @param string word to singularize
  39. * @param integer number of things
  40. * @return string
  41. */
  42. public static function singular($str, $count = NULL)
  43. {
  44. // Remove garbage
  45. $str = strtolower(trim($str));
  46. if (is_string($count))
  47. {
  48. // Convert to integer when using a digit string
  49. $count = (int) $count;
  50. }
  51. // Do nothing with a single count
  52. if ($count === 0 OR $count > 1)
  53. return $str;
  54. // Cache key name
  55. $key = 'singular_'.$str.$count;
  56. if (isset(self::$cache[$key]))
  57. return self::$cache[$key];
  58. if (inflector::uncountable($str))
  59. return self::$cache[$key] = $str;
  60. if (empty(self::$irregular))
  61. {
  62. // Cache irregular words
  63. self::$irregular = Kohana::config('inflector.irregular');
  64. }
  65. if ($irregular = array_search($str, self::$irregular))
  66. {
  67. $str = $irregular;
  68. }
  69. elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
  70. {
  71. // Remove "es"
  72. $str = substr($str, 0, -2);
  73. }
  74. elseif (preg_match('/[^aeiou]ies$/', $str))
  75. {
  76. $str = substr($str, 0, -3).'y';
  77. }
  78. elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
  79. {
  80. $str = substr($str, 0, -1);
  81. }
  82. return self::$cache[$key] = $str;
  83. }
  84. /**
  85. * Makes a singular word plural.
  86. *
  87. * @param string word to pluralize
  88. * @return string
  89. */
  90. public static function plural($str, $count = NULL)
  91. {
  92. // Remove garbage
  93. $str = strtolower(trim($str));
  94. if (is_string($count))
  95. {
  96. // Convert to integer when using a digit string
  97. $count = (int) $count;
  98. }
  99. // Do nothing with singular
  100. if ($count === 1)
  101. return $str;
  102. // Cache key name
  103. $key = 'plural_'.$str.$count;
  104. if (isset(self::$cache[$key]))
  105. return self::$cache[$key];
  106. if (inflector::uncountable($str))
  107. return self::$cache[$key] = $str;
  108. if (empty(self::$irregular))
  109. {
  110. // Cache irregular words
  111. self::$irregular = Kohana::config('inflector.irregular');
  112. }
  113. if (isset(self::$irregular[$str]))
  114. {
  115. $str = self::$irregular[$str];
  116. }
  117. elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str))
  118. {
  119. $str .= 'es';
  120. }
  121. elseif (preg_match('/[^aeiou]y$/', $str))
  122. {
  123. // Change "y" to "ies"
  124. $str = substr_replace($str, 'ies', -1);
  125. }
  126. else
  127. {
  128. $str .= 's';
  129. }
  130. // Set the cache and return
  131. return self::$cache[$key] = $str;
  132. }
  133. /**
  134. * Makes a phrase camel case.
  135. *
  136. * @param string phrase to camelize
  137. * @return string
  138. */
  139. public static function camelize($str)
  140. {
  141. $str = 'x'.strtolower(trim($str));
  142. $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
  143. return substr(str_replace(' ', '', $str), 1);
  144. }
  145. /**
  146. * Makes a phrase underscored instead of spaced.
  147. *
  148. * @param string phrase to underscore
  149. * @return string
  150. */
  151. public static function underscore($str)
  152. {
  153. return preg_replace('/\s+/', '_', trim($str));
  154. }
  155. /**
  156. * Makes an underscored or dashed phrase human-reable.
  157. *
  158. * @param string phrase to make human-reable
  159. * @return string
  160. */
  161. public static function humanize($str)
  162. {
  163. return preg_replace('/[_-]+/', ' ', trim($str));
  164. }
  165. } // End inflector