| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fGrammar |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
|
|---|
| 20 |
class fGrammar |
|---|
| 21 |
{ |
|---|
| 22 |
|
|---|
| 23 |
const addCamelUnderscoreRule = 'fGrammar::addCamelUnderscoreRule'; |
|---|
| 24 |
const addHumanizeRule = 'fGrammar::addHumanizeRule'; |
|---|
| 25 |
const addSingularPluralRule = 'fGrammar::addSingularPluralRule'; |
|---|
| 26 |
const camelize = 'fGrammar::camelize'; |
|---|
| 27 |
const humanize = 'fGrammar::humanize'; |
|---|
| 28 |
const inflectOnQuantity = 'fGrammar::inflectOnQuantity'; |
|---|
| 29 |
const joinArray = 'fGrammar::joinArray'; |
|---|
| 30 |
const pluralize = 'fGrammar::pluralize'; |
|---|
| 31 |
const registerJoinArrayCallback = 'fGrammar::registerJoinArrayCallback'; |
|---|
| 32 |
const reset = 'fGrammar::reset'; |
|---|
| 33 |
const singularize = 'fGrammar::singularize'; |
|---|
| 34 |
const underscorize = 'fGrammar::underscorize'; |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
@var |
|---|
| 41 |
|
|---|
| 42 |
static private $cache = array( |
|---|
| 43 |
'camelize' => array(0 => array(), 1 => array()), |
|---|
| 44 |
'humanize' => array(), |
|---|
| 45 |
'pluralize' => array(), |
|---|
| 46 |
'singularize' => array(), |
|---|
| 47 |
'underscorize' => array() |
|---|
| 48 |
); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
@var |
|---|
| 54 |
|
|---|
| 55 |
static private $camelize_rules = array(); |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
@var |
|---|
| 61 |
|
|---|
| 62 |
static private $humanize_rules = array(); |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
@var |
|---|
| 68 |
|
|---|
| 69 |
static private $join_array_callback = NULL; |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
@var |
|---|
| 75 |
|
|---|
| 76 |
static private $plural_to_singular_rules = array( |
|---|
| 77 |
'([ml])ice' => '\1ouse', |
|---|
| 78 |
'(media|info(rmation)?|news)$' => '\1', |
|---|
| 79 |
'(q)uizzes$' => '\1uiz', |
|---|
| 80 |
'(c)hildren$' => '\1hild', |
|---|
| 81 |
'(p)eople$' => '\1erson', |
|---|
| 82 |
'(m)en$' => '\1an', |
|---|
| 83 |
'((?!sh).)oes$' => '\1o', |
|---|
| 84 |
'((?<!o)[ieu]s|[ieuo]x)es$' => '\1', |
|---|
| 85 |
'([cs]h)es$' => '\1', |
|---|
| 86 |
'(ss)es$' => '\1', |
|---|
| 87 |
'([aeo]l)ves$' => '\1f', |
|---|
| 88 |
'([^d]ea)ves$' => '\1f', |
|---|
| 89 |
'(ar)ves$' => '\1f', |
|---|
| 90 |
'([nlw]i)ves$' => '\1fe', |
|---|
| 91 |
'([aeiou]y)s$' => '\1', |
|---|
| 92 |
'([^aeiou])ies$' => '\1y', |
|---|
| 93 |
'(la)ses$' => '\1s', |
|---|
| 94 |
'(.)s$' => '\1' |
|---|
| 95 |
); |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
@var |
|---|
| 101 |
|
|---|
| 102 |
static private $singular_to_plural_rules = array( |
|---|
| 103 |
'([ml])ouse$' => '\1ice', |
|---|
| 104 |
'(media|info(rmation)?|news)$' => '\1', |
|---|
| 105 |
'(phot|log)o$' => '\1os', |
|---|
| 106 |
'^(q)uiz$' => '\1uizzes', |
|---|
| 107 |
'(c)hild$' => '\1hildren', |
|---|
| 108 |
'(p)erson$' => '\1eople', |
|---|
| 109 |
'(m)an$' => '\1en', |
|---|
| 110 |
'([ieu]s|[ieuo]x)$' => '\1es', |
|---|
| 111 |
'([cs]h)$' => '\1es', |
|---|
| 112 |
'(ss)$' => '\1es', |
|---|
| 113 |
'([aeo]l)f$' => '\1ves', |
|---|
| 114 |
'([^d]ea)f$' => '\1ves', |
|---|
| 115 |
'(ar)f$' => '\1ves', |
|---|
| 116 |
'([nlw]i)fe$' => '\1ves', |
|---|
| 117 |
'([aeiou]y)$' => '\1s', |
|---|
| 118 |
'([^aeiou])y$' => '\1ies', |
|---|
| 119 |
'([^o])o$' => '\1oes', |
|---|
| 120 |
's$' => 'ses', |
|---|
| 121 |
'(.)$' => '\1s' |
|---|
| 122 |
); |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
@var |
|---|
| 128 |
|
|---|
| 129 |
static private $underscorize_rules = array(); |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
@param |
|---|
| 136 |
@param |
|---|
| 137 |
@return |
|---|
| 138 |
|
|---|
| 139 |
static public function addHumanizeRule($non_humanized_string, $humanized_string) |
|---|
| 140 |
{ |
|---|
| 141 |
self::$humanize_rules[$non_humanized_string] = $humanized_string; |
|---|
| 142 |
|
|---|
| 143 |
self::$cache['humanize'] = array(); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
@param |
|---|
| 151 |
@param |
|---|
| 152 |
@return |
|---|
| 153 |
|
|---|
| 154 |
static public function addCamelUnderscoreRule($camel_case, $underscore_notation) |
|---|
| 155 |
{ |
|---|
| 156 |
$camel_case = strtolower($camel_case[0]) . substr($camel_case, 1); |
|---|
| 157 |
self::$underscorize_rules[$camel_case] = $underscore_notation; |
|---|
| 158 |
self::$camelize_rules[$underscore_notation] = $camel_case; |
|---|
| 159 |
|
|---|
| 160 |
self::$cache['camelize'] = array(0 => array(), 1 => array()); |
|---|
| 161 |
self::$cache['underscorize'] = array(); |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
@param |
|---|
| 169 |
@param |
|---|
| 170 |
@return |
|---|
| 171 |
|
|---|
| 172 |
static public function addSingularPluralRule($singular, $plural) |
|---|
| 173 |
{ |
|---|
| 174 |
self::$singular_to_plural_rules = array_merge( |
|---|
| 175 |
array( |
|---|
| 176 |
'^(' . preg_quote($singular[0], '#') . ')' . preg_quote(substr($singular, 1), '#') . '$' => |
|---|
| 177 |
'\1' . strtr(substr($plural, 1), array('\\' => '\\\\', '$' => '\\$')) |
|---|
| 178 |
), |
|---|
| 179 |
self::$singular_to_plural_rules |
|---|
| 180 |
); |
|---|
| 181 |
self::$plural_to_singular_rules = array_merge( |
|---|
| 182 |
array( |
|---|
| 183 |
'^(' . preg_quote($plural[0], '#') . ')' . preg_quote(substr($plural, 1), '#') . '$' => |
|---|
| 184 |
'\1' . strtr(substr($singular, 1), array('\\' => '\\\\', '$' => '\\$')) |
|---|
| 185 |
), |
|---|
| 186 |
self::$plural_to_singular_rules |
|---|
| 187 |
); |
|---|
| 188 |
|
|---|
| 189 |
self::$cache['pluralize'] = array(); |
|---|
| 190 |
self::$cache['singularize'] = array(); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
@param |
|---|
| 198 |
@param |
|---|
| 199 |
@return |
|---|
| 200 |
|
|---|
| 201 |
static public function camelize($string, $upper) |
|---|
| 202 |
{ |
|---|
| 203 |
$upper = (int) $upper; |
|---|
| 204 |
if (isset(self::$cache['camelize'][$upper][$string])) { |
|---|
| 205 |
return self::$cache['camelize'][$upper][$string]; |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
$original = $string; |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
if (isset(self::$camelize_rules[$string])) { |
|---|
| 212 |
$string = self::$camelize_rules[$string]; |
|---|
| 213 |
if ($upper) { |
|---|
| 214 |
$string = strtoupper($camel[0]) . substr($camel, 1); |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
} elseif (strpos($string, ' ') !== FALSE) { |
|---|
| 219 |
$string = strtolower(preg_replace('#\s+#', '_', $string)); |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
} elseif (strpos($string, '_') === FALSE) { |
|---|
| 223 |
if ($upper) { |
|---|
| 224 |
$string = strtoupper($string[0]) . substr($string, 1); |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
} else { |
|---|
| 229 |
$string = strtolower($string); |
|---|
| 230 |
if ($upper) { |
|---|
| 231 |
$string = strtoupper($string[0]) . substr($string, 1); |
|---|
| 232 |
} |
|---|
| 233 |
$string = preg_replace('/(_([a-z0-9]))/e', 'strtoupper("\2")', $string); |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
self::$cache['camelize'][$upper][$original] = $string; |
|---|
| 237 |
return $string; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
@param |
|---|
| 245 |
@param |
|---|
| 246 |
@param |
|---|
| 247 |
@return |
|---|
| 248 |
|
|---|
| 249 |
static protected function compose($message) |
|---|
| 250 |
{ |
|---|
| 251 |
$args = array_slice(func_get_args(), 1); |
|---|
| 252 |
|
|---|
| 253 |
if (class_exists('fText', FALSE)) { |
|---|
| 254 |
return call_user_func_array( |
|---|
| 255 |
array('fText', 'compose'), |
|---|
| 256 |
array($message, $args) |
|---|
| 257 |
); |
|---|
| 258 |
} else { |
|---|
| 259 |
return vsprintf($message, $args); |
|---|
| 260 |
} |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
@param |
|---|
| 268 |
@return |
|---|
| 269 |
|
|---|
| 270 |
static public function humanize($string) |
|---|
| 271 |
{ |
|---|
| 272 |
if (isset(self::$cache['humanize'][$string])) { |
|---|
| 273 |
return self::$cache['humanize'][$string]; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
$original = $string; |
|---|
| 277 |
|
|---|
| 278 |
if (isset(self::$humanize_rules[$string])) { |
|---|
| 279 |
$string = self::$humanize_rules[$string]; |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
} elseif (strpos($string, ' ') === FALSE) { |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
if (strpos($string, '_') === FALSE) { |
|---|
| 286 |
$string = self::underscorize($string); |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
$string = preg_replace( |
|---|
| 290 |
'/(\b(api|css|gif|html|id|jpg|js|mp3|pdf|php|png|sql|swf|url|xhtml|xml)\b|\b\w)/e', |
|---|
| 291 |
'strtoupper("\1")', |
|---|
| 292 |
str_replace('_', ' ', $string) |
|---|
| 293 |
); |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
self::$cache['humanize'][$original] = $string; |
|---|
| 297 |
|
|---|
| 298 |
return $string; |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
@param |
|---|
| 306 |
@param |
|---|
| 307 |
@param |
|---|
| 308 |
@param |
|---|
| 309 |
@return |
|---|
| 310 |
|
|---|
| 311 |
static public function inflectOnQuantity($quantity, $singular_form, $plural_form=NULL, $use_words_for_single_digits=FALSE) |
|---|
| 312 |
{ |
|---|
| 313 |
if ($plural_form === NULL) { |
|---|
| 314 |
$plural_form = self::pluralize($singular_form); |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
if (is_array($quantity)) { |
|---|
| 318 |
$quantity = sizeof($quantity); |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
if ($quantity == 1) { |
|---|
| 322 |
return $singular_form; |
|---|
| 323 |
|
|---|
| 324 |
} else { |
|---|
| 325 |
$output = $plural_form; |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
if (strpos($output, '%d') !== FALSE) { |
|---|
| 329 |
|
|---|
| 330 |
if ($use_words_for_single_digits && $quantity < 10) { |
|---|
| 331 |
static $replacements = array(); |
|---|
| 332 |
if (!$replacements) { |
|---|
| 333 |
$replacements = array( |
|---|
| 334 |
0 => self::compose('zero'), |
|---|
| 335 |
1 => self::compose('one'), |
|---|
| 336 |
2 => self::compose('two'), |
|---|
| 337 |
3 => self::compose('three'), |
|---|
| 338 |
4 => self::compose('four'), |
|---|
| 339 |
5 => self::compose('five'), |
|---|
| 340 |
6 => self::compose('six'), |
|---|
| 341 |
7 => self::compose('seven'), |
|---|
| 342 |
8 => self::compose('eight'), |
|---|
| 343 |
9 => self::compose('nine') |
|---|
| 344 |
); |
|---|
| 345 |
} |
|---|
| 346 |
$quantity = $replacements[$quantity]; |
|---|
| 347 |
} |
|---|
| 348 |
|
|---|
| 349 |
$output = str_replace('%d', $quantity, $output); |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
return $output; |
|---|
| 353 |
} |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
@param |
|---|
| 361 |
@param |
|---|
| 362 |
@return |
|---|
| 363 |
|
|---|
| 364 |
static public function joinArray($strings, $type) |
|---|
| 365 |
{ |
|---|
| 366 |
$valid_types = array('and', 'or'); |
|---|
| 367 |
if (!in_array($type, $valid_types)) { |
|---|
| 368 |
throw new fProgrammerException( |
|---|
| 369 |
'The type specified, %1$s, is invalid. Must be one of: %2$s.', |
|---|
| 370 |
$type, |
|---|
| 371 |
join(', ', $valid_types) |
|---|
| 372 |
); |
|---|
| 373 |
} |
|---|
| 374 |
|
|---|
| 375 |
if (self::$join_array_callback) { |
|---|
| 376 |
return call_user_func(self::$join_array_callback, $strings, $type); |
|---|
| 377 |
} |
|---|
| 378 |
|
|---|
| 379 |
settype($strings, 'array'); |
|---|
| 380 |
$strings = array_values($strings); |
|---|
| 381 |
|
|---|
| 382 |
switch (sizeof($strings)) { |
|---|
| 383 |
case 0: |
|---|
| 384 |
return ''; |
|---|
| 385 |
break; |
|---|
| 386 |
|
|---|
| 387 |
case 1: |
|---|
| 388 |
return $strings[0]; |
|---|
| 389 |
break; |
|---|
| 390 |
|
|---|
| 391 |
case 2: |
|---|
| 392 |
return $strings[0] . ' ' . $type . ' ' . $strings[1]; |
|---|
| 393 |
break; |
|---|
| 394 |
|
|---|
| 395 |
default: |
|---|
| 396 |
$last_string = array_pop($strings); |
|---|
| 397 |
return join(', ', $strings) . ' ' . $type . ' ' . $last_string; |
|---|
| 398 |
break; |
|---|
| 399 |
} |
|---|
| 400 |
} |
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
@param |
|---|
| 407 |
@return |
|---|
| 408 |
|
|---|
| 409 |
static public function pluralize($singular_noun) |
|---|
| 410 |
{ |
|---|
| 411 |
if (isset(self::$cache['pluralize'][$singular_noun])) { |
|---|
| 412 |
return self::$cache['pluralize'][$singular_noun]; |
|---|
| 413 |
} |
|---|
| 414 |
|
|---|
| 415 |
$original = $singular_noun; |
|---|
| 416 |
$plural_noun = NULL; |
|---|
| 417 |
|
|---|
| 418 |
list ($beginning, $singular_noun) = self::splitLastWord($singular_noun); |
|---|
| 419 |
foreach (self::$singular_to_plural_rules as $from => $to) { |
|---|
| 420 |
if (preg_match('#' . $from . '#iD', $singular_noun)) { |
|---|
| 421 |
$plural_noun = $beginning . preg_replace('#' . $from . '#iD', $to, $singular_noun); |
|---|
| 422 |
break; |
|---|
| 423 |
} |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
if (!$plural_noun) { |
|---|
| 427 |
throw new fProgrammerException('The noun specified could not be pluralized'); |
|---|
| 428 |
} |
|---|
| 429 |
|
|---|
| 430 |
self::$cache['pluralize'][$original] = $plural_noun; |
|---|
| 431 |
|
|---|
| 432 |
return $plural_noun; |
|---|
| 433 |
} |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
@param |
|---|
| 443 |
@return |
|---|
| 444 |
|
|---|
| 445 |
static public function registerJoinArrayCallback($callback) |
|---|
| 446 |
{ |
|---|
| 447 |
if (is_string($callback) && strpos($callback, '::') !== FALSE) { |
|---|
| 448 |
$callback = explode('::', $callback); |
|---|
| 449 |
} |
|---|
| 450 |
self::$join_array_callback = $callback; |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
@internal |
|---|
| 458 |
|
|---|
| 459 |
@return |
|---|
| 460 |
|
|---|
| 461 |
static public function reset() |
|---|
| 462 |
{ |
|---|
| 463 |
self::$cache = array( |
|---|
| 464 |
'camelize' => array(0 => array(), 1 => array()), |
|---|
| 465 |
'humanize' => array(), |
|---|
| 466 |
'pluralize' => array(), |
|---|
| 467 |
'singularize' => array(), |
|---|
| 468 |
'underscorize' => array() |
|---|
| 469 |
); |
|---|
| 470 |
self::$camelize_rules = array(); |
|---|
| 471 |
self::$humanize_rules = array(); |
|---|
| 472 |
self::$join_array_callback = NULL; |
|---|
| 473 |
self::$plural_to_singular_rules = array( |
|---|
| 474 |
'([ml])ice' => '\1ouse', |
|---|
| 475 |
'(media|info(rmation)?|news)$' => '\1', |
|---|
| 476 |
'(q)uizzes$' => '\1uiz', |
|---|
| 477 |
'(c)hildren$' => '\1hild', |
|---|
| 478 |
'(p)eople$' => '\1erson', |
|---|
| 479 |
'(m)en$' => '\1an', |
|---|
| 480 |
'((?!sh).)oes$' => '\1o', |
|---|
| 481 |
'((?<!o)[ieu]s|[ieuo]x)es$' => '\1', |
|---|
| 482 |
'([cs]h)es$' => '\1', |
|---|
| 483 |
'(ss)es$' => '\1', |
|---|
| 484 |
'([aeo]l)ves$' => '\1f', |
|---|
| 485 |
'([^d]ea)ves$' => '\1f', |
|---|
| 486 |
'(ar)ves$' => '\1f', |
|---|
| 487 |
'([nlw]i)ves$' => '\1fe', |
|---|
| 488 |
'([aeiou]y)s$' => '\1', |
|---|
| 489 |
'([^aeiou])ies$' => '\1y', |
|---|
| 490 |
'(la)ses$' => '\1s', |
|---|
| 491 |
'(.)s$' => '\1' |
|---|
| 492 |
); |
|---|
| 493 |
self::$singular_to_plural_rules = array( |
|---|
| 494 |
'([ml])ouse$' => '\1ice', |
|---|
| 495 |
'(media|info(rmation)?|news)$' => '\1', |
|---|
| 496 |
'(phot|log)o$' => '\1os', |
|---|
| 497 |
'^(q)uiz$' => '\1uizzes', |
|---|
| 498 |
'(c)hild$' => '\1hildren', |
|---|
| 499 |
'(p)erson$' => '\1eople', |
|---|
| 500 |
'(m)an$' => '\1en', |
|---|
| 501 |
'([ieu]s|[ieuo]x)$' => '\1es', |
|---|
| 502 |
'([cs]h)$' => '\1es', |
|---|
| 503 |
'(ss)$' => '\1es', |
|---|
| 504 |
'([aeo]l)f$' => '\1ves', |
|---|
| 505 |
'([^d]ea)f$' => '\1ves', |
|---|
| 506 |
'(ar)f$' => '\1ves', |
|---|
| 507 |
'([nlw]i)fe$' => '\1ves', |
|---|
| 508 |
'([aeiou]y)$' => '\1s', |
|---|
| 509 |
'([^aeiou])y$' => '\1ies', |
|---|
| 510 |
'([^o])o$' => '\1oes', |
|---|
| 511 |
's$' => 'ses', |
|---|
| 512 |
'(.)$' => '\1s' |
|---|
| 513 |
); |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
@param |
|---|
| 521 |
@return |
|---|
| 522 |
|
|---|
| 523 |
static public function singularize($plural_noun) |
|---|
| 524 |
{ |
|---|
| 525 |
if (isset(self::$cache['singularize'][$plural_noun])) { |
|---|
| 526 |
return self::$cache['singularize'][$plural_noun]; |
|---|
| 527 |
} |
|---|
| 528 |
|
|---|
| 529 |
$original = $plural_noun; |
|---|
| 530 |
$singular_noun = NULL; |
|---|
| 531 |
|
|---|
| 532 |
list ($beginning, $plural_noun) = self::splitLastWord($plural_noun); |
|---|
| 533 |
foreach (self::$plural_to_singular_rules as $from => $to) { |
|---|
| 534 |
if (preg_match('#' . $from . '#iD', $plural_noun)) { |
|---|
| 535 |
$singular_noun = $beginning . preg_replace('#' . $from . '#iD', $to, $plural_noun); |
|---|
| 536 |
break; |
|---|
| 537 |
} |
|---|
| 538 |
} |
|---|
| 539 |
|
|---|
| 540 |
if (!$singular_noun) { |
|---|
| 541 |
throw new fProgrammerException('The noun specified could not be singularized'); |
|---|
| 542 |
} |
|---|
| 543 |
|
|---|
| 544 |
self::$cache['singularize'][$original] = $singular_noun; |
|---|
| 545 |
|
|---|
| 546 |
return $singular_noun; |
|---|
| 547 |
} |
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
@param |
|---|
| 554 |
@return |
|---|
| 555 |
|
|---|
| 556 |
static private function splitLastWord($string) |
|---|
| 557 |
{ |
|---|
| 558 |
|
|---|
| 559 |
if (strpos($string, ' ') !== FALSE) { |
|---|
| 560 |
return array(substr($string, 0, strrpos($string, ' ')+1), substr($string, strrpos($string, ' ')+1)); |
|---|
| 561 |
} |
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
if ($string == self::underscorize($string)) { |
|---|
| 565 |
if (strpos($string, '_') === FALSE) { return array('', $string); } |
|---|
| 566 |
return array(substr($string, 0, strrpos($string, '_')+1), substr($string, strrpos($string, '_')+1)); |
|---|
| 567 |
} |
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
if (preg_match('#(.*)((?<=[a-zA-Z]|^)(?:[0-9]+|[A-Z][a-z]*)|(?<=[0-9A-Z]|^)(?:[A-Z][a-z]*))$#D', $string, $match)) { |
|---|
| 571 |
return array($match[1], $match[2]); |
|---|
| 572 |
} |
|---|
| 573 |
|
|---|
| 574 |
return array('', $string); |
|---|
| 575 |
} |
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
@param |
|---|
| 582 |
@return |
|---|
| 583 |
|
|---|
| 584 |
static public function underscorize($string) |
|---|
| 585 |
{ |
|---|
| 586 |
if (isset(self::$cache['underscorize'][$string])) { |
|---|
| 587 |
return self::$cache['underscorize'][$string]; |
|---|
| 588 |
} |
|---|
| 589 |
|
|---|
| 590 |
$original = $string; |
|---|
| 591 |
$string = strtolower($string[0]) . substr($string, 1); |
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
if (isset(self::$underscorize_rules[$string])) { |
|---|
| 595 |
$string = self::$underscorize_rules[$string]; |
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
} elseif (strpos($string, '_') !== FALSE) { |
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
} elseif (strpos($string, ' ') !== FALSE) { |
|---|
| 602 |
$string = strtolower(preg_replace('#\s+#', '_', $string)); |
|---|
| 603 |
|
|---|
| 604 |
} else { |
|---|
| 605 |
do { |
|---|
| 606 |
$old_string = $string; |
|---|
| 607 |
$string = preg_replace('/([a-zA-Z])([0-9])/', '\1_\2', $string); |
|---|
| 608 |
$string = preg_replace('/([a-z0-9A-Z])([A-Z])/', '\1_\2', $string); |
|---|
| 609 |
} while ($old_string != $string); |
|---|
| 610 |
|
|---|
| 611 |
$string = strtolower($string); |
|---|
| 612 |
} |
|---|
| 613 |
|
|---|
| 614 |
self::$cache['underscorize'][$original] = $string; |
|---|
| 615 |
|
|---|
| 616 |
return $string; |
|---|
| 617 |
} |
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 |
|
|---|
| 623 |
@return |
|---|
| 624 |
|
|---|
| 625 |
private function __construct() { } |
|---|
| 626 |
} |
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
will@flourishlib.com |
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
|
|---|
| 650 |
|
|---|