inflector_helper.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Inflector Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/helpers/inflector_helper.html
  47. */
  48. // --------------------------------------------------------------------
  49. if ( ! function_exists('singular'))
  50. {
  51. /**
  52. * Singular
  53. *
  54. * Takes a plural word and makes it singular
  55. *
  56. * @param string $str Input string
  57. * @return string
  58. */
  59. function singular($str)
  60. {
  61. $result = strval($str);
  62. if ( ! is_countable($result))
  63. {
  64. return $result;
  65. }
  66. $singular_rules = array(
  67. '/(matr)ices$/' => '\1ix',
  68. '/(vert|ind)ices$/' => '\1ex',
  69. '/^(ox)en/' => '\1',
  70. '/(alias)es$/' => '\1',
  71. '/([octop|vir])i$/' => '\1us',
  72. '/(cris|ax|test)es$/' => '\1is',
  73. '/(shoe)s$/' => '\1',
  74. '/(o)es$/' => '\1',
  75. '/(bus|campus)es$/' => '\1',
  76. '/([m|l])ice$/' => '\1ouse',
  77. '/(x|ch|ss|sh)es$/' => '\1',
  78. '/(m)ovies$/' => '\1\2ovie',
  79. '/(s)eries$/' => '\1\2eries',
  80. '/([^aeiouy]|qu)ies$/' => '\1y',
  81. '/([lr])ves$/' => '\1f',
  82. '/(tive)s$/' => '\1',
  83. '/(hive)s$/' => '\1',
  84. '/([^f])ves$/' => '\1fe',
  85. '/(^analy)ses$/' => '\1sis',
  86. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
  87. '/([ti])a$/' => '\1um',
  88. '/(p)eople$/' => '\1\2erson',
  89. '/(m)en$/' => '\1an',
  90. '/(s)tatuses$/' => '\1\2tatus',
  91. '/(c)hildren$/' => '\1\2hild',
  92. '/(n)ews$/' => '\1\2ews',
  93. '/([^us])s$/' => '\1'
  94. );
  95. foreach ($singular_rules as $rule => $replacement)
  96. {
  97. if (preg_match($rule, $result))
  98. {
  99. $result = preg_replace($rule, $replacement, $result);
  100. break;
  101. }
  102. }
  103. return $result;
  104. }
  105. }
  106. // --------------------------------------------------------------------
  107. if ( ! function_exists('plural'))
  108. {
  109. /**
  110. * Plural
  111. *
  112. * Takes a singular word and makes it plural
  113. *
  114. * @param string $str Input string
  115. * @return string
  116. */
  117. function plural($str)
  118. {
  119. $result = strval($str);
  120. if ( ! is_countable($result))
  121. {
  122. return $result;
  123. }
  124. $plural_rules = array(
  125. '/(quiz)$/' => '\1zes', // quizzes
  126. '/^(ox)$/' => '\1\2en', // ox
  127. '/([m|l])ouse$/' => '\1ice', // mouse, louse
  128. '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
  129. '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
  130. '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
  131. '/(hive)$/' => '\1s', // archive, hive
  132. '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
  133. '/sis$/' => 'ses', // basis, diagnosis
  134. '/([ti])um$/' => '\1a', // datum, medium
  135. '/(p)erson$/' => '\1eople', // person, salesperson
  136. '/(m)an$/' => '\1en', // man, woman, spokesman
  137. '/(c)hild$/' => '\1hildren', // child
  138. '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
  139. '/(bu|campu)s$/' => '\1\2ses', // bus, campus
  140. '/(alias|status|virus)$/' => '\1es', // alias
  141. '/(octop)us$/' => '\1i', // octopus
  142. '/(ax|cris|test)is$/' => '\1es', // axis, crisis
  143. '/s$/' => 's', // no change (compatibility)
  144. '/$/' => 's',
  145. );
  146. foreach ($plural_rules as $rule => $replacement)
  147. {
  148. if (preg_match($rule, $result))
  149. {
  150. $result = preg_replace($rule, $replacement, $result);
  151. break;
  152. }
  153. }
  154. return $result;
  155. }
  156. }
  157. // --------------------------------------------------------------------
  158. if ( ! function_exists('camelize'))
  159. {
  160. /**
  161. * Camelize
  162. *
  163. * Takes multiple words separated by spaces or underscores and camelizes them
  164. *
  165. * @param string $str Input string
  166. * @return string
  167. */
  168. function camelize($str)
  169. {
  170. return strtolower($str[0]).substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
  171. }
  172. }
  173. // --------------------------------------------------------------------
  174. if ( ! function_exists('underscore'))
  175. {
  176. /**
  177. * Underscore
  178. *
  179. * Takes multiple words separated by spaces and underscores them
  180. *
  181. * @param string $str Input string
  182. * @return string
  183. */
  184. function underscore($str)
  185. {
  186. return preg_replace('/[\s]+/', '_', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str)));
  187. }
  188. }
  189. // --------------------------------------------------------------------
  190. if ( ! function_exists('humanize'))
  191. {
  192. /**
  193. * Humanize
  194. *
  195. * Takes multiple words separated by the separator and changes them to spaces
  196. *
  197. * @param string $str Input string
  198. * @param string $separator Input separator
  199. * @return string
  200. */
  201. function humanize($str, $separator = '_')
  202. {
  203. return ucwords(preg_replace('/['.preg_quote($separator).']+/', ' ', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str))));
  204. }
  205. }
  206. // --------------------------------------------------------------------
  207. if ( ! function_exists('is_countable'))
  208. {
  209. /**
  210. * Checks if the given word has a plural version.
  211. *
  212. * @param string $word Word to check
  213. * @return bool
  214. */
  215. function is_countable($word)
  216. {
  217. return ! in_array(
  218. strtolower($word),
  219. array(
  220. 'audio',
  221. 'bison',
  222. 'chassis',
  223. 'compensation',
  224. 'coreopsis',
  225. 'data',
  226. 'deer',
  227. 'education',
  228. 'emoji',
  229. 'equipment',
  230. 'fish',
  231. 'furniture',
  232. 'gold',
  233. 'information',
  234. 'knowledge',
  235. 'love',
  236. 'rain',
  237. 'money',
  238. 'moose',
  239. 'nutrition',
  240. 'offspring',
  241. 'plankton',
  242. 'pokemon',
  243. 'police',
  244. 'rice',
  245. 'series',
  246. 'sheep',
  247. 'species',
  248. 'swine',
  249. 'traffic',
  250. 'wheat'
  251. )
  252. );
  253. }
  254. }