[ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: inflector.php 4409 2007-02-02 13:20:59Z phpnut $ */ 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://www.cakephp.org/> 11 * Copyright 2005-2007, Cake Software Foundation, Inc. 12 * 1785 E. Sahara Avenue, Suite 490-204 13 * Las Vegas, Nevada 89104 14 * 15 * Licensed under The MIT License 16 * Redistributions of files must retain the above copyright notice. 17 * 18 * @filesource 19 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 20 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 21 * @package cake 22 * @subpackage cake.cake.libs 23 * @since CakePHP(tm) v 0.2.9 24 * @version $Revision: 4409 $ 25 * @modifiedby $LastChangedBy: phpnut $ 26 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 27 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 28 */ 29 /** 30 * Included libraries. 31 * 32 */ 33 if (!class_exists('Object')) { 34 uses('object'); 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 */ 46 class Inflector extends Object{ 47 /** 48 * Constructor. 49 * 50 */ 51 function __construct() { 52 parent::__construct(); 53 } 54 /** 55 * Gets a reference to the Inflector object instance 56 * 57 * @return object 58 */ 59 function &getInstance() { 60 static $instance = array(); 61 62 if (!isset($instance[0]) || !$instance[0]) { 63 $instance[0] = &new Inflector(); 64 } 65 66 return $instance[0]; 67 } 68 /** 69 * Initializes plural inflection rules 70 * 71 * @access protected 72 * @return void 73 */ 74 function __initPluralRules() { 75 $_this =& Inflector::getInstance(); 76 $corePluralRules = array('/(s)tatus$/i' => '\1\2tatuses', 77 '/^(ox)$/i' => '\1\2en', # ox 78 '/([m|l])ouse$/i' => '\1ice', # mouse, louse 79 '/(matr|vert|ind)ix|ex$/i' => '\1ices', # matrix, vertex, index 80 '/(x|ch|ss|sh)$/i' => '\1es', # search, switch, fix, box, process, address 81 '/([^aeiouy]|qu)y$/i' => '\1ies', # query, ability, agency 82 '/(hive)$/i' => '\1s', # archive, hive 83 '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', # half, safe, wife 84 '/sis$/i' => 'ses', # basis, diagnosis 85 '/([ti])um$/i' => '\1a', # datum, medium 86 '/(p)erson$/i' => '\1eople', # person, salesperson 87 '/(m)an$/i' => '\1en', # man, woman, spokesman 88 '/(c)hild$/i' => '\1hildren', # child 89 '/(buffal|tomat)o$/i' => '\1\2oes', # buffalo, tomato 90 '/us$/' => 'uses', # us 91 '/(alias)/i' => '\1es', # alias 92 '/(octop|vir)us$/i' => '\1i', # octopus, virus - virus has no defined plural (according to Latin/dictionary.com), but viri is better than viruses/viruss 93 '/(ax|cri|test)is$/i' => '\1es', # axis, crisis 94 '/s$/' => 's', # no change (compatibility) 95 '/$/' => 's'); 96 97 $coreUninflectedPlural = array('.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'Amoyese', 98 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus', 'carp', 'chassis', 'clippers', 99 'cod', 'coitus', 'Congoese', 'contretemps', 'corps', 'debris', 'diabetes', 'djinn', 'eland', 'elk', 100 'equipment', 'Faroese', 'flounder', 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti', 101 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings', 'jackanapes', 'Kiplingese', 102 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'mews', 'moose', 'mumps', 'Nankingese', 'news', 103 'nexus', 'Niasese', 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese', 'proceedings', 104 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors', 'sea[- ]bass', 'series', 'Shavese', 'shears', 105 'siemens', 'species', 'swine', 'testes', 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 106 'whiting', 'wildebeest', 'Yengeese',); 107 108 $coreIrregularPlural = array('atlas' => 'atlases', 109 'beef' => 'beefs', 110 'brother' => 'brothers', 111 'child' => 'children', 112 'corpus' => 'corpuses', 113 'cow' => 'cows', 114 'ganglion' => 'ganglions', 115 'genie' => 'genies', 116 'genus' => 'genera', 117 'graffito' => 'graffiti', 118 'hoof' => 'hoofs', 119 'loaf' => 'loaves', 120 'man' => 'men', 121 'money' => 'monies', 122 'mongoose' => 'mongooses', 123 'move' => 'moves', 124 'mythos' => 'mythoi', 125 'numen' => 'numina', 126 'occiput' => 'occiputs', 127 'octopus' => 'octopuses', 128 'opus' => 'opuses', 129 'ox' => 'oxen', 130 'penis' => 'penises', 131 'person' => 'people', 132 'sex' => 'sexes', 133 'soliloquy' => 'soliloquies', 134 'testis' => 'testes', 135 'trilby' => 'trilbys', 136 'turf' => 'turfs',); 137 138 $pluralRules = $corePluralRules; 139 $uninflected = $coreUninflectedPlural; 140 $irregular = $coreIrregularPlural; 141 142 if (file_exists(CONFIGS . 'inflections.php')) { 143 include(CONFIGS.'inflections.php'); 144 $pluralRules = array_merge($corePluralRules, $pluralRules); 145 $uninflected = array_merge($coreUninflectedPlural, $uninflectedPlural); 146 $irregular = array_merge($coreIrregularPlural, $irregularPlural); 147 } 148 $_this->pluralRules = array('pluralRules' => $pluralRules, 'uninflected' => $uninflected, 'irregular' => $irregular); 149 $_this->pluralized = array(); 150 } 151 /** 152 * Return $word in plural form. 153 * 154 * @param string $word Word in singular 155 * @return string Word in plural 156 */ 157 function pluralize($word) { 158 159 $_this =& Inflector::getInstance(); 160 if (!isset($_this->pluralRules) || empty($_this->pluralRules)) { 161 $_this->__initPluralRules(); 162 } 163 164 if (isset($_this->pluralized[$word])) { 165 return $_this->pluralized[$word]; 166 } 167 168 extract($_this->pluralRules); 169 if (!isset($regexUninflected) || !isset($regexIrregular)) { 170 $regexUninflected = __enclose(join( '|', $uninflected)); 171 $regexIrregular = __enclose(join( '|', array_keys($irregular))); 172 $_this->pluralRules['regexUninflected'] = $regexUninflected; 173 $_this->pluralRules['regexIrregular'] = $regexIrregular; 174 } 175 176 if (preg_match('/^(' . $regexUninflected . ')$/i', $word, $regs)) { 177 $_this->pluralized[$word] = $word; 178 return $word; 179 } 180 181 if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) { 182 $_this->pluralized[$word] = $regs[1] . $irregular[strtolower($regs[2])]; 183 return $_this->pluralized[$word]; 184 } 185 186 foreach($pluralRules as $rule => $replacement) { 187 if (preg_match($rule, $word)) { 188 $_this->pluralized[$word] = preg_replace($rule, $replacement, $word); 189 return $_this->pluralized[$word]; 190 } 191 } 192 $_this->pluralized[$word] = $word; 193 return $word; 194 } 195 /** 196 * Initializes singular inflection rules 197 * 198 * @access protected 199 * @return void 200 */ 201 function __initSingularRules() { 202 203 $_this =& Inflector::getInstance(); 204 $coreSingularRules = array('/(s)tatuses$/i' => '\1\2tatus', 205 '/(matr)ices$/i' => '\1ix', 206 '/(vert|ind)ices$/i' => '\1ex', 207 '/^(ox)en/i' => '\1', 208 '/(alias)es$/i' => '\1', 209 '/([octop|vir])i$/i' => '\1us', 210 '/(cris|ax|test)es$/i' => '\1is', 211 '/(shoe)s$/i' => '\1', 212 '/(o)es$/i' => '\1', 213 '/uses$/' => 'us', 214 '/([m|l])ice$/i' => '\1ouse', 215 '/(x|ch|ss|sh)es$/i' => '\1', 216 '/(m)ovies$/i' => '\1\2ovie', 217 '/(s)eries$/i' => '\1\2eries', 218 '/([^aeiouy]|qu)ies$/i' => '\1y', 219 '/([lr])ves$/i' => '\1f', 220 '/(tive)s$/i' => '\1', 221 '/(hive)s$/i' => '\1', 222 '/(drive)s$/i' => '\1', 223 '/([^f])ves$/i' => '\1fe', 224 '/(^analy)ses$/i' => '\1sis', 225 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', 226 '/([ti])a$/i' => '\1um', 227 '/(p)eople$/i' => '\1\2erson', 228 '/(m)en$/i' => '\1an', 229 '/(c)hildren$/i' => '\1\2hild', 230 '/(n)ews$/i' => '\1\2ews', 231 '/s$/i' => ''); 232 233 $coreUninflectedSingular = array('.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*us', '.*ss', 'Amoyese', 234 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus', 'carp', 'chassis', 'clippers', 235 'cod', 'coitus', 'Congoese', 'contretemps', 'corps', 'debris', 'diabetes', 'djinn', 'eland', 'elk', 236 'equipment', 'Faroese', 'flounder', 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti', 237 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings', 'jackanapes', 'Kiplingese', 238 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'mews', 'moose', 'mumps', 'Nankingese', 'news', 239 'nexus', 'Niasese', 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese', 'proceedings', 240 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors', 'sea[- ]bass', 'series', 'Shavese', 'shears', 241 'siemens', 'species', 'swine', 'testes', 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 242 'whiting', 'wildebeest', 'Yengeese',); 243 244 $coreIrregularSingular = array('atlases' => 'atlas', 245 'beefs' => 'beef', 246 'brothers' => 'brother', 247 'children' => 'child', 248 'corpuses' => 'corpus', 249 'cows' => 'cow', 250 'ganglions' => 'ganglion', 251 'genies' => 'genie', 252 'genera' => 'genus', 253 'graffiti' => 'graffito', 254 'hoofs' => 'hoof', 255 'loaves' => 'loaf', 256 'men' => 'man', 257 'monies' => 'money', 258 'mongooses' => 'mongoose', 259 'moves' => 'move', 260 'mythoi' => 'mythos', 261 'numina' => 'numen', 262 'occiputs' => 'occiput', 263 'octopuses' => 'octopus', 264 'opuses' => 'opus', 265 'oxen' => 'ox', 266 'penises' => 'penis', 267 'people' => 'person', 268 'sexes' => 'sex', 269 'soliloquies' => 'soliloquy', 270 'testes' => 'testis', 271 'trilbys' => 'trilby', 272 'turfs' => 'turf',); 273 274 $singularRules = $coreSingularRules; 275 $uninflected = $coreUninflectedSingular; 276 $irregular = $coreIrregularSingular; 277 278 if (file_exists(CONFIGS . 'inflections.php')) { 279 include(CONFIGS.'inflections.php'); 280 $singularRules = array_merge($coreSingularRules, $singularRules); 281 $uninflected = array_merge($coreUninflectedSingular, $uninflectedSingular); 282 $irregular = array_merge($coreIrregularSingular, $irregularSingular); 283 } 284 $_this->singularRules = array('singularRules' => $singularRules, 'uninflected' => $uninflected, 'irregular' => $irregular); 285 $_this->singularized = array(); 286 } 287 /** 288 * Return $word in singular form. 289 * 290 * @param string $word Word in plural 291 * @return string Word in singular 292 */ 293 function singularize($word) { 294 295 $_this =& Inflector::getInstance(); 296 if (!isset($_this->singularRules) || empty($_this->singularRules)) { 297 $_this->__initSingularRules(); 298 } 299 300 if (isset($_this->singularized[$word])) { 301 return $_this->singularized[$word]; 302 } 303 304 extract($_this->singularRules); 305 if (!isset($regexUninflected) || !isset($regexIrregular)) { 306 $regexUninflected = __enclose(join( '|', $uninflected)); 307 $regexIrregular = __enclose(join( '|', array_keys($irregular))); 308 $_this->singularRules['regexUninflected'] = $regexUninflected; 309 $_this->singularRules['regexIrregular'] = $regexIrregular; 310 } 311 312 if (preg_match('/^(' . $regexUninflected . ')$/i', $word, $regs)) { 313 $_this->singularized[$word] = $word; 314 return $word; 315 } 316 317 if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) { 318 $_this->singularized[$word] = $regs[1] . $irregular[strtolower($regs[2])]; 319 return $_this->singularized[$word]; 320 } 321 322 foreach($singularRules as $rule => $replacement) { 323 if (preg_match($rule, $word)) { 324 $_this->singularized[$word] = preg_replace($rule, $replacement, $word); 325 return $_this->singularized[$word]; 326 } 327 } 328 $_this->singularized[$word] = $word; 329 return $word; 330 } 331 /** 332 * Returns given $lower_case_and_underscored_word as a camelCased word. 333 * 334 * @param string $lower_case_and_underscored_word Word to camelize 335 * @return string Camelized word. likeThis. 336 */ 337 function camelize($lowerCaseAndUnderscoredWord) { 338 $replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord))); 339 return $replace; 340 } 341 /** 342 * Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word. 343 * 344 * @param string $camel_cased_word Camel-cased word to be "underscorized" 345 * @return string Underscore-syntaxed version of the $camel_cased_word 346 */ 347 function underscore($camelCasedWord) { 348 $replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord)); 349 return $replace; 350 } 351 /** 352 * Returns a human-readable string from $lower_case_and_underscored_word, 353 * by replacing underscores with a space, and by upper-casing the initial characters. 354 * 355 * @param string $lower_case_and_underscored_word String to be made more readable 356 * @return string Human-readable string 357 */ 358 function humanize($lowerCaseAndUnderscoredWord) { 359 $replace = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)); 360 return $replace; 361 } 362 /** 363 * Returns corresponding table name for given $class_name. ("posts" for the model class "Post"). 364 * 365 * @param string $class_name Name of class to get database table name for 366 * @return string Name of the database table for given class 367 */ 368 function tableize($className) { 369 $replace = Inflector::pluralize(Inflector::underscore($className)); 370 return $replace; 371 } 372 /** 373 * Returns Cake model class name ("Post" for the database table "posts".) for given database table. 374 * 375 * @param string $tableName Name of database table to get class name for 376 * @return string 377 */ 378 function classify($tableName) { 379 $replace = Inflector::camelize(Inflector::singularize($tableName)); 380 return $replace; 381 } 382 /** 383 * Returns camelBacked version of a string. 384 * 385 * @param string $string 386 * @return string 387 */ 388 function variable($string) { 389 $string = Inflector::camelize(Inflector::underscore($string)); 390 $replace = strtolower(substr($string, 0, 1)); 391 $variable = preg_replace('/\\w/', $replace, $string, 1); 392 return $variable; 393 } 394 } 395 396 /** 397 * Enter description here... 398 * 399 * @param unknown_type $string 400 * @return unknown 401 */ 402 function __enclose($string) { 403 return '(?:' . $string . ')'; 404 } 405 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |