[ Index ] |
|
Code source de PHPonTrax 2.6.6-svn |
1 <?php 2 /** 3 * File containing the Inflector class 4 * 5 * (PHP 5) 6 * 7 * @package PHPonTrax 8 * @version $Id: inflector.php 201 2006-05-25 08:58:10Z john $ 9 * @copyright (c) 2005 John Peterson 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining 12 * a copy of this software and associated documentation files (the 13 * "Software"), to deal in the Software without restriction, including 14 * without limitation the rights to use, copy, modify, merge, publish, 15 * distribute, sublicense, and/or sell copies of the Software, and to 16 * permit persons to whom the Software is furnished to do so, subject to 17 * the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be 20 * included in all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 */ 30 31 32 include_once (TRAX_LIB_ROOT . "/inflections.php"); 33 34 /** 35 * Implement the Trax naming convention 36 * 37 * This class provides static methods to implement the 38 * {@tutorial PHPonTrax/naming.pkg Trax naming convention}. 39 * Inflector is never instantiated. 40 * @tutorial PHPonTrax/Inflector.cls 41 */ 42 class Inflector { 43 44 /** 45 * Pluralize a word according to English rules 46 * 47 * Convert a lower-case singular word to plural form. 48 * @param string $word Word to be pluralized 49 * @return string Plural of $word 50 */ 51 function pluralize($word) { 52 if(!in_array($word, Inflections::$uncountables)) { 53 $original = $word; 54 foreach(Inflections::$plurals as $plural_rule) { 55 $word = preg_replace($plural_rule['rule'], $plural_rule['replacement'], $word); 56 if($original != $word) break; 57 } 58 } 59 return $word; 60 } 61 62 /** 63 * Singularize a word according to English rules 64 * 65 * @param string $word Word to be singularized 66 * @return string Singular of $word 67 */ 68 function singularize($word) { 69 if(!in_array($word, Inflections::$uncountables)) { 70 $original = $word; 71 foreach(Inflections::$singulars as $singular_rule) { 72 $word = preg_replace($singular_rule['rule'], $singular_rule['replacement'], $word); 73 if($original != $word) break; 74 } 75 } 76 return $word; 77 } 78 79 /** 80 * Capitalize a word making it all lower case with first letter uppercase 81 * 82 * @param string $word Word to be capitalized 83 * @return string Capitalized $word 84 */ 85 function capitalize($word) { 86 return ucfirst(strtolower($word)); 87 } 88 89 /** 90 * Convert a phrase from the lower case and underscored form 91 * to the camel case form 92 * 93 * @param string $lower_case_and_underscored_word Phrase to 94 * convert 95 * @return string Camel case form of the phrase 96 */ 97 function camelize($lower_case_and_underscored_word) { 98 return str_replace(" ","",ucwords(str_replace("_"," ",$lower_case_and_underscored_word))); 99 } 100 101 /** 102 * Convert a phrase from the camel case form to the lower case 103 * and underscored form 104 * 105 * @param string $camel_cased_word Phrase to convert 106 * @return string Lower case and underscored form of the phrase 107 */ 108 function underscore($camel_cased_word) { 109 $camel_cased_word = preg_replace('/([A-Z]+)([A-Z])/','\1_\2',$camel_cased_word); 110 return strtolower(preg_replace('/([a-z])([A-Z])/','\1_\2',$camel_cased_word)); 111 } 112 113 /** 114 * Generate a more human version of a lower case underscored word 115 * 116 * @param string $lower_case_and_underscored_word A word or phrase in 117 * lower_case_underscore form 118 * @return string The input value with underscores replaced by 119 * blanks and the first letter of each word capitalized 120 */ 121 function humanize($lower_case_and_underscored_word) { 122 return ucwords(str_replace("_"," ",$lower_case_and_underscored_word)); 123 } 124 125 /** 126 * Convert a word or phrase into a title format "Welcome To My Site" 127 * 128 * @param string $word A word or phrase 129 * @return string A string that has all words capitalized and splits on existing caps. 130 */ 131 function titleize($word) { 132 return preg_replace('/\b([a-z])/', self::capitalize('$1'), self::humanize(self::underscore($word))); 133 } 134 135 /** 136 * Convert a word's underscores into dashes 137 * 138 * @param string $underscored_word Word to convert 139 * @return string All underscores converted to dashes 140 */ 141 function dasherize($underscored_word) { 142 return str_replace('_', '-', self::underscore($underscored_word)); 143 } 144 145 /** 146 * Convert a class name to the corresponding table name 147 * 148 * The class name is a singular word or phrase in CamelCase. 149 * By convention it corresponds to a table whose name is a plural 150 * word or phrase in lower case underscore form. 151 * @param string $class_name Name of {@link ActiveRecord} sub-class 152 * @return string Pluralized lower_case_underscore form of name 153 */ 154 function tableize($class_name) { 155 return self::pluralize(self::underscore($class_name)); 156 } 157 158 /** 159 * Convert a table name to the corresponding class name 160 * 161 * @param string $table_name Name of table in the database 162 * @return string Singular CamelCase form of $table_name 163 */ 164 function classify($table_name) { 165 return self::camelize(self::singularize($table_name)); 166 } 167 168 /** 169 * Get foreign key column corresponding to a table name 170 * 171 * @param string $table_name Name of table referenced by foreign 172 * key 173 * @return string Column name of the foreign key column 174 */ 175 function foreign_key($class_name) { 176 return self::underscore($class_name) . "_id"; 177 } 178 179 180 /** 181 * Add to a number st, nd, rd, th 182 * 183 * @param integer $number Number to append to 184 * key 185 * @return string Number formatted with correct st, nd, rd, or th 186 */ 187 function ordinalize($number) { 188 $test = (intval($number) % 100); 189 if($test >= 11 && $test <= 13) { 190 $number = "{$number}th"; 191 } else { 192 switch((intval($number) % 10)) { 193 case 1: 194 $number = "{$number}st"; 195 break; 196 case 2: 197 $number = "{$number}nd"; 198 break; 199 case 3: 200 $number = "{$number}rd"; 201 break; 202 default: 203 $number = "{$number}th"; 204 } 205 } 206 return $number; 207 } 208 209 } 210 211 212 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 20:04:38 2007 | par Balluche grâce à PHPXref 0.7 |