You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
7.3 KiB

7 years ago
  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. '/(quiz)zes$/' => '\1',
  94. '/([^us])s$/' => '\1'
  95. );
  96. foreach ($singular_rules as $rule => $replacement)
  97. {
  98. if (preg_match($rule, $result))
  99. {
  100. $result = preg_replace($rule, $replacement, $result);
  101. break;
  102. }
  103. }
  104. return $result;
  105. }
  106. }
  107. // --------------------------------------------------------------------
  108. if ( ! function_exists('plural'))
  109. {
  110. /**
  111. * Plural
  112. *
  113. * Takes a singular word and makes it plural
  114. *
  115. * @param string $str Input string
  116. * @return string
  117. */
  118. function plural($str)
  119. {
  120. $result = strval($str);
  121. if ( ! is_countable($result))
  122. {
  123. return $result;
  124. }
  125. $plural_rules = array(
  126. '/(quiz)$/' => '\1zes', // quizzes
  127. '/^(ox)$/' => '\1\2en', // ox
  128. '/([m|l])ouse$/' => '\1ice', // mouse, louse
  129. '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
  130. '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
  131. '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
  132. '/(hive)$/' => '\1s', // archive, hive
  133. '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
  134. '/sis$/' => 'ses', // basis, diagnosis
  135. '/([ti])um$/' => '\1a', // datum, medium
  136. '/(p)erson$/' => '\1eople', // person, salesperson
  137. '/(m)an$/' => '\1en', // man, woman, spokesman
  138. '/(c)hild$/' => '\1hildren', // child
  139. '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
  140. '/(bu|campu)s$/' => '\1\2ses', // bus, campus
  141. '/(alias|status|virus)$/' => '\1es', // alias
  142. '/(octop)us$/' => '\1i', // octopus
  143. '/(ax|cris|test)is$/' => '\1es', // axis, crisis
  144. '/s$/' => 's', // no change (compatibility)
  145. '/$/' => 's',
  146. );
  147. foreach ($plural_rules as $rule => $replacement)
  148. {
  149. if (preg_match($rule, $result))
  150. {
  151. $result = preg_replace($rule, $replacement, $result);
  152. break;
  153. }
  154. }
  155. return $result;
  156. }
  157. }
  158. // --------------------------------------------------------------------
  159. if ( ! function_exists('camelize'))
  160. {
  161. /**
  162. * Camelize
  163. *
  164. * Takes multiple words separated by spaces or underscores and camelizes them
  165. *
  166. * @param string $str Input string
  167. * @return string
  168. */
  169. function camelize($str)
  170. {
  171. return strtolower($str[0]).substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
  172. }
  173. }
  174. // --------------------------------------------------------------------
  175. if ( ! function_exists('underscore'))
  176. {
  177. /**
  178. * Underscore
  179. *
  180. * Takes multiple words separated by spaces and underscores them
  181. *
  182. * @param string $str Input string
  183. * @return string
  184. */
  185. function underscore($str)
  186. {
  187. return preg_replace('/[\s]+/', '_', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str)));
  188. }
  189. }
  190. // --------------------------------------------------------------------
  191. if ( ! function_exists('humanize'))
  192. {
  193. /**
  194. * Humanize
  195. *
  196. * Takes multiple words separated by the separator and changes them to spaces
  197. *
  198. * @param string $str Input string
  199. * @param string $separator Input separator
  200. * @return string
  201. */
  202. function humanize($str, $separator = '_')
  203. {
  204. return ucwords(preg_replace('/['.preg_quote($separator).']+/', ' ', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str))));
  205. }
  206. }
  207. // --------------------------------------------------------------------
  208. if ( ! function_exists('is_countable'))
  209. {
  210. /**
  211. * Checks if the given word has a plural version.
  212. *
  213. * @param string $word Word to check
  214. * @return bool
  215. */
  216. function is_countable($word)
  217. {
  218. return ! in_array(
  219. strtolower($word),
  220. array(
  221. 'audio',
  222. 'bison',
  223. 'chassis',
  224. 'compensation',
  225. 'coreopsis',
  226. 'data',
  227. 'deer',
  228. 'education',
  229. 'emoji',
  230. 'equipment',
  231. 'fish',
  232. 'furniture',
  233. 'gold',
  234. 'information',
  235. 'knowledge',
  236. 'love',
  237. 'rain',
  238. 'money',
  239. 'moose',
  240. 'nutrition',
  241. 'offspring',
  242. 'plankton',
  243. 'pokemon',
  244. 'police',
  245. 'rice',
  246. 'series',
  247. 'sheep',
  248. 'species',
  249. 'swine',
  250. 'traffic',
  251. 'wheat'
  252. )
  253. );
  254. }
  255. }