PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/libs/inflector.php

https://github.com/msadouni/cakephp2x
PHP | 435 lines | 243 code | 32 blank | 160 comment | 18 complexity | 618ef16b04a8593d70c5175e39c78cb8 MD5 | raw file
  1. <?php
  2. /**
  3. * Pluralize and singularize English words.
  4. *
  5. * Used by Cake's naming conventions throughout the framework.
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 0.2.9
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. /**
  23. * Pluralize and singularize English words.
  24. *
  25. * Inflector pluralizes and singularizes English nouns.
  26. * Used by Cake's naming conventions throughout the framework.
  27. *
  28. * @package cake
  29. * @subpackage cake.cake.libs
  30. * @link http://book.cakephp.org/view/491/Inflector
  31. */
  32. final class Inflector extends Object {
  33. /**
  34. * Plural inflector rules
  35. *
  36. * @var array
  37. * @access protected
  38. */
  39. protected static $_plural = array(
  40. 'rules' => array(
  41. '/(s)tatus$/i' => '\1\2tatuses',
  42. '/(quiz)$/i' => '\1zes',
  43. '/^(ox)$/i' => '\1\2en',
  44. '/([m|l])ouse$/i' => '\1ice',
  45. '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
  46. '/(x|ch|ss|sh)$/i' => '\1es',
  47. '/([^aeiouy]|qu)y$/i' => '\1ies',
  48. '/(hive)$/i' => '\1s',
  49. '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
  50. '/sis$/i' => 'ses',
  51. '/([ti])um$/i' => '\1a',
  52. '/(p)erson$/i' => '\1eople',
  53. '/(m)an$/i' => '\1en',
  54. '/(c)hild$/i' => '\1hildren',
  55. '/(buffal|tomat)o$/i' => '\1\2oes',
  56. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
  57. '/us$/' => 'uses',
  58. '/(alias)$/i' => '\1es',
  59. '/(ax|cris|test)is$/i' => '\1es',
  60. '/s$/' => 's',
  61. '/^$/' => '',
  62. '/$/' => 's',
  63. ),
  64. 'uninflected' => array(
  65. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people'
  66. ),
  67. 'irregular' => array(
  68. 'atlas' => 'atlases',
  69. 'beef' => 'beefs',
  70. 'brother' => 'brothers',
  71. 'child' => 'children',
  72. 'corpus' => 'corpuses',
  73. 'cow' => 'cows',
  74. 'ganglion' => 'ganglions',
  75. 'genie' => 'genies',
  76. 'genus' => 'genera',
  77. 'graffito' => 'graffiti',
  78. 'hoof' => 'hoofs',
  79. 'loaf' => 'loaves',
  80. 'man' => 'men',
  81. 'money' => 'monies',
  82. 'mongoose' => 'mongooses',
  83. 'move' => 'moves',
  84. 'mythos' => 'mythoi',
  85. 'niche' => 'niches',
  86. 'numen' => 'numina',
  87. 'occiput' => 'occiputs',
  88. 'octopus' => 'octopuses',
  89. 'opus' => 'opuses',
  90. 'ox' => 'oxen',
  91. 'penis' => 'penises',
  92. 'person' => 'people',
  93. 'sex' => 'sexes',
  94. 'soliloquy' => 'soliloquies',
  95. 'testis' => 'testes',
  96. 'trilby' => 'trilbys',
  97. 'turf' => 'turfs'
  98. )
  99. );
  100. /**
  101. * Singular inflector rules
  102. *
  103. * @var array
  104. * @access protected
  105. */
  106. protected static $_singular = array(
  107. 'rules' => array(
  108. '/(s)tatuses$/i' => '\1\2tatus',
  109. '/^(.*)(menu)s$/i' => '\1\2',
  110. '/(quiz)zes$/i' => '\\1',
  111. '/(matr)ices$/i' => '\1ix',
  112. '/(vert|ind)ices$/i' => '\1ex',
  113. '/^(ox)en/i' => '\1',
  114. '/(alias)(es)*$/i' => '\1',
  115. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  116. '/([ftw]ax)es/i' => '\1',
  117. '/(cris|ax|test)es$/i' => '\1is',
  118. '/(shoe|slave)s$/i' => '\1',
  119. '/(o)es$/i' => '\1',
  120. '/ouses$/' => 'ouse',
  121. '/uses$/' => 'us',
  122. '/([m|l])ice$/i' => '\1ouse',
  123. '/(x|ch|ss|sh)es$/i' => '\1',
  124. '/(m)ovies$/i' => '\1\2ovie',
  125. '/(s)eries$/i' => '\1\2eries',
  126. '/([^aeiouy]|qu)ies$/i' => '\1y',
  127. '/([lr])ves$/i' => '\1f',
  128. '/(tive)s$/i' => '\1',
  129. '/(hive)s$/i' => '\1',
  130. '/(drive)s$/i' => '\1',
  131. '/([^fo])ves$/i' => '\1fe',
  132. '/(^analy)ses$/i' => '\1sis',
  133. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  134. '/([ti])a$/i' => '\1um',
  135. '/(p)eople$/i' => '\1\2erson',
  136. '/(m)en$/i' => '\1an',
  137. '/(c)hildren$/i' => '\1\2hild',
  138. '/(n)ews$/i' => '\1\2ews',
  139. '/eaus$/' => 'eau',
  140. '/^(.*us)$/' => '\\1',
  141. '/s$/i' => ''
  142. ),
  143. 'uninflected' => array(
  144. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
  145. ),
  146. 'irregular' => array(
  147. 'waves' => 'wave'
  148. )
  149. );
  150. /**
  151. * Words that should not be inflected
  152. *
  153. * @var array
  154. * @access protected
  155. */
  156. protected static $_uninflected = array(
  157. 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
  158. 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
  159. 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
  160. 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
  161. 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
  162. 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media',
  163. 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
  164. 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
  165. 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
  166. 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
  167. 'trousers', 'trout','tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
  168. 'Yengeese'
  169. );
  170. /**
  171. * Cached array identity map of pluralized words.
  172. *
  173. * @var array
  174. * @access protected
  175. */
  176. protected static $_pluralized = array();
  177. /**
  178. * Cached array identity map of singularized words.
  179. *
  180. * @var array
  181. * @access protected
  182. */
  183. protected static $_singularized = array();
  184. /**
  185. * Adds custom inflection $rules, of either 'plural' or 'singular' $type.
  186. *
  187. * @param string $type The type of inflection, either 'singular' or 'plural'
  188. * @param array $rules Array of rules to be added. Example usage:
  189. * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
  190. * Inflector::rules('plural', array(
  191. * 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
  192. * 'uninflected' => array('dontinflectme'),
  193. * 'irregular' => array('red' => 'redlings')
  194. * ));
  195. * @access public
  196. * @return void
  197. * @static
  198. */
  199. public static function rules($type, $rules = array()) {
  200. $type = '_'.$type;
  201. foreach ($rules as $rule => $pattern) {
  202. if (is_array($pattern)) {
  203. self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
  204. unset($rules[$rule], self::${$type}['cache' . ucfirst($rule)], self::${$type}['merged'][$rule]);
  205. }
  206. }
  207. self::${$type}['rules'] = array_merge($rules, self::${$type}['rules']);
  208. }
  209. /**
  210. * Return $word in plural form.
  211. *
  212. * @param string $word Word in singular
  213. * @return string Word in plural
  214. * @access public
  215. * @static
  216. * @link http://book.cakephp.org/view/572/Class-methods
  217. */
  218. public static function pluralize($word) {
  219. if (isset(self::$_pluralized[$word])) {
  220. return self::$_pluralized[$word];
  221. }
  222. if (!isset(self::$_plural['merged']['irregular'])) {
  223. self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
  224. }
  225. if (!isset(self::$_plural['merged']['uninflected'])) {
  226. self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
  227. }
  228. if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
  229. self::$_plural['cacheUninflected'] = '(?:' . join( '|', self::$_plural['merged']['uninflected']) . ')';
  230. self::$_plural['cacheIrregular'] = '(?:' . join( '|', array_keys(self::$_plural['merged']['irregular'])) . ')';
  231. }
  232. if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
  233. self::$_pluralized[$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
  234. return self::$_pluralized[$word];
  235. }
  236. if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
  237. self::$_pluralized[$word] = $word;
  238. return $word;
  239. }
  240. foreach (self::$_plural['rules'] as $rule => $replacement) {
  241. if (preg_match($rule, $word)) {
  242. self::$_pluralized[$word] = preg_replace($rule, $replacement, $word);
  243. return self::$_pluralized[$word];
  244. }
  245. }
  246. }
  247. /**
  248. * Return $word in singular form.
  249. *
  250. * @param string $word Word in plural
  251. * @return string Word in singular
  252. * @access public
  253. * @static
  254. * @link http://book.cakephp.org/view/572/Class-methods
  255. */
  256. function singularize($word) {
  257. if (isset(self::$_singularized[$word])) {
  258. return self::$_singularized[$word];
  259. }
  260. if (!isset(self::$_singular['merged']['uninflected'])) {
  261. self::$_singular['merged']['uninflected'] = array_merge(self::$_singular['uninflected'], self::$_uninflected);
  262. }
  263. if (!isset(self::$_singular['merged']['irregular'])) {
  264. self::$_singular['merged']['irregular'] = array_merge(self::$_singular['irregular'], array_flip(self::$_plural['irregular']));
  265. }
  266. if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
  267. self::$_singular['cacheUninflected'] = '(?:' . join( '|', self::$_singular['merged']['uninflected']) . ')';
  268. self::$_singular['cacheIrregular'] = '(?:' . join( '|', array_keys(self::$_singular['merged']['irregular'])) . ')';
  269. }
  270. if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
  271. self::$_singularized[$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
  272. return self::$_singularized[$word];
  273. }
  274. if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
  275. self::$_singularized[$word] = $word;
  276. return $word;
  277. }
  278. foreach (self::$_singular['rules'] as $rule => $replacement) {
  279. if (preg_match($rule, $word)) {
  280. self::$_singularized[$word] = preg_replace($rule, $replacement, $word);
  281. return self::$_singularized[$word];
  282. }
  283. }
  284. self::$_singularized[$word] = $word;
  285. return $word;
  286. }
  287. /**
  288. * Returns the given lower_case_and_underscored_word as a CamelCased word.
  289. *
  290. * @param string $lower_case_and_underscored_word Word to camelize
  291. * @return string Camelized word. LikeThis.
  292. * @access public
  293. * @static
  294. * @link http://book.cakephp.org/view/572/Class-methods
  295. */
  296. function camelize($lowerCaseAndUnderscoredWord) {
  297. return str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
  298. }
  299. /**
  300. * Returns the given camelCasedWord as an underscored_word.
  301. *
  302. * @param string $camelCasedWord Camel-cased word to be "underscorized"
  303. * @return string Underscore-syntaxed version of the $camelCasedWord
  304. * @access public
  305. * @static
  306. * @link http://book.cakephp.org/view/572/Class-methods
  307. */
  308. function underscore($camelCasedWord) {
  309. return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  310. }
  311. /**
  312. * Returns the given underscored_word_group as a Human Readable Word Group.
  313. * (Underscores are replaced by spaces and capitalized following words.)
  314. *
  315. * @param string $lower_case_and_underscored_word String to be made more readable
  316. * @return string Human-readable string
  317. * @access public
  318. * @static
  319. * @link http://book.cakephp.org/view/572/Class-methods
  320. */
  321. function humanize($lowerCaseAndUnderscoredWord) {
  322. return ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
  323. }
  324. /**
  325. * Returns corresponding table name for given model $className. ("people" for the model class "Person").
  326. *
  327. * @param string $className Name of class to get database table name for
  328. * @return string Name of the database table for given class
  329. * @access public
  330. * @static
  331. * @link http://book.cakephp.org/view/572/Class-methods
  332. */
  333. function tableize($className) {
  334. return Inflector::pluralize(Inflector::underscore($className));
  335. }
  336. /**
  337. * Returns Cake model class name ("Person" for the database table "people".) for given database table.
  338. *
  339. * @param string $tableName Name of database table to get class name for
  340. * @return string Class name
  341. * @access public
  342. * @static
  343. * @link http://book.cakephp.org/view/572/Class-methods
  344. */
  345. function classify($tableName) {
  346. return Inflector::camelize(Inflector::singularize($tableName));
  347. }
  348. /**
  349. * Returns camelBacked version of an underscored string.
  350. *
  351. * @param string $string
  352. * @return string in variable form
  353. * @access public
  354. * @static
  355. * @link http://book.cakephp.org/view/572/Class-methods
  356. */
  357. function variable($string) {
  358. $string = Inflector::camelize(Inflector::underscore($string));
  359. $replace = strtolower(substr($string, 0, 1));
  360. return preg_replace('/\\w/', $replace, $string, 1);
  361. }
  362. /**
  363. * Returns a string with all spaces converted to underscores (by default), accented
  364. * characters converted to non-accented characters, and non word characters removed.
  365. *
  366. * @param string $string the string you want to slug
  367. * @param string $replacement will replace keys in map
  368. * @param array $map extra elements to map to the replacement
  369. * @return string
  370. * @access public
  371. * @static
  372. * @link http://book.cakephp.org/view/572/Class-methods
  373. */
  374. function slug($string, $replacement = '_', $map = array()) {
  375. if (is_array($replacement)) {
  376. $map = $replacement;
  377. $replacement = '_';
  378. }
  379. $quotedReplacement = preg_quote($replacement, '/');
  380. $default = array(
  381. '/à|á|å|â/' => 'a',
  382. '/è|é|ê|ẽ|ë/' => 'e',
  383. '/ì|í|î/' => 'i',
  384. '/ò|ó|ô|ø/' => 'o',
  385. '/ù|ú|ů|û/' => 'u',
  386. '/ç/' => 'c',
  387. '/ñ/' => 'n',
  388. '/ä|æ/' => 'ae',
  389. '/ö/' => 'oe',
  390. '/ü/' => 'ue',
  391. '/Ä/' => 'Ae',
  392. '/Ü/' => 'Ue',
  393. '/Ö/' => 'Oe',
  394. '/ß/' => 'ss',
  395. '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
  396. '/\\s+/' => $replacement,
  397. sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
  398. );
  399. $map = array_merge($default, $map);
  400. return preg_replace(array_keys($map), array_values($map), $string);
  401. }
  402. }
  403. ?>