Changeset 453626
- Timestamp:
- 10/20/2011 10:06:47 PM (14 years ago)
- Location:
- wp-mvc
- Files:
-
- 2 edited
-
tags/1.1.1/core/mvc_inflector.php (modified) (5 diffs)
-
trunk/core/mvc_inflector.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-mvc/tags/1.1.1/core/mvc_inflector.php
r390534 r453626 2 2 3 3 class MvcInflector { 4 5 public function class_name_from_filename($filename) { 4 5 // Plural inflector rules 6 protected static $_plural = array( 7 'rules' => array( 8 '/status$/i' => 'statuses', 9 '/quiz$/i' => 'quizzes', 10 '/^ox$/i' => '\oxen', 11 '/([m|l])ouse$/i' => '\1ice', 12 '/(matr|vert|ind)(ix|ex)$/i' => '\1ices', 13 '/(x|ch|ss|sh)$/i' => '\1es', 14 '/([^aeiouy]|qu)y$/i' => '\1ies', 15 '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', 16 '/sis$/i' => 'ses', 17 '/([ti])um$/i' => '\1a', 18 '/person$/i' => 'people', 19 '/man$/i' => 'men', 20 '/child$/i' => 'children', 21 '/(buffal|tomat)o$/i' => '\1\2oes', 22 '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', 23 '/us$/i' => 'uses', 24 '/alias$/i' => 'aliases', 25 '/(ax|cris|test)is$/i' => '\1es', 26 '/s$/' => 's', 27 '/^$/' => '', 28 '/$/' => 's', 29 ), 30 'uninflected' => array( 31 '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people' 32 ), 33 'irregular' => array( 34 'atlas' => 'atlases', 35 'beef' => 'beefs', 36 'brother' => 'brothers', 37 'cafe' => 'cafes', 38 'child' => 'children', 39 'corpus' => 'corpuses', 40 'cow' => 'cows', 41 'ganglion' => 'ganglions', 42 'genie' => 'genies', 43 'genus' => 'genera', 44 'graffito' => 'graffiti', 45 'hoof' => 'hoofs', 46 'loaf' => 'loaves', 47 'man' => 'men', 48 'money' => 'monies', 49 'mongoose' => 'mongooses', 50 'move' => 'moves', 51 'mythos' => 'mythoi', 52 'niche' => 'niches', 53 'numen' => 'numina', 54 'occiput' => 'occiputs', 55 'octopus' => 'octopuses', 56 'opus' => 'opuses', 57 'ox' => 'oxen', 58 'person' => 'people', 59 'sex' => 'sexes', 60 'soliloquy' => 'soliloquies', 61 'trilby' => 'trilbys', 62 'turf' => 'turfs' 63 ) 64 ); 65 66 // Singular inflector rules 67 protected static $_singular = array( 68 'rules' => array( 69 '/statuses$/i' => 'status', 70 '/quizzes$/i' => 'quiz', 71 '/matrices$/i' => 'matrix', 72 '/(vert|ind)ices$/i' => '\1ex', 73 '/^oxen/i' => 'ox', 74 '/aliases$/i' => 'alias', 75 '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', 76 '/([ftw]ax)es/i' => '\1', 77 '/(cris|ax|test)es$/i' => '\1is', 78 '/(shoe|slave)s$/i' => '\1', 79 '/oes$/i' => 'o', 80 '/ouses$/' => 'ouse', 81 '/([^a])uses$/' => '\1us', 82 '/([m|l])ice$/i' => '\1ouse', 83 '/(x|ch|ss|sh)es$/i' => '\1', 84 '/([^aeiouy]|qu)ies$/i' => '\1y', 85 '/([lr])ves$/i' => '\1f', 86 '/([^fo])ves$/i' => '\1fe', 87 '/^analyses$/i' => 'analysis', 88 '/(analy|ba|diagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', 89 '/([ti])a$/i' => '\1um', 90 '/people$/i' => 'person', 91 '/men$/i' => 'man', 92 '/children$/i' => 'child', 93 '/eaus$/' => 'eau', 94 '/^(.*us)$/' => '\\1', 95 '/s$/i' => '' 96 ), 97 'uninflected' => array( 98 '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss' 99 ), 100 'irregular' => array( 101 'waves' => 'wave', 102 'curves' => 'curve' 103 ) 104 ); 105 106 // Words that should not be inflected 107 protected static $_uninflected = array( 108 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus', 109 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps', 110 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder', 111 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti', 112 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings', 113 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media', 114 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese', 115 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese', 116 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors', 117 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes', 118 'trousers', 'trout','tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest', 119 'Yengeese' 120 ); 121 122 protected static $_cache = array(); 123 124 protected static function set_cached_patterns() { 125 126 self::$_singular['merged'] = array(); 127 self::$_singular['merged']['irregular'] = array_merge(self::$_singular['irregular'], array_flip(self::$_plural['irregular'])); 128 self::$_singular['merged']['uninflected'] = array_merge(self::$_singular['uninflected'], self::$_uninflected); 129 130 self::$_singular['cached'] = array(); 131 self::$_singular['cached']['irregular'] = '(?:'.join('|', array_keys(self::$_singular['merged']['irregular'])).')'; 132 self::$_singular['cached']['uninflected'] = '(?:'.join('|', self::$_singular['merged']['uninflected']).')'; 133 134 self::$_plural['merged'] = array(); 135 self::$_plural['merged']['irregular'] = array_merge(self::$_plural['irregular'], array_flip(self::$_singular['irregular'])); 136 self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected); 137 138 self::$_plural['cached'] = array(); 139 self::$_plural['cached']['irregular'] = '(?:'.join('|', array_keys(self::$_plural['merged']['irregular'])).')'; 140 self::$_plural['cached']['uninflected'] = '(?:'.join('|', self::$_plural['merged']['uninflected']).')'; 141 142 } 143 144 public static function class_name_from_filename($filename) { 6 145 return MvcInflector::camelize(str_replace('.php', '', $filename)); 7 146 } 8 147 9 public function camelize($string) {148 public static function camelize($string) { 10 149 $string = str_replace('_', ' ', $string); 11 150 $string = ucwords($string); … … 14 153 } 15 154 16 public function tableize($string) {155 public static function tableize($string) { 17 156 $string = MvcInflector::underscore($string); 18 157 $string = MvcInflector::pluralize($string); … … 20 159 } 21 160 22 public function underscore($string) {161 public static function underscore($string) { 23 162 $string = preg_replace('/[A-Z]/', ' $0', $string); 24 163 $string = trim(strtolower($string)); … … 27 166 } 28 167 29 public function pluralize($string) { 30 return $string.'s'; 31 } 32 33 public function singularize($string) { 34 return preg_replace('/s$/', '', $string); 35 } 36 37 public function titleize($string) { 168 public static function pluralize($string) { 169 170 if (isset(self::$_cache['pluralize'][$string])) { 171 return self::$_cache['pluralize'][$string]; 172 } 173 174 if (!isset($_plural['cached'])) { 175 self::set_cached_patterns(); 176 } 177 178 if (preg_match('/(.*)\\b('.self::$_plural['cached']['irregular'].')$/i', $string, $regs)) { 179 self::$_cache['pluralize'][$string] = $regs[1].substr($string, 0, 1).substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1); 180 return self::$_cache['pluralize'][$string]; 181 } 182 183 if (preg_match('/^('.self::$_plural['cached']['uninflected'].')$/i', $string, $regs)) { 184 self::$_cache['pluralize'][$string] = $string; 185 return $string; 186 } 187 188 foreach (self::$_plural['rules'] as $rule => $replacement) { 189 if (preg_match($rule, $string)) { 190 self::$_cache['pluralize'][$string] = preg_replace($rule, $replacement, $string); 191 return self::$_cache['pluralize'][$string]; 192 } 193 } 194 195 self::$_cache['pluralize'][$string] = $string; 196 197 return $string; 198 199 } 200 201 public static function singularize($string) { 202 203 if (isset(self::$_cache['singularize'][$string])) { 204 return self::$_cache['singularize'][$string]; 205 } 206 207 if (!isset($_singular['cached'])) { 208 self::set_cached_patterns(); 209 } 210 211 if (preg_match('/(.*)\\b('.self::$_singular['cached']['irregular'].')$/i', $string, $regs)) { 212 self::$_cache['singularize'][$string] = $regs[1].substr($string, 0, 1).substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1); 213 return self::$_cache['singularize'][$string]; 214 } 215 216 if (preg_match('/^('.self::$_singular['cached']['uninflected'].')$/i', $string, $regs)) { 217 self::$_cache['singularize'][$string] = $string; 218 return $string; 219 } 220 221 foreach (self::$_singular['rules'] as $rule => $replacement) { 222 if (preg_match($rule, $string)) { 223 self::$_cache['singularize'][$string] = preg_replace($rule, $replacement, $string); 224 return self::$_cache['singularize'][$string]; 225 } 226 } 227 228 self::$_cache['singularize'][$string] = $string; 229 230 return $string; 231 232 } 233 234 public static function titleize($string) { 38 235 $string = preg_replace('/[A-Z]/', ' $0', $string); 39 236 $string = trim(str_replace('_', ' ', $string)); … … 42 239 } 43 240 44 public function pluralize_titleize($string) {241 public static function pluralize_titleize($string) { 45 242 $string = MvcInflector::pluralize(MvcInflector::titleize($string)); 46 243 return $string; 47 244 } 48 245 246 public static function rules($type, $rules, $reset = false) { 247 248 $variable_name = '_'.$type; 249 250 foreach ($rules as $rule => $pattern) { 251 if (is_array($pattern)) { 252 if ($reset) { 253 self::${$var}[$rule] = $pattern; 254 } else { 255 if ($rule === 'uninflected') { 256 self::${$variable_name}[$rule] = array_merge($pattern, self::${$variable_name}[$rule]); 257 } else { 258 self::${$variable_name}[$rule] = $pattern + self::${$variable_name}[$rule]; 259 } 260 } 261 unset($rules[$rule], self::${$variable_name}['cached'][$rule]); 262 if (isset(self::${$variable_name}['merged'][$rule])) { 263 unset(self::${$variable_name}['merged'][$rule]); 264 } 265 if ($type === 'plural') { 266 self::$_cache['pluralize'] = self::$_cache['tableize'] = array(); 267 } elseif ($type === 'singular') { 268 self::$_cache['singularize'] = array(); 269 } 270 } 271 } 272 273 self::${$variable_name}['rules'] = $rules + self::${$variable_name}['rules']; 274 275 } 276 49 277 } 50 278 -
wp-mvc/trunk/core/mvc_inflector.php
r390534 r453626 2 2 3 3 class MvcInflector { 4 5 public function class_name_from_filename($filename) { 4 5 // Plural inflector rules 6 protected static $_plural = array( 7 'rules' => array( 8 '/status$/i' => 'statuses', 9 '/quiz$/i' => 'quizzes', 10 '/^ox$/i' => '\oxen', 11 '/([m|l])ouse$/i' => '\1ice', 12 '/(matr|vert|ind)(ix|ex)$/i' => '\1ices', 13 '/(x|ch|ss|sh)$/i' => '\1es', 14 '/([^aeiouy]|qu)y$/i' => '\1ies', 15 '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', 16 '/sis$/i' => 'ses', 17 '/([ti])um$/i' => '\1a', 18 '/person$/i' => 'people', 19 '/man$/i' => 'men', 20 '/child$/i' => 'children', 21 '/(buffal|tomat)o$/i' => '\1\2oes', 22 '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', 23 '/us$/i' => 'uses', 24 '/alias$/i' => 'aliases', 25 '/(ax|cris|test)is$/i' => '\1es', 26 '/s$/' => 's', 27 '/^$/' => '', 28 '/$/' => 's', 29 ), 30 'uninflected' => array( 31 '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people' 32 ), 33 'irregular' => array( 34 'atlas' => 'atlases', 35 'beef' => 'beefs', 36 'brother' => 'brothers', 37 'cafe' => 'cafes', 38 'child' => 'children', 39 'corpus' => 'corpuses', 40 'cow' => 'cows', 41 'ganglion' => 'ganglions', 42 'genie' => 'genies', 43 'genus' => 'genera', 44 'graffito' => 'graffiti', 45 'hoof' => 'hoofs', 46 'loaf' => 'loaves', 47 'man' => 'men', 48 'money' => 'monies', 49 'mongoose' => 'mongooses', 50 'move' => 'moves', 51 'mythos' => 'mythoi', 52 'niche' => 'niches', 53 'numen' => 'numina', 54 'occiput' => 'occiputs', 55 'octopus' => 'octopuses', 56 'opus' => 'opuses', 57 'ox' => 'oxen', 58 'person' => 'people', 59 'sex' => 'sexes', 60 'soliloquy' => 'soliloquies', 61 'trilby' => 'trilbys', 62 'turf' => 'turfs' 63 ) 64 ); 65 66 // Singular inflector rules 67 protected static $_singular = array( 68 'rules' => array( 69 '/statuses$/i' => 'status', 70 '/quizzes$/i' => 'quiz', 71 '/matrices$/i' => 'matrix', 72 '/(vert|ind)ices$/i' => '\1ex', 73 '/^oxen/i' => 'ox', 74 '/aliases$/i' => 'alias', 75 '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', 76 '/([ftw]ax)es/i' => '\1', 77 '/(cris|ax|test)es$/i' => '\1is', 78 '/(shoe|slave)s$/i' => '\1', 79 '/oes$/i' => 'o', 80 '/ouses$/' => 'ouse', 81 '/([^a])uses$/' => '\1us', 82 '/([m|l])ice$/i' => '\1ouse', 83 '/(x|ch|ss|sh)es$/i' => '\1', 84 '/([^aeiouy]|qu)ies$/i' => '\1y', 85 '/([lr])ves$/i' => '\1f', 86 '/([^fo])ves$/i' => '\1fe', 87 '/^analyses$/i' => 'analysis', 88 '/(analy|ba|diagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', 89 '/([ti])a$/i' => '\1um', 90 '/people$/i' => 'person', 91 '/men$/i' => 'man', 92 '/children$/i' => 'child', 93 '/eaus$/' => 'eau', 94 '/^(.*us)$/' => '\\1', 95 '/s$/i' => '' 96 ), 97 'uninflected' => array( 98 '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss' 99 ), 100 'irregular' => array( 101 'waves' => 'wave', 102 'curves' => 'curve' 103 ) 104 ); 105 106 // Words that should not be inflected 107 protected static $_uninflected = array( 108 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus', 109 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps', 110 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder', 111 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti', 112 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings', 113 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media', 114 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese', 115 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese', 116 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors', 117 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes', 118 'trousers', 'trout','tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest', 119 'Yengeese' 120 ); 121 122 protected static $_cache = array(); 123 124 protected static function set_cached_patterns() { 125 126 self::$_singular['merged'] = array(); 127 self::$_singular['merged']['irregular'] = array_merge(self::$_singular['irregular'], array_flip(self::$_plural['irregular'])); 128 self::$_singular['merged']['uninflected'] = array_merge(self::$_singular['uninflected'], self::$_uninflected); 129 130 self::$_singular['cached'] = array(); 131 self::$_singular['cached']['irregular'] = '(?:'.join('|', array_keys(self::$_singular['merged']['irregular'])).')'; 132 self::$_singular['cached']['uninflected'] = '(?:'.join('|', self::$_singular['merged']['uninflected']).')'; 133 134 self::$_plural['merged'] = array(); 135 self::$_plural['merged']['irregular'] = array_merge(self::$_plural['irregular'], array_flip(self::$_singular['irregular'])); 136 self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected); 137 138 self::$_plural['cached'] = array(); 139 self::$_plural['cached']['irregular'] = '(?:'.join('|', array_keys(self::$_plural['merged']['irregular'])).')'; 140 self::$_plural['cached']['uninflected'] = '(?:'.join('|', self::$_plural['merged']['uninflected']).')'; 141 142 } 143 144 public static function class_name_from_filename($filename) { 6 145 return MvcInflector::camelize(str_replace('.php', '', $filename)); 7 146 } 8 147 9 public function camelize($string) {148 public static function camelize($string) { 10 149 $string = str_replace('_', ' ', $string); 11 150 $string = ucwords($string); … … 14 153 } 15 154 16 public function tableize($string) {155 public static function tableize($string) { 17 156 $string = MvcInflector::underscore($string); 18 157 $string = MvcInflector::pluralize($string); … … 20 159 } 21 160 22 public function underscore($string) {161 public static function underscore($string) { 23 162 $string = preg_replace('/[A-Z]/', ' $0', $string); 24 163 $string = trim(strtolower($string)); … … 27 166 } 28 167 29 public function pluralize($string) { 30 return $string.'s'; 31 } 32 33 public function singularize($string) { 34 return preg_replace('/s$/', '', $string); 35 } 36 37 public function titleize($string) { 168 public static function pluralize($string) { 169 170 if (isset(self::$_cache['pluralize'][$string])) { 171 return self::$_cache['pluralize'][$string]; 172 } 173 174 if (!isset($_plural['cached'])) { 175 self::set_cached_patterns(); 176 } 177 178 if (preg_match('/(.*)\\b('.self::$_plural['cached']['irregular'].')$/i', $string, $regs)) { 179 self::$_cache['pluralize'][$string] = $regs[1].substr($string, 0, 1).substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1); 180 return self::$_cache['pluralize'][$string]; 181 } 182 183 if (preg_match('/^('.self::$_plural['cached']['uninflected'].')$/i', $string, $regs)) { 184 self::$_cache['pluralize'][$string] = $string; 185 return $string; 186 } 187 188 foreach (self::$_plural['rules'] as $rule => $replacement) { 189 if (preg_match($rule, $string)) { 190 self::$_cache['pluralize'][$string] = preg_replace($rule, $replacement, $string); 191 return self::$_cache['pluralize'][$string]; 192 } 193 } 194 195 self::$_cache['pluralize'][$string] = $string; 196 197 return $string; 198 199 } 200 201 public static function singularize($string) { 202 203 if (isset(self::$_cache['singularize'][$string])) { 204 return self::$_cache['singularize'][$string]; 205 } 206 207 if (!isset($_singular['cached'])) { 208 self::set_cached_patterns(); 209 } 210 211 if (preg_match('/(.*)\\b('.self::$_singular['cached']['irregular'].')$/i', $string, $regs)) { 212 self::$_cache['singularize'][$string] = $regs[1].substr($string, 0, 1).substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1); 213 return self::$_cache['singularize'][$string]; 214 } 215 216 if (preg_match('/^('.self::$_singular['cached']['uninflected'].')$/i', $string, $regs)) { 217 self::$_cache['singularize'][$string] = $string; 218 return $string; 219 } 220 221 foreach (self::$_singular['rules'] as $rule => $replacement) { 222 if (preg_match($rule, $string)) { 223 self::$_cache['singularize'][$string] = preg_replace($rule, $replacement, $string); 224 return self::$_cache['singularize'][$string]; 225 } 226 } 227 228 self::$_cache['singularize'][$string] = $string; 229 230 return $string; 231 232 } 233 234 public static function titleize($string) { 38 235 $string = preg_replace('/[A-Z]/', ' $0', $string); 39 236 $string = trim(str_replace('_', ' ', $string)); … … 42 239 } 43 240 44 public function pluralize_titleize($string) {241 public static function pluralize_titleize($string) { 45 242 $string = MvcInflector::pluralize(MvcInflector::titleize($string)); 46 243 return $string; 47 244 } 48 245 246 public static function rules($type, $rules, $reset = false) { 247 248 $variable_name = '_'.$type; 249 250 foreach ($rules as $rule => $pattern) { 251 if (is_array($pattern)) { 252 if ($reset) { 253 self::${$var}[$rule] = $pattern; 254 } else { 255 if ($rule === 'uninflected') { 256 self::${$variable_name}[$rule] = array_merge($pattern, self::${$variable_name}[$rule]); 257 } else { 258 self::${$variable_name}[$rule] = $pattern + self::${$variable_name}[$rule]; 259 } 260 } 261 unset($rules[$rule], self::${$variable_name}['cached'][$rule]); 262 if (isset(self::${$variable_name}['merged'][$rule])) { 263 unset(self::${$variable_name}['merged'][$rule]); 264 } 265 if ($type === 'plural') { 266 self::$_cache['pluralize'] = self::$_cache['tableize'] = array(); 267 } elseif ($type === 'singular') { 268 self::$_cache['singularize'] = array(); 269 } 270 } 271 } 272 273 self::${$variable_name}['rules'] = $rules + self::${$variable_name}['rules']; 274 275 } 276 49 277 } 50 278
Note: See TracChangeset
for help on using the changeset viewer.