inflector_helper.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Inflector Helpers
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/helpers/directory_helper.html
  24. */
  25. // --------------------------------------------------------------------
  26. /**
  27. * Singular
  28. *
  29. * Takes a plural word and makes it singular
  30. *
  31. * @access public
  32. * @param string
  33. * @return str
  34. */
  35. if ( ! function_exists('singular'))
  36. {
  37. function singular($str)
  38. {
  39. $result = strval($str);
  40. $singular_rules = array(
  41. '/(matr)ices$/' => '\1ix',
  42. '/(vert|ind)ices$/' => '\1ex',
  43. '/^(ox)en/' => '\1',
  44. '/(alias)es$/' => '\1',
  45. '/([octop|vir])i$/' => '\1us',
  46. '/(cris|ax|test)es$/' => '\1is',
  47. '/(shoe)s$/' => '\1',
  48. '/(o)es$/' => '\1',
  49. '/(bus|campus)es$/' => '\1',
  50. '/([m|l])ice$/' => '\1ouse',
  51. '/(x|ch|ss|sh)es$/' => '\1',
  52. '/(m)ovies$/' => '\1\2ovie',
  53. '/(s)eries$/' => '\1\2eries',
  54. '/([^aeiouy]|qu)ies$/' => '\1y',
  55. '/([lr])ves$/' => '\1f',
  56. '/(tive)s$/' => '\1',
  57. '/(hive)s$/' => '\1',
  58. '/([^f])ves$/' => '\1fe',
  59. '/(^analy)ses$/' => '\1sis',
  60. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
  61. '/([ti])a$/' => '\1um',
  62. '/(p)eople$/' => '\1\2erson',
  63. '/(m)en$/' => '\1an',
  64. '/(s)tatuses$/' => '\1\2tatus',
  65. '/(c)hildren$/' => '\1\2hild',
  66. '/(n)ews$/' => '\1\2ews',
  67. '/([^u])s$/' => '\1',
  68. );
  69. foreach ($singular_rules as $rule => $replacement)
  70. {
  71. if (preg_match($rule, $result))
  72. {
  73. $result = preg_replace($rule, $replacement, $result);
  74. break;
  75. }
  76. }
  77. return $result;
  78. }
  79. }
  80. // --------------------------------------------------------------------
  81. /**
  82. * Plural
  83. *
  84. * Takes a singular word and makes it plural
  85. *
  86. * @access public
  87. * @param string
  88. * @param bool
  89. * @return str
  90. */
  91. if ( ! function_exists('plural'))
  92. {
  93. function plural($str, $force = FALSE)
  94. {
  95. $result = strval($str);
  96. $plural_rules = array(
  97. '/^(ox)$/' => '\1\2en', // ox
  98. '/([m|l])ouse$/' => '\1ice', // mouse, louse
  99. '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
  100. '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
  101. '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
  102. '/(hive)$/' => '\1s', // archive, hive
  103. '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
  104. '/sis$/' => 'ses', // basis, diagnosis
  105. '/([ti])um$/' => '\1a', // datum, medium
  106. '/(p)erson$/' => '\1eople', // person, salesperson
  107. '/(m)an$/' => '\1en', // man, woman, spokesman
  108. '/(c)hild$/' => '\1hildren', // child
  109. '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
  110. '/(bu|campu)s$/' => '\1\2ses', // bus, campus
  111. '/(alias|status|virus)/' => '\1es', // alias
  112. '/(octop)us$/' => '\1i', // octopus
  113. '/(ax|cris|test)is$/' => '\1es', // axis, crisis
  114. '/s$/' => 's', // no change (compatibility)
  115. '/$/' => 's',
  116. );
  117. foreach ($plural_rules as $rule => $replacement)
  118. {
  119. if (preg_match($rule, $result))
  120. {
  121. $result = preg_replace($rule, $replacement, $result);
  122. break;
  123. }
  124. }
  125. return $result;
  126. }
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Camelize
  131. *
  132. * Takes multiple words separated by spaces or underscores and camelizes them
  133. *
  134. * @access public
  135. * @param string
  136. * @return str
  137. */
  138. if ( ! function_exists('camelize'))
  139. {
  140. function camelize($str)
  141. {
  142. $str = 'x'.strtolower(trim($str));
  143. $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
  144. return substr(str_replace(' ', '', $str), 1);
  145. }
  146. }
  147. // --------------------------------------------------------------------
  148. /**
  149. * Underscore
  150. *
  151. * Takes multiple words separated by spaces and underscores them
  152. *
  153. * @access public
  154. * @param string
  155. * @return str
  156. */
  157. if ( ! function_exists('underscore'))
  158. {
  159. function underscore($str)
  160. {
  161. return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
  162. }
  163. }
  164. // --------------------------------------------------------------------
  165. /**
  166. * Humanize
  167. *
  168. * Takes multiple words separated by underscores and changes them to spaces
  169. *
  170. * @access public
  171. * @param string
  172. * @return str
  173. */
  174. if ( ! function_exists('humanize'))
  175. {
  176. function humanize($str)
  177. {
  178. return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
  179. }
  180. }
  181. /* End of file inflector_helper.php */
  182. /* Location: ./system/helpers/inflector_helper.php */