inflector_helper.php 5.3 KB

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