Changeset 138 for trunk/trax/vendor/trax/inflector.php
- Timestamp:
- 02/23/06 20:09:13 (6 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/inflector.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/inflector.php
r137 r138 1 1 <?php 2 # $Id$ 3 # 4 # Copyright (c) 2005 John Peterson 5 # 6 # Permission is hereby granted, free of charge, to any person obtaining 7 # a copy of this software and associated documentation files (the 8 # "Software"), to deal in the Software without restriction, including 9 # without limitation the rights to use, copy, modify, merge, publish, 10 # distribute, sublicense, and/or sell copies of the Software, and to 11 # permit persons to whom the Software is furnished to do so, subject to 12 # the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be 15 # included in all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2 /** 3 * File containing the Inflector class 4 * 5 * (PHP 5) 6 * 7 * @package PHPonTrax 8 * @version $Id$ 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 */ 24 30 31 /** 32 * Inflector contains static methods to convert English words between 33 * singular and plural, and phrases between the camel case form and 34 * the lower case underscore form. For details see the 35 * {@tutorial PHPonTrax/Inflector.cls} tutorial. 36 * 37 * @package PHPonTrax 38 * 39 */ 25 40 class Inflector { 26 41 42 /** 43 * Rules for converting an English singular word to plural form 44 */ 27 45 private static $plural_rules = 28 46 array( '/(x|ch|ss|sh)$/' => '\1es', # search, switch, fix, box, process, address … … 41 59 ); 42 60 61 /** 62 * Rules for converting an English plural word to singular form 63 */ 43 64 private static $singular_rules = 44 65 array( '/(x|ch|ss)es$/' => '\1', … … 58 79 ); 59 80 81 /** 82 * Pluralize a word according to English rules 83 * 84 * Convert a lower-case singular word to plural form. 85 * @param string $word Word to be pluralized 86 * @return string Plural of $word 87 */ 60 88 function pluralize($word) { 61 89 $original = $word; … … 67 95 } 68 96 97 /** 98 * Singularize a word according to English rules 99 * 100 * @param string $word Word to be singularized 101 * @return string Singular of $word 102 */ 69 103 function singularize($word) { 70 104 $original = $word; … … 76 110 } 77 111 112 /** 113 * Convert a phrase from the lower case and underscored form 114 * to the camel case form 115 * 116 * @param string $lower_case_and_underscored_word Phrase to 117 * convert 118 * @return string Camel case form of the phrase 119 */ 78 120 function camelize($lower_case_and_underscored_word) { 79 121 return str_replace(" ","",ucwords(str_replace("_"," ",$lower_case_and_underscored_word))); 80 122 } 81 123 124 /** 125 * Convert a phrase from the camel case form to the lower case 126 * and underscored form 127 * 128 * @param string $camel_cased_word Phrase to convert 129 * @return string Lower case and underscored form of the phrase 130 */ 82 131 function underscore($camel_cased_word) { 83 132 $camel_cased_word = preg_replace('/([A-Z]+)([A-Z])/','\1_\2',$camel_cased_word); … … 85 134 } 86 135 136 /** 137 * Generate a more human version of a lower case underscored word 138 * 139 * @param string $lower_case_and_underscored_word A word or phrase in 140 * lower_case_underscore form 141 * @return string The input value with underscores replaced by 142 * blanks and the first letter of each word capitalized 143 */ 87 144 function humanize($lower_case_and_underscored_word) { 88 145 return ucwords(str_replace("_"," ",$lower_case_and_underscored_word)); 89 146 } 90 147 148 /** 149 * Convert a class name to the corresponding table name 150 * 151 * The class name is a singular word or phrase in CamelCase. 152 * By convention it corresponds to a table whose name is a plural 153 * word or phrase in lower case underscore form. 154 * @param string $class_name Name of {@link ActiveRecord} sub-class 155 * @return string Pluralized lower_case_underscore form of name 156 */ 91 157 function tableize($class_name) { 92 158 return self::pluralize(self::underscore($class_name)); 93 159 } 94 160 161 /** 162 * Convert a table name to the corresponding class name 163 * 164 * @param string $table_name Name of table in the database 165 * @return string Singular CamelCase form of $table_name 166 */ 95 167 function classify($table_name) { 96 168 return self::camelize(self::singularize($table_name)); 97 169 } 98 170 171 /** 172 * Get foreign key column corresponding to a table name 173 * 174 * @param string $table_name Name of table referenced by foreign 175 * key 176 * @return string Column name of the foreign key column 177 */ 99 178 function foreign_key($class_name) { 100 179 return self::underscore($class_name) . "_id";
