PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/inflector.php

https://github.com/mariuz/firetube
PHP | 531 lines | 303 code | 19 blank | 209 comment | 21 complexity | 57179cbdcc1935c53b84fc2e7c32a8b5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Pluralize and singularize English words.
  5. *
  6. * Used by Cake's naming conventions throughout the framework.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package cake
  19. * @subpackage cake.cake.libs
  20. * @since CakePHP(tm) v 0.2.9
  21. * @version $Revision$
  22. * @modifiedby $LastChangedBy$
  23. * @lastmodified $Date$
  24. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  25. */
  26. /**
  27. * Included libraries.
  28. *
  29. */
  30. if (!class_exists('Object')) {
  31. uses('object');
  32. }
  33. if (!class_exists('Set')) {
  34. require LIBS . 'set.php';
  35. }
  36. /**
  37. * Pluralize and singularize English words.
  38. *
  39. * Inflector pluralizes and singularizes English nouns.
  40. * Used by Cake's naming conventions throughout the framework.
  41. * Test with $i = new Inflector(); $i->test();
  42. *
  43. * @package cake
  44. * @subpackage cake.cake.libs
  45. * @link http://book.cakephp.org/view/491/Inflector
  46. */
  47. class Inflector extends Object {
  48. /**
  49. * Pluralized words.
  50. *
  51. * @var array
  52. * @access private
  53. **/
  54. var $pluralized = array();
  55. /**
  56. * List of pluralization rules in the form of pattern => replacement.
  57. *
  58. * @var array
  59. * @access public
  60. * @link http://book.cakephp.org/view/47/Custom-Inflections
  61. **/
  62. var $pluralRules = array();
  63. /**
  64. * Singularized words.
  65. *
  66. * @var array
  67. * @access private
  68. **/
  69. var $singularized = array();
  70. /**
  71. * List of singularization rules in the form of pattern => replacement.
  72. *
  73. * @var array
  74. * @access public
  75. * @link http://book.cakephp.org/view/47/Custom-Inflections
  76. **/
  77. var $singularRules = array();
  78. /**
  79. * Plural rules from inflections.php
  80. *
  81. * @var array
  82. * @access private
  83. **/
  84. var $__pluralRules = array();
  85. /**
  86. * Un-inflected plural rules from inflections.php
  87. *
  88. * @var array
  89. * @access private
  90. **/
  91. var $__uninflectedPlural = array();
  92. /**
  93. * Irregular plural rules from inflections.php
  94. *
  95. * @var array
  96. * @access private
  97. **/
  98. var $__irregularPlural = array();
  99. /**
  100. * Singular rules from inflections.php
  101. *
  102. * @var array
  103. * @access private
  104. **/
  105. var $__singularRules = array();
  106. /**
  107. * Un-inflectd singular rules from inflections.php
  108. *
  109. * @var array
  110. * @access private
  111. **/
  112. var $__uninflectedSingular = array();
  113. /**
  114. * Irregular singular rules from inflections.php
  115. *
  116. * @var array
  117. * @access private
  118. **/
  119. var $__irregularSingular = array();
  120. /**
  121. * Gets a reference to the Inflector object instance
  122. *
  123. * @return object
  124. * @access public
  125. */
  126. function &getInstance() {
  127. static $instance = array();
  128. if (!$instance) {
  129. $instance[0] =& new Inflector();
  130. if (file_exists(CONFIGS.'inflections.php')) {
  131. include(CONFIGS.'inflections.php');
  132. $instance[0]->__pluralRules = $pluralRules;
  133. $instance[0]->__uninflectedPlural = $uninflectedPlural;
  134. $instance[0]->__irregularPlural = $irregularPlural;
  135. $instance[0]->__singularRules = $singularRules;
  136. $instance[0]->__uninflectedSingular = $uninflectedPlural;
  137. $instance[0]->__irregularSingular = array_flip($irregularPlural);
  138. }
  139. }
  140. return $instance[0];
  141. }
  142. /**
  143. * Initializes plural inflection rules.
  144. *
  145. * @return void
  146. * @access private
  147. */
  148. function __initPluralRules() {
  149. $corePluralRules = array(
  150. '/(s)tatus$/i' => '\1\2tatuses',
  151. '/(quiz)$/i' => '\1zes',
  152. '/^(ox)$/i' => '\1\2en',
  153. '/([m|l])ouse$/i' => '\1ice',
  154. '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
  155. '/(x|ch|ss|sh)$/i' => '\1es',
  156. '/([^aeiouy]|qu)y$/i' => '\1ies',
  157. '/(hive)$/i' => '\1s',
  158. '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
  159. '/sis$/i' => 'ses',
  160. '/([ti])um$/i' => '\1a',
  161. '/(p)erson$/i' => '\1eople',
  162. '/(m)an$/i' => '\1en',
  163. '/(c)hild$/i' => '\1hildren',
  164. '/(buffal|tomat)o$/i' => '\1\2oes',
  165. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
  166. '/us$/' => 'uses',
  167. '/(alias)$/i' => '\1es',
  168. '/(ax|cris|test)is$/i' => '\1es',
  169. '/s$/' => 's',
  170. '/^$/' => '',
  171. '/$/' => 's');
  172. $coreUninflectedPlural = array(
  173. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'Amoyese',
  174. 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus', 'carp', 'chassis', 'clippers',
  175. 'cod', 'coitus', 'Congoese', 'contretemps', 'corps', 'debris', 'diabetes', 'djinn', 'eland', 'elk',
  176. 'equipment', 'Faroese', 'flounder', 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
  177. 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings', 'jackanapes', 'Kiplingese',
  178. 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media', 'mews', 'moose', 'mumps', 'Nankingese', 'news',
  179. 'nexus', 'Niasese', 'Pekingese', 'People', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese', 'proceedings',
  180. 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors', 'sea[- ]bass', 'series', 'Shavese', 'shears',
  181. 'siemens', 'species', 'swine', 'testes', 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese',
  182. 'whiting', 'wildebeest', 'Yengeese');
  183. $coreIrregularPlural = array(
  184. 'atlas' => 'atlases',
  185. 'beef' => 'beefs',
  186. 'brother' => 'brothers',
  187. 'child' => 'children',
  188. 'corpus' => 'corpuses',
  189. 'cow' => 'cows',
  190. 'ganglion' => 'ganglions',
  191. 'genie' => 'genies',
  192. 'genus' => 'genera',
  193. 'graffito' => 'graffiti',
  194. 'hoof' => 'hoofs',
  195. 'loaf' => 'loaves',
  196. 'man' => 'men',
  197. 'money' => 'monies',
  198. 'mongoose' => 'mongooses',
  199. 'move' => 'moves',
  200. 'mythos' => 'mythoi',
  201. 'numen' => 'numina',
  202. 'occiput' => 'occiputs',
  203. 'octopus' => 'octopuses',
  204. 'opus' => 'opuses',
  205. 'ox' => 'oxen',
  206. 'penis' => 'penises',
  207. 'person' => 'people',
  208. 'sex' => 'sexes',
  209. 'soliloquy' => 'soliloquies',
  210. 'testis' => 'testes',
  211. 'trilby' => 'trilbys',
  212. 'turf' => 'turfs');
  213. $pluralRules = Set::pushDiff($this->__pluralRules, $corePluralRules);
  214. $uninflected = Set::pushDiff($this->__uninflectedPlural, $coreUninflectedPlural);
  215. $irregular = Set::pushDiff($this->__irregularPlural, $coreIrregularPlural);
  216. $this->pluralRules = array('pluralRules' => $pluralRules, 'uninflected' => $uninflected, 'irregular' => $irregular);
  217. $this->pluralized = array();
  218. }
  219. /**
  220. * Return $word in plural form.
  221. *
  222. * @param string $word Word in singular
  223. * @return string Word in plural
  224. * @access public
  225. * @static
  226. * @link http://book.cakephp.org/view/572/Class-methods
  227. */
  228. function pluralize($word) {
  229. $_this =& Inflector::getInstance();
  230. if (!isset($_this->pluralRules) || empty($_this->pluralRules)) {
  231. $_this->__initPluralRules();
  232. }
  233. if (isset($_this->pluralized[$word])) {
  234. return $_this->pluralized[$word];
  235. }
  236. extract($_this->pluralRules);
  237. if (!isset($regexUninflected) || !isset($regexIrregular)) {
  238. $regexUninflected = __enclose(implode( '|', $uninflected));
  239. $regexIrregular = __enclose(implode( '|', array_keys($irregular)));
  240. $_this->pluralRules['regexUninflected'] = $regexUninflected;
  241. $_this->pluralRules['regexIrregular'] = $regexIrregular;
  242. }
  243. if (preg_match('/^(' . $regexUninflected . ')$/i', $word, $regs)) {
  244. $_this->pluralized[$word] = $word;
  245. return $word;
  246. }
  247. if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) {
  248. $_this->pluralized[$word] = $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
  249. return $_this->pluralized[$word];
  250. }
  251. foreach ($pluralRules as $rule => $replacement) {
  252. if (preg_match($rule, $word)) {
  253. $_this->pluralized[$word] = preg_replace($rule, $replacement, $word);
  254. return $_this->pluralized[$word];
  255. }
  256. }
  257. }
  258. /**
  259. * Initializes singular inflection rules.
  260. *
  261. * @return void
  262. * @access protected
  263. */
  264. function __initSingularRules() {
  265. $coreSingularRules = array(
  266. '/(s)tatuses$/i' => '\1\2tatus',
  267. '/^(.*)(menu)s$/i' => '\1\2',
  268. '/(quiz)zes$/i' => '\\1',
  269. '/(matr)ices$/i' => '\1ix',
  270. '/(vert|ind)ices$/i' => '\1ex',
  271. '/^(ox)en/i' => '\1',
  272. '/(alias)(es)*$/i' => '\1',
  273. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  274. '/([ftw]ax)es/' => '\1',
  275. '/(cris|ax|test)es$/i' => '\1is',
  276. '/(shoe)s$/i' => '\1',
  277. '/(o)es$/i' => '\1',
  278. '/ouses$/' => 'ouse',
  279. '/uses$/' => 'us',
  280. '/([m|l])ice$/i' => '\1ouse',
  281. '/(x|ch|ss|sh)es$/i' => '\1',
  282. '/(m)ovies$/i' => '\1\2ovie',
  283. '/(s)eries$/i' => '\1\2eries',
  284. '/([^aeiouy]|qu)ies$/i' => '\1y',
  285. '/([lr])ves$/i' => '\1f',
  286. '/(tive)s$/i' => '\1',
  287. '/(hive)s$/i' => '\1',
  288. '/(drive)s$/i' => '\1',
  289. '/([^fo])ves$/i' => '\1fe',
  290. '/(^analy)ses$/i' => '\1sis',
  291. '/(analy|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  292. '/([ti])a$/i' => '\1um',
  293. '/(p)eople$/i' => '\1\2erson',
  294. '/(m)en$/i' => '\1an',
  295. '/(c)hildren$/i' => '\1\2hild',
  296. '/(n)ews$/i' => '\1\2ews',
  297. '/eaus$/' => 'eau',
  298. '/^(.*us)$/' => '\\1',
  299. '/s$/i' => '');
  300. $coreUninflectedSingular = array(
  301. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss', 'Amoyese',
  302. 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus', 'carp', 'chassis', 'clippers',
  303. 'cod', 'coitus', 'Congoese', 'contretemps', 'corps', 'debris', 'diabetes', 'djinn', 'eland', 'elk',
  304. 'equipment', 'Faroese', 'flounder', 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
  305. 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings', 'jackanapes', 'Kiplingese',
  306. 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media', 'mews', 'moose', 'mumps', 'Nankingese', 'news',
  307. 'nexus', 'Niasese', 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese', 'proceedings',
  308. 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors', 'sea[- ]bass', 'series', 'Shavese', 'shears',
  309. 'siemens', 'species', 'swine', 'testes', 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese',
  310. 'whiting', 'wildebeest', 'Yengeese'
  311. );
  312. $coreIrregularSingular = array(
  313. 'atlases' => 'atlas',
  314. 'beefs' => 'beef',
  315. 'brothers' => 'brother',
  316. 'children' => 'child',
  317. 'corpuses' => 'corpus',
  318. 'cows' => 'cow',
  319. 'ganglions' => 'ganglion',
  320. 'genies' => 'genie',
  321. 'genera' => 'genus',
  322. 'graffiti' => 'graffito',
  323. 'hoofs' => 'hoof',
  324. 'loaves' => 'loaf',
  325. 'men' => 'man',
  326. 'monies' => 'money',
  327. 'mongooses' => 'mongoose',
  328. 'moves' => 'move',
  329. 'mythoi' => 'mythos',
  330. 'numina' => 'numen',
  331. 'occiputs' => 'occiput',
  332. 'octopuses' => 'octopus',
  333. 'opuses' => 'opus',
  334. 'oxen' => 'ox',
  335. 'penises' => 'penis',
  336. 'people' => 'person',
  337. 'sexes' => 'sex',
  338. 'soliloquies' => 'soliloquy',
  339. 'testes' => 'testis',
  340. 'trilbys' => 'trilby',
  341. 'turfs' => 'turf',
  342. 'waves' => 'wave'
  343. );
  344. $singularRules = Set::pushDiff($this->__singularRules, $coreSingularRules);
  345. $uninflected = Set::pushDiff($this->__uninflectedSingular, $coreUninflectedSingular);
  346. $irregular = Set::pushDiff($this->__irregularSingular, $coreIrregularSingular);
  347. $this->singularRules = array('singularRules' => $singularRules, 'uninflected' => $uninflected, 'irregular' => $irregular);
  348. $this->singularized = array();
  349. }
  350. /**
  351. * Return $word in singular form.
  352. *
  353. * @param string $word Word in plural
  354. * @return string Word in singular
  355. * @access public
  356. * @static
  357. * @link http://book.cakephp.org/view/572/Class-methods
  358. */
  359. function singularize($word) {
  360. $_this =& Inflector::getInstance();
  361. if (!isset($_this->singularRules) || empty($_this->singularRules)) {
  362. $_this->__initSingularRules();
  363. }
  364. if (isset($_this->singularized[$word])) {
  365. return $_this->singularized[$word];
  366. }
  367. extract($_this->singularRules);
  368. if (!isset($regexUninflected) || !isset($regexIrregular)) {
  369. $regexUninflected = __enclose(implode( '|', $uninflected));
  370. $regexIrregular = __enclose(implode( '|', array_keys($irregular)));
  371. $_this->singularRules['regexUninflected'] = $regexUninflected;
  372. $_this->singularRules['regexIrregular'] = $regexIrregular;
  373. }
  374. if (preg_match('/^(' . $regexUninflected . ')$/i', $word, $regs)) {
  375. $_this->singularized[$word] = $word;
  376. return $word;
  377. }
  378. if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) {
  379. $_this->singularized[$word] = $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
  380. return $_this->singularized[$word];
  381. }
  382. foreach ($singularRules as $rule => $replacement) {
  383. if (preg_match($rule, $word)) {
  384. $_this->singularized[$word] = preg_replace($rule, $replacement, $word);
  385. return $_this->singularized[$word];
  386. }
  387. }
  388. $_this->singularized[$word] = $word;
  389. return $word;
  390. }
  391. /**
  392. * Returns the given lower_case_and_underscored_word as a CamelCased word.
  393. *
  394. * @param string $lower_case_and_underscored_word Word to camelize
  395. * @return string Camelized word. LikeThis.
  396. * @access public
  397. * @static
  398. * @link http://book.cakephp.org/view/572/Class-methods
  399. */
  400. function camelize($lowerCaseAndUnderscoredWord) {
  401. return str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
  402. }
  403. /**
  404. * Returns the given camelCasedWord as an underscored_word.
  405. *
  406. * @param string $camelCasedWord Camel-cased word to be "underscorized"
  407. * @return string Underscore-syntaxed version of the $camelCasedWord
  408. * @access public
  409. * @static
  410. * @link http://book.cakephp.org/view/572/Class-methods
  411. */
  412. function underscore($camelCasedWord) {
  413. return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  414. }
  415. /**
  416. * Returns the given underscored_word_group as a Human Readable Word Group.
  417. * (Underscores are replaced by spaces and capitalized following words.)
  418. *
  419. * @param string $lower_case_and_underscored_word String to be made more readable
  420. * @return string Human-readable string
  421. * @access public
  422. * @static
  423. * @link http://book.cakephp.org/view/572/Class-methods
  424. */
  425. function humanize($lowerCaseAndUnderscoredWord) {
  426. return ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
  427. }
  428. /**
  429. * Returns corresponding table name for given model $className. ("people" for the model class "Person").
  430. *
  431. * @param string $className Name of class to get database table name for
  432. * @return string Name of the database table for given class
  433. * @access public
  434. * @static
  435. * @link http://book.cakephp.org/view/572/Class-methods
  436. */
  437. function tableize($className) {
  438. return Inflector::pluralize(Inflector::underscore($className));
  439. }
  440. /**
  441. * Returns Cake model class name ("Person" for the database table "people".) for given database table.
  442. *
  443. * @param string $tableName Name of database table to get class name for
  444. * @return string Class name
  445. * @access public
  446. * @static
  447. * @link http://book.cakephp.org/view/572/Class-methods
  448. */
  449. function classify($tableName) {
  450. return Inflector::camelize(Inflector::singularize($tableName));
  451. }
  452. /**
  453. * Returns camelBacked version of an underscored string.
  454. *
  455. * @param string $string
  456. * @return string in variable form
  457. * @access public
  458. * @static
  459. * @link http://book.cakephp.org/view/572/Class-methods
  460. */
  461. function variable($string) {
  462. $string = Inflector::camelize(Inflector::underscore($string));
  463. $replace = strtolower(substr($string, 0, 1));
  464. return $replace . substr($string, 1);
  465. }
  466. /**
  467. * Returns a string with all spaces converted to underscores (by default), accented
  468. * characters converted to non-accented characters, and non word characters removed.
  469. *
  470. * @param string $string
  471. * @param string $replacement
  472. * @return string
  473. * @access public
  474. * @static
  475. * @link http://book.cakephp.org/view/572/Class-methods
  476. */
  477. function slug($string, $replacement = '_') {
  478. if (!class_exists('String')) {
  479. require LIBS . 'string.php';
  480. }
  481. $map = array(
  482. '/à|á|å|â/' => 'a',
  483. '/è|é|ê|ẽ|ë/' => 'e',
  484. '/ì|í|î/' => 'i',
  485. '/ò|ó|ô|ø/' => 'o',
  486. '/ù|ú|ů|û/' => 'u',
  487. '/ç/' => 'c',
  488. '/ñ/' => 'n',
  489. '/ä|æ/' => 'ae',
  490. '/ö/' => 'oe',
  491. '/ü/' => 'ue',
  492. '/Ä/' => 'Ae',
  493. '/Ü/' => 'Ue',
  494. '/Ö/' => 'Oe',
  495. '/ß/' => 'ss',
  496. '/[^\w\s]/' => ' ',
  497. '/\\s+/' => $replacement,
  498. String::insert('/^[:replacement]+|[:replacement]+$/', array('replacement' => preg_quote($replacement, '/'))) => '',
  499. );
  500. return preg_replace(array_keys($map), array_values($map), $string);
  501. }
  502. }
  503. /**
  504. * Enclose a string for preg matching.
  505. *
  506. * @param string $string String to enclose
  507. * @return string Enclosed string
  508. */
  509. function __enclose($string) {
  510. return '(?:' . $string . ')';
  511. }
  512. ?>