#0001
#0002
#0003
#0004
#0005
#0006
#0007
#0008
#0009
#0010
#0011
#0012
#0013
#0014
#0015
#0016
#0017
#0018
#0019
#0020
#0021
#0022
#0023
#0024
#0025
#0026
#0027
#0028
#0029
#0030
#0031
#0032
#0033
#0034
#0035
#0036
#0037
#0038
#0039
#0040
#0041
#0042
#0043
#0044
#0045
#0046
#0047
#0048
#0049
#0050
#0051
#0052
#0053
#0054
#0055
#0056
#0057
#0058
#0059
#0060
#0061
#0062
#0063
#0064
#0065
#0066
#0067
#0068
#0069
#0070
#0071
#0072
#0073
#0074
#0075
#0076
#0077
#0078
#0079
#0080
#0081
#0082
#0083
#0084
#0085
#0086
#0087
#0088
#0089
#0090
#0091
#0092
#0093
#0094
#0095
#0096
#0097
#0098
#0099
#0100
#0101
#0102
#0103
#0104
#0105
#0106
#0107
#0108
#0109
#0110
#0111
#0112
#0113
#0114
#0115
#0116
#0117
#0118
#0119
#0120
#0121
#0122
#0123
#0124
#0125
#0126
#0127
#0128
#0129
#0130
#0131
#0132
#0133
#0134
#0135
#0136
#0137
#0138
#0139
#0140
#0141
#0142
#0143
#0144
#0145
#0146
#0147
#0148
#0149
#0150
#0151
#0152
#0153
#0154
#0155
#0156
#0157
#0158
#0159
#0160
#0161
#0162
#0163
#0164
#0165
#0166
#0167
#0168
#0169
#0170
#0171
#0172
#0173
#0174
#0175
#0176
#0177
#0178
#0179
#0180
#0181
#0182
#0183
#0184
#0185
#0186
#0187
#0188
#0189
#0190
#0191
#0192
#0193
#0194
#0195
#0196
#0197
#0198
#0199
#0200
#0201
#0202
#0203
#0204
#0205
#0206
#0207
#0208
#0209
#0210
#0211
#0212
#0213
#0214
#0215
#0216
#0217
#0218
#0219
#0220
#0221
#0222
#0223
#0224
#0225
#0226
#0227
#0228
#0229
#0230
#0231
#0232
#0233
#0234
#0235
#0236
#0237
#0238
#0239
#0240
#0241
#0242
#0243
#0244
#0245
#0246
#0247
#0248
#0249
#0250
#0251
#0252
#0253
#0254
#0255
#0256
#0257
#0258
#0259
#0260
#0261
#0262
#0263
#0264
#0265
#0266
#0267
#0268
#0269
#0270
#0271
#0272
#0273
#0274
#0275
#0276
#0277
#0278
#0279
#0280
#0281
#0282
#0283
#0284
#0285
#0286
#0287
#0288
#0289
#0290
#0291
#0292
#0293
#0294
#0295
#0296
#0297
#0298
#0299
#0300
#0301
#0302
#0303
#0304
#0305
#0306
#0307
#0308
#0309
#0310
#0311
#0312
#0313
#0314
#0315
#0316
#0317
#0318
#0319
#0320
#0321
#0322
#0323
#0324
#0325
#0326
#0327
#0328
#0329
#0330
#0331
#0332
#0333
#0334
#0335
#0336
#0337
#0338
#0339
#0340
#0341
#0342
#0343
#0344
#0345
#0346
#0347
#0348
#0349
#0350
#0351
#0352
#0353
#0354
#0355
#0356
#0357
#0358
#0359
#0360
#0361
#0362
#0363
#0364
#0365
#0366
#0367
#0368
#0369
#0370
#0371
#0372
#0373
#0374
#0375
#0376
#0377
#0378
#0379
|
<?php /* * Inflector class slightly modified for creovel. */
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+ // | Akelos PHP Application Framework | // +----------------------------------------------------------------------+ // | Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.com/ | // | Released under the GNU Lesser General Public License | // +----------------------------------------------------------------------+ // | You should have received the following files along with this library | // | - COPYRIGHT (Additional copyright notice) | // | - DISCLAIMER (Disclaimer of warranty) | // | - README (Important information regarding this library) | // +----------------------------------------------------------------------+
/** * Inflector for pluralize and singularize English nouns. * * This Inflector is a port of Ruby on Rails Inflector. * * It can be really helpful for developers that want to * create frameworks based on naming conventions rather than * configurations. * * It was ported to PHP for the Akelos Framework, a * multilingual Ruby on Rails like framework for PHP that will * be launched soon. * * @author Bermi Ferrer Martinez <bermi akelos com> * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html> * @since 0.1 * @version $Revision 0.1 $ */ class inflector { // ------ CLASS METHODS ------ //
// ---- Public methods ---- //
// {{{ pluralize()
/** * Pluralizes English nouns. * * @access public * @static * @param string $word English noun to pluralize * @return string Plural noun */ public function pluralize($word) { $plural = array( '/(quiz)$/i' => '\1zes', '/^(ox)$/i' => '\1en', '/([m|l])ouse$/i' => '\1ice', '/(matr|vert|ind)ix|ex$/i' => '\1ices', '/(x|ch|ss|sh)$/i' => '\1es', '/([^aeiouy]|qu)ies$/i' => '\1y', '/([^aeiouy]|qu)y$/i' => '\1ies', '/(hive)$/i' => '\1s', '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', '/sis$/i' => 'ses', '/([ti])um$/i' => '\1a', '/(buffal|tomat)o$/i' => '\1oes', '/(bu)s$/i' => '\1ses', '/(alias|status)/i'=> '\1es', '/(octop|vir)us$/i'=> '\1i', '/(ax|test)is$/i'=> '\1es', '/s$/i'=> 's', '/$/'=> 's');
$uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
$irregular = array( 'person' => 'people', 'man' => 'men', 'child' => 'children', 'sex' => 'sexes', 'move' => 'moves');
$lowercased_word = strtolower($word);
foreach ($uncountable as $_uncountable){ if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){ return $word; } }
foreach ($irregular as $_plural=> $_singular){ if (preg_match('/('.$_plural.')$/i', $word, $arr)) { return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word); } }
foreach ($plural as $rule => $replacement) { if (preg_match($rule, $word)) { return preg_replace($rule, $replacement, $word); } } return false;
}
// }}} // {{{ singularize()
/** * Singularizes English nouns. * * @access public * @static * @param string $word English noun to singularize * @return string Singular noun. */ public function singularize($word) { $singular = array ( '/(quiz)zes$/i' => '\\1', '/(matr)ices$/i' => '\\1ix', '/(vert|ind)ices$/i' => '\\1ex', '/^(ox)en/i' => '\\1', '/(alias|status)es$/i' => '\\1', '/([octop|vir])i$/i' => '\\1us', '/(cris|ax|test)es$/i' => '\\1is', '/(shoe)s$/i' => '\\1', '/(o)es$/i' => '\\1', '/(bus)es$/i' => '\\1', '/([m|l])ice$/i' => '\\1ouse', '/(x|ch|ss|sh)es$/i' => '\\1', '/(m)ovies$/i' => '\\1ovie', '/(s)eries$/i' => '\\1eries', '/([^aeiouy]|qu)ies$/i' => '\\1y', '/([lr])ves$/i' => '\\1f', '/(tive)s$/i' => '\\1', '/(hive)s$/i' => '\\1', '/([^f])ves$/i' => '\\1fe', '/(^analy)ses$/i' => '\\1sis', '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\\1\\2sis', '/([ti])a$/i' => '\\1um', '/(n)ews$/i' => '\\1ews', '/s$/i' => '', );
$uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
$irregular = array( 'person' => 'people', 'man' => 'men', 'child' => 'children', 'sex' => 'sexes', 'move' => 'moves');
$lowercased_word = strtolower($word); foreach ($uncountable as $_uncountable){ if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){ return $word; } }
foreach ($irregular as $_plural=> $_singular){ if (preg_match('/('.$_singular.')$/i', $word, $arr)) { return preg_replace('/('.$_singular.')$/i', substr($arr[0],0,1).substr($_plural,1), $word); } }
foreach ($singular as $rule => $replacement) { if (preg_match($rule, $word)) { return preg_replace($rule, $replacement, $word); } }
return $word; }
// }}} // {{{ titleize()
/** * Converts an underscored or CamelCase word into a English * sentence. * * The titleize public function converts text like "WelcomePage", * "welcome_page" or "welcome page" to this "Welcome * Page". * If second parameter is set to 'first' it will only * capitalize the first character of the title. * * @access public * @static * @param string $word Word to format as tile * @param string $uppercase If set to 'first' it will only uppercase the * first character. Otherwise it will uppercase all * the words in the title. * @return string Text formatted as title */ public function titleize($word, $uppercase = '') { $uppercase = $uppercase == 'first' ? 'ucfirst' : 'ucwords'; return $uppercase(self::humanize(self::underscore($word))); }
// }}} // {{{ camelize()
/** * Returns given word as CamelCased * * Converts a word like "send_email" to "SendEmail". It * will remove non alphanumeric character from the word, so * "who's online" will be converted to "WhoSOnline" * * @access public * @static * @see variablize * @param string $word Word to convert to camel case * @return string UpperCamelCasedWord */ public function camelize($word) { return str_replace(' ','',ucwords(preg_replace('/[^A-Z^a-z^0-9]+/',' ',$word))); }
// }}} // {{{ underscore()
/** * Converts a word "into_it_s_underscored_version" * * Convert any "CamelCased" or "ordinary Word" into an * "underscored_word". * * This can be really useful for creating friendly URLs. * * @access public * @static * @param string $word Word to underscore * @return string Underscored word */ public function underscore($word) { return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/','_', preg_replace('/([a-z\d])([A-Z])/','\1_\2', preg_replace('/([A-Z]+)([A-Z][a-z])/','\1_\2',$word)))); }
// }}} // {{{ humanize()
/** * Returns a human-readable string from $word
* * Returns a human-readable string from $word, by replacing * underscores with a space, and by upper-casing the initial * character by default. * * If you need to uppercase all the words you just have to * pass 'all' as a second parameter. * * @access public * @static * @param string $word String to "humanize" * @param string $uppercase If set to 'all' it will uppercase all the words * instead of just the first one. * @return string Human-readable word */ public function humanize($word, $uppercase = '') { $uppercase = $uppercase == 'all' ? 'ucwords' : 'ucfirst'; return $uppercase(str_replace('_',' ',preg_replace('/_id$/', '',$word))); }
// }}} // {{{ variablize()
/** * Same as camelize but first char is underscored * * Converts a word like "send_email" to "sendEmail". It * will remove non alphanumeric character from the word, so * "who's online" will be converted to "whoSOnline" * * @access public * @static * @see camelize * @param string $word Word to lowerCamelCase * @return string Returns a lowerCamelCasedWord */ public function variablize($word) { $word = self::camelize($word); return strtolower($word[0]).substr($word,1); }
// }}} // {{{ tableize()
/** * Converts a class name to its table name according to rails * naming conventions. * * Converts "Person" to "people" * * @access public * @static * @see classify * @param string $class_name Class name for getting related table_name. * @return string plural_table_name */ public function tableize($class_name) { return self::pluralize(self::underscore($class_name)); }
// }}} // {{{ classify()
/** * Converts a table name to its class name according to rails * naming conventions. * * Converts "people" to "Person" * * @access public * @static * @see tableize * @param string $table_name Table name for getting related ClassName. * @return string SingularClassName */ public function classify($table_name) { return self::camelize(self::singularize($table_name)); }
// }}} // {{{ ordinalize()
/** * Converts number to its ordinal English form. * * This method converts 13 to 13th, 2 to 2nd ... * * @access public * @static * @param integer $number Number to get its ordinal value * @return string Ordinal representation of given string. */ public function ordinalize($number) { if (in_array(($number % 100),range(11,13))){ return $number.'th'; }else{ switch (($number % 10)) { case 1: return $number.'st'; break; case 2: return $number.'nd'; break; case 3: return $number.'rd'; default: return $number.'th'; break; } } }
// }}}
} ?>
|