PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ci_system/helpers/inflector_helper.php

https://bitbucket.org/jdhall75/skillsdemo
PHP | 235 lines | 122 code | 23 blank | 90 comment | 11 complexity | 8ff116296adfcc765eefc774d734bb82 MD5 | raw file
Possible License(s): JSON, IPL-1.0
  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. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. // ------------------------------------------------------------------------
  28. /**
  29. * CodeIgniter Inflector Helpers
  30. *
  31. * @package CodeIgniter
  32. * @subpackage Helpers
  33. * @category Helpers
  34. * @author EllisLab Dev Team
  35. * @link http://codeigniter.com/user_guide/helpers/inflector_helper.html
  36. */
  37. // --------------------------------------------------------------------
  38. /**
  39. * Singular
  40. *
  41. * Takes a plural word and makes it singular
  42. *
  43. * @param string
  44. * @return str
  45. */
  46. if ( ! function_exists('singular'))
  47. {
  48. function singular($str)
  49. {
  50. $result = strval($str);
  51. if ( ! is_countable($result))
  52. {
  53. return $result;
  54. }
  55. $singular_rules = array(
  56. '/(matr)ices$/' => '\1ix',
  57. '/(vert|ind)ices$/' => '\1ex',
  58. '/^(ox)en/' => '\1',
  59. '/(alias)es$/' => '\1',
  60. '/([octop|vir])i$/' => '\1us',
  61. '/(cris|ax|test)es$/' => '\1is',
  62. '/(shoe)s$/' => '\1',
  63. '/(o)es$/' => '\1',
  64. '/(bus|campus)es$/' => '\1',
  65. '/([m|l])ice$/' => '\1ouse',
  66. '/(x|ch|ss|sh)es$/' => '\1',
  67. '/(m)ovies$/' => '\1\2ovie',
  68. '/(s)eries$/' => '\1\2eries',
  69. '/([^aeiouy]|qu)ies$/' => '\1y',
  70. '/([lr])ves$/' => '\1f',
  71. '/(tive)s$/' => '\1',
  72. '/(hive)s$/' => '\1',
  73. '/([^f])ves$/' => '\1fe',
  74. '/(^analy)ses$/' => '\1sis',
  75. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
  76. '/([ti])a$/' => '\1um',
  77. '/(p)eople$/' => '\1\2erson',
  78. '/(m)en$/' => '\1an',
  79. '/(s)tatuses$/' => '\1\2tatus',
  80. '/(c)hildren$/' => '\1\2hild',
  81. '/(n)ews$/' => '\1\2ews',
  82. '/([^us])s$/' => '\1',
  83. );
  84. foreach ($singular_rules as $rule => $replacement)
  85. {
  86. if (preg_match($rule, $result))
  87. {
  88. $result = preg_replace($rule, $replacement, $result);
  89. break;
  90. }
  91. }
  92. return $result;
  93. }
  94. }
  95. // --------------------------------------------------------------------
  96. /**
  97. * Plural
  98. *
  99. * Takes a singular word and makes it plural
  100. *
  101. * @param string
  102. * @param bool
  103. * @return str
  104. */
  105. if ( ! function_exists('plural'))
  106. {
  107. function plural($str, $force = FALSE)
  108. {
  109. $result = strval($str);
  110. if ( ! is_countable($result))
  111. {
  112. return $result;
  113. }
  114. $plural_rules = array(
  115. '/^(ox)$/' => '\1\2en', // ox
  116. '/([m|l])ouse$/' => '\1ice', // mouse, louse
  117. '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
  118. '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
  119. '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
  120. '/(hive)$/' => '\1s', // archive, hive
  121. '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
  122. '/sis$/' => 'ses', // basis, diagnosis
  123. '/([ti])um$/' => '\1a', // datum, medium
  124. '/(p)erson$/' => '\1eople', // person, salesperson
  125. '/(m)an$/' => '\1en', // man, woman, spokesman
  126. '/(c)hild$/' => '\1hildren', // child
  127. '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
  128. '/(bu|campu)s$/' => '\1\2ses', // bus, campus
  129. '/(alias|status|virus)$/' => '\1es', // alias
  130. '/(octop)us$/' => '\1i', // octopus
  131. '/(ax|cris|test)is$/' => '\1es', // axis, crisis
  132. '/s$/' => 's', // no change (compatibility)
  133. '/$/' => 's',
  134. );
  135. foreach ($plural_rules as $rule => $replacement)
  136. {
  137. if (preg_match($rule, $result))
  138. {
  139. $result = preg_replace($rule, $replacement, $result);
  140. break;
  141. }
  142. }
  143. return $result;
  144. }
  145. }
  146. // --------------------------------------------------------------------
  147. /**
  148. * Camelize
  149. *
  150. * Takes multiple words separated by spaces or underscores and camelizes them
  151. *
  152. * @param string
  153. * @return str
  154. */
  155. if ( ! function_exists('camelize'))
  156. {
  157. function camelize($str)
  158. {
  159. return substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
  160. }
  161. }
  162. // --------------------------------------------------------------------
  163. /**
  164. * Underscore
  165. *
  166. * Takes multiple words separated by spaces and underscores them
  167. *
  168. * @param string
  169. * @return str
  170. */
  171. if ( ! function_exists('underscore'))
  172. {
  173. function underscore($str)
  174. {
  175. return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
  176. }
  177. }
  178. // --------------------------------------------------------------------
  179. /**
  180. * Humanize
  181. *
  182. * Takes multiple words separated by the separator and changes them to spaces
  183. *
  184. * @param string $str
  185. * @param string $separator
  186. * @return str
  187. */
  188. if ( ! function_exists('humanize'))
  189. {
  190. function humanize($str, $separator = '_')
  191. {
  192. return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str))));
  193. }
  194. }
  195. /**
  196. * Checks if the given word has a plural version.
  197. *
  198. * @param string the word to check
  199. * @return bool if the word is countable
  200. */
  201. if ( ! function_exists('is_countable'))
  202. {
  203. function is_countable($word)
  204. {
  205. return ! (in_array(strtolower(strval($word)), array(
  206. 'equipment', 'information', 'rice', 'money',
  207. 'species', 'series', 'fish', 'meta'
  208. )));
  209. }
  210. }
  211. /* End of file inflector_helper.php */
  212. /* Location: ./system/helpers/inflector_helper.php */