PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/inflector.php

https://github.com/lmorchard/friendfeedarchiver
PHP | 191 lines | 104 code | 27 blank | 60 comment | 16 complexity | a06d612f223215df7910c61334d5c177 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Inflector helper class.
  4. *
  5. * $Id: inflector.php 2513 2008-04-16 18:18:29Z JAAulde $
  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. /**
  14. * Checks if a word is defined as uncountable.
  15. *
  16. * @param string word to check
  17. * @return boolean
  18. */
  19. public static function uncountable($str)
  20. {
  21. static $uncountables = NULL;
  22. if ($uncountables === NULL)
  23. {
  24. // Makes a mirrored array, eg: foo => foo
  25. $uncountables = array_flip(Kohana::lang('inflector'));
  26. }
  27. return isset($uncountables[$str]);
  28. }
  29. /**
  30. * Makes a plural word singular.
  31. *
  32. * @param string word to singularize
  33. * @return string
  34. */
  35. public static function singular($str, $count = NULL)
  36. {
  37. static $cache;
  38. $str = trim($str);
  39. // Cache key name
  40. $key = $str.$count;
  41. // We can just return uncountable words
  42. if (inflector::uncountable($str))
  43. return $str;
  44. if (is_string($count) AND ctype_digit($count))
  45. {
  46. // Convert to integer when using a digit string
  47. $count = (int) $count;
  48. }
  49. if ($cache === NULL)
  50. {
  51. // Initialize the cache
  52. $cache = array();
  53. }
  54. else
  55. {
  56. // Already pluralized
  57. if (isset($cache[$key]))
  58. return $cache[$key];
  59. }
  60. // Do nothing with a single count
  61. if (is_int($count) AND $count === 0 OR $count > 1)
  62. return $str;
  63. if (substr($str, -3) === 'ies')
  64. {
  65. $str = substr($str, 0, strlen($str) - 3).'y';
  66. }
  67. elseif (substr($str, -4) === 'sses' OR substr($str, -3) === 'xes')
  68. {
  69. $str = substr($str, 0, strlen($str) - 2);
  70. }
  71. elseif (substr($str, -1) === 's')
  72. {
  73. $str = substr($str, 0, strlen($str) - 1);
  74. }
  75. return $cache[$key] = $str;
  76. }
  77. /**
  78. * Makes a singular word plural.
  79. *
  80. * @param string word to pluralize
  81. * @return string
  82. */
  83. public static function plural($str, $count = NULL)
  84. {
  85. static $cache;
  86. $str = trim($str);
  87. // Cache key name
  88. $key = $str.$count;
  89. // We can just return uncountable words
  90. if (inflector::uncountable($str))
  91. return $str;
  92. if (is_string($count) AND ctype_digit($count))
  93. {
  94. // Convert to integer when using a digit string
  95. $count = (int) $count;
  96. }
  97. if ($cache === NULL)
  98. {
  99. // Initialize the cache
  100. $cache = array();
  101. }
  102. else
  103. {
  104. // Already pluralized
  105. if (isset($cache[$key]))
  106. return $cache[$key];
  107. }
  108. // If the count is one, do not pluralize
  109. if ($count === 1)
  110. return $str;
  111. $end = substr($str, -1);
  112. $low = (strcmp($end, strtolower($end)) === 0) ? TRUE : FALSE;
  113. if (preg_match('/[sxz]$/i', $str) OR preg_match('/[^aeioudgkprt]h$/i', $str))
  114. {
  115. $end = 'es';
  116. $str .= ($low == FALSE) ? strtoupper($end) : $end;
  117. }
  118. elseif (preg_match('/[^aeiou]y$/i', $str))
  119. {
  120. $end = 'ies';
  121. $end = ($low == FALSE) ? strtoupper($end) : $end;
  122. $str = substr_replace($str, $end, -1);
  123. }
  124. else
  125. {
  126. $end = 's';
  127. $str .= ($low == FALSE) ? strtoupper($end) : $end;
  128. }
  129. // Set the cache and return
  130. return $cache[$key] = $str;
  131. }
  132. /**
  133. * Makes a phrase camel case.
  134. *
  135. * @param string phrase to camelize
  136. * @return string
  137. */
  138. public static function camelize($str)
  139. {
  140. $str = 'x'.strtolower(trim($str));
  141. $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
  142. return substr(str_replace(' ', '', $str), 1);
  143. }
  144. /**
  145. * Makes a phrase underscored instead of spaced.
  146. *
  147. * @param string phrase to underscore
  148. * @return string
  149. */
  150. public static function underscore($str)
  151. {
  152. return preg_replace('/\s+/', '_', trim($str));
  153. }
  154. /**
  155. * Makes an underscored or dashed phrase human-reable.
  156. *
  157. * @param string phrase to make human-reable
  158. * @return string
  159. */
  160. public static function humanize($str)
  161. {
  162. return preg_replace('/[_-]+/', ' ', trim($str));
  163. }
  164. } // End inflector