PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Phactory/Inflector.php

http://github.com/chriskite/phactory
PHP | 377 lines | 158 code | 42 blank | 177 comment | 12 complexity | d35a81386bc170af7b5f0f4bc9f3ebe2 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Akelos PHP Application Framework |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.com/ |
  7. // | Released under the GNU Lesser General Public License |
  8. // +----------------------------------------------------------------------+
  9. // | You should have received the following files along with this library |
  10. // | - COPYRIGHT (Additional copyright notice) |
  11. // | - DISCLAIMER (Disclaimer of warranty) |
  12. // | - README (Important information regarding this library) |
  13. // +----------------------------------------------------------------------+
  14. namespace Phactory;
  15. /**
  16. * Inflector for pluralize and singularize English nouns.
  17. *
  18. * This Inflector is a port of Ruby on Rails Inflector.
  19. *
  20. * It can be really helpful for developers that want to
  21. * create frameworks based on naming conventions rather than
  22. * configurations.
  23. *
  24. * It was ported to PHP for the Akelos Framework, a
  25. * multilingual Ruby on Rails like framework for PHP that will
  26. * be launched soon.
  27. *
  28. * @author Bermi Ferrer Martinez <bermi akelos com>
  29. * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  30. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  31. * @since 0.1
  32. * @version $Revision 0.1 $
  33. */
  34. class Inflector
  35. {
  36. // ------ CLASS METHODS ------ //
  37. // ---- Public methods ---- //
  38. // {{{ pluralize()
  39. /**
  40. * Pluralizes English nouns.
  41. *
  42. * @access public
  43. * @static
  44. * @param string $word English noun to pluralize
  45. * @return string Plural noun
  46. */
  47. public static function pluralize($word)
  48. {
  49. $plural = array(
  50. '/(quiz)$/i' => '\1zes',
  51. '/^(ox)$/i' => '\1en',
  52. '/([m|l])ouse$/i' => '\1ice',
  53. '/(matr|vert|ind)ix|ex$/i' => '\1ices',
  54. '/(x|ch|ss|sh)$/i' => '\1es',
  55. '/([^aeiouy]|qu)ies$/i' => '\1y',
  56. '/([^aeiouy]|qu)y$/i' => '\1ies',
  57. '/(hive)$/i' => '\1s',
  58. '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
  59. '/sis$/i' => 'ses',
  60. '/([ti])um$/i' => '\1a',
  61. '/(buffal|tomat)o$/i' => '\1oes',
  62. '/(bu)s$/i' => '\1ses',
  63. '/(alias|status)/i'=> '\1es',
  64. '/(octop|vir)us$/i'=> '\1i',
  65. '/(ax|test)is$/i'=> '\1es',
  66. '/s$/i'=> 's',
  67. '/$/'=> 's');
  68. $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
  69. $irregular = array(
  70. 'person' => 'people',
  71. 'man' => 'men',
  72. 'child' => 'children',
  73. 'sex' => 'sexes',
  74. 'move' => 'moves');
  75. $lowercased_word = strtolower($word);
  76. foreach ($uncountable as $_uncountable){
  77. if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
  78. return $word;
  79. }
  80. }
  81. foreach ($irregular as $_plural=> $_singular){
  82. if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
  83. return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);
  84. }
  85. }
  86. foreach ($plural as $rule => $replacement) {
  87. if (preg_match($rule, $word)) {
  88. return preg_replace($rule, $replacement, $word);
  89. }
  90. }
  91. return false;
  92. }
  93. // }}}
  94. // {{{ singularize()
  95. /**
  96. * Singularizes English nouns.
  97. *
  98. * @access public
  99. * @static
  100. * @param string $word English noun to singularize
  101. * @return string Singular noun.
  102. */
  103. function singularize($word)
  104. {
  105. $singular = array (
  106. '/(quiz)zes$/i' => '\\1',
  107. '/(matr)ices$/i' => '\\1ix',
  108. '/(vert|ind)ices$/i' => '\\1ex',
  109. '/^(ox)en/i' => '\\1',
  110. '/(alias|status)es$/i' => '\\1',
  111. '/([octop|vir])i$/i' => '\\1us',
  112. '/(cris|ax|test)es$/i' => '\\1is',
  113. '/(shoe)s$/i' => '\\1',
  114. '/(o)es$/i' => '\\1',
  115. '/(bus)es$/i' => '\\1',
  116. '/([m|l])ice$/i' => '\\1ouse',
  117. '/(x|ch|ss|sh)es$/i' => '\\1',
  118. '/(m)ovies$/i' => '\\1ovie',
  119. '/(s)eries$/i' => '\\1eries',
  120. '/([^aeiouy]|qu)ies$/i' => '\\1y',
  121. '/([lr])ves$/i' => '\\1f',
  122. '/(tive)s$/i' => '\\1',
  123. '/(hive)s$/i' => '\\1',
  124. '/([^f])ves$/i' => '\\1fe',
  125. '/(^analy)ses$/i' => '\\1sis',
  126. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\\1\\2sis',
  127. '/([ti])a$/i' => '\\1um',
  128. '/(n)ews$/i' => '\\1ews',
  129. '/s$/i' => '',
  130. );
  131. $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
  132. $irregular = array(
  133. 'person' => 'people',
  134. 'man' => 'men',
  135. 'child' => 'children',
  136. 'sex' => 'sexes',
  137. 'move' => 'moves');
  138. $lowercased_word = strtolower($word);
  139. foreach ($uncountable as $_uncountable){
  140. if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
  141. return $word;
  142. }
  143. }
  144. foreach ($irregular as $_plural=> $_singular){
  145. if (preg_match('/('.$_singular.')$/i', $word, $arr)) {
  146. return preg_replace('/('.$_singular.')$/i', substr($arr[0],0,1).substr($_plural,1), $word);
  147. }
  148. }
  149. foreach ($singular as $rule => $replacement) {
  150. if (preg_match($rule, $word)) {
  151. return preg_replace($rule, $replacement, $word);
  152. }
  153. }
  154. return $word;
  155. }
  156. // }}}
  157. // {{{ titleize()
  158. /**
  159. * Converts an underscored or CamelCase word into a English
  160. * sentence.
  161. *
  162. * The titleize function converts text like "WelcomePage",
  163. * "welcome_page" or "welcome page" to this "Welcome
  164. * Page".
  165. * If second parameter is set to 'first' it will only
  166. * capitalize the first character of the title.
  167. *
  168. * @access public
  169. * @static
  170. * @param string $word Word to format as tile
  171. * @param string $uppercase If set to 'first' it will only uppercase the
  172. * first character. Otherwise it will uppercase all
  173. * the words in the title.
  174. * @return string Text formatted as title
  175. */
  176. function titleize($word, $uppercase = '')
  177. {
  178. $uppercase = $uppercase == 'first' ? 'ucfirst' : 'ucwords';
  179. return $uppercase(Inflector::humanize(Inflector::underscore($word)));
  180. }
  181. // }}}
  182. // {{{ camelize()
  183. /**
  184. * Returns given word as CamelCased
  185. *
  186. * Converts a word like "send_email" to "SendEmail". It
  187. * will remove non alphanumeric character from the word, so
  188. * "who's online" will be converted to "WhoSOnline"
  189. *
  190. * @access public
  191. * @static
  192. * @see variablize
  193. * @param string $word Word to convert to camel case
  194. * @return string UpperCamelCasedWord
  195. */
  196. function camelize($word)
  197. {
  198. return str_replace(' ','',ucwords(preg_replace('/[^A-Z^a-z^0-9]+/',' ',$word)));
  199. }
  200. // }}}
  201. // {{{ underscore()
  202. /**
  203. * Converts a word "into_it_s_underscored_version"
  204. *
  205. * Convert any "CamelCased" or "ordinary Word" into an
  206. * "underscored_word".
  207. *
  208. * This can be really useful for creating friendly URLs.
  209. *
  210. * @access public
  211. * @static
  212. * @param string $word Word to underscore
  213. * @return string Underscored word
  214. */
  215. function underscore($word)
  216. {
  217. return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/','_',
  218. preg_replace('/([a-z\d])([A-Z])/','\1_\2',
  219. preg_replace('/([A-Z]+)([A-Z][a-z])/','\1_\2',$word))));
  220. }
  221. // }}}
  222. // {{{ humanize()
  223. /**
  224. * Returns a human-readable string from $word
  225. *
  226. * Returns a human-readable string from $word, by replacing
  227. * underscores with a space, and by upper-casing the initial
  228. * character by default.
  229. *
  230. * If you need to uppercase all the words you just have to
  231. * pass 'all' as a second parameter.
  232. *
  233. * @access public
  234. * @static
  235. * @param string $word String to "humanize"
  236. * @param string $uppercase If set to 'all' it will uppercase all the words
  237. * instead of just the first one.
  238. * @return string Human-readable word
  239. */
  240. function humanize($word, $uppercase = '')
  241. {
  242. $uppercase = $uppercase == 'all' ? 'ucwords' : 'ucfirst';
  243. return $uppercase(str_replace('_',' ',preg_replace('/_id$/', '',$word)));
  244. }
  245. // }}}
  246. // {{{ variablize()
  247. /**
  248. * Same as camelize but first char is underscored
  249. *
  250. * Converts a word like "send_email" to "sendEmail". It
  251. * will remove non alphanumeric character from the word, so
  252. * "who's online" will be converted to "whoSOnline"
  253. *
  254. * @access public
  255. * @static
  256. * @see camelize
  257. * @param string $word Word to lowerCamelCase
  258. * @return string Returns a lowerCamelCasedWord
  259. */
  260. function variablize($word)
  261. {
  262. $word = Inflector::camelize($word);
  263. return strtolower($word[0]).substr($word,1);
  264. }
  265. // }}}
  266. // {{{ tableize()
  267. /**
  268. * Converts a class name to its table name according to rails
  269. * naming conventions.
  270. *
  271. * Converts "Person" to "people"
  272. *
  273. * @access public
  274. * @static
  275. * @see classify
  276. * @param string $class_name Class name for getting related table_name.
  277. * @return string plural_table_name
  278. */
  279. function tableize($class_name)
  280. {
  281. return Inflector::pluralize(Inflector::underscore($class_name));
  282. }
  283. // }}}
  284. // {{{ classify()
  285. /**
  286. * Converts a table name to its class name according to rails
  287. * naming conventions.
  288. *
  289. * Converts "people" to "Person"
  290. *
  291. * @access public
  292. * @static
  293. * @see tableize
  294. * @param string $table_name Table name for getting related ClassName.
  295. * @return string SingularClassName
  296. */
  297. function classify($table_name)
  298. {
  299. return Inflector::camelize(Inflector::singularize($table_name));
  300. }
  301. // }}}
  302. // {{{ ordinalize()
  303. /**
  304. * Converts number to its ordinal English form.
  305. *
  306. * This method converts 13 to 13th, 2 to 2nd ...
  307. *
  308. * @access public
  309. * @static
  310. * @param integer $number Number to get its ordinal value
  311. * @return string Ordinal representation of given string.
  312. */
  313. function ordinalize($number)
  314. {
  315. if (in_array(($number % 100),range(11,13))){
  316. return $number.'th';
  317. }else{
  318. switch (($number % 10)) {
  319. case 1:
  320. return $number.'st';
  321. break;
  322. case 2:
  323. return $number.'nd';
  324. break;
  325. case 3:
  326. return $number.'rd';
  327. default:
  328. return $number.'th';
  329. break;
  330. }
  331. }
  332. }
  333. // }}}
  334. }
  335. ?>