-
Notifications
You must be signed in to change notification settings - Fork 78
/
fGrammar.php
838 lines (722 loc) · 25.7 KB
/
fGrammar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
<?php
/**
* Provides english word inflection, notation conversion, grammar helpers and internationlization support
*
* @copyright Copyright (c) 2007-2011 Will Bond
* @author Will Bond [wb] <[email protected]>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fGrammar
*
* @version 1.0.0b15
* @changes 1.0.0b15 Added length checking to ensure blank strings are not being passed to various methods [wb, 2011-06-20]
* @changes 1.0.0b14 Fixed a bug in singularization that would affect words containing the substring `mice` or `lice` [wb, 2011-02-24]
* @changes 1.0.0b13 Fixed the pluralization of video [wb, 2010-08-10]
* @changes 1.0.0b12 Updated ::singularize() and ::pluralize() to be able to handle underscore_CamelCase [wb, 2010-08-06]
* @changes 1.0.0b11 Fixed custom camelCase to underscore_notation rules [wb, 2010-06-23]
* @changes 1.0.0b10 Removed `e` flag from preg_replace() calls [wb, 2010-06-08]
* @changes 1.0.0b9 Fixed a bug with ::camelize() and human-friendly strings [wb, 2010-06-08]
* @changes 1.0.0b8 Added the ::stem() method [wb, 2010-05-27]
* @changes 1.0.0b7 Added the `$return_error` parameter to ::pluralize() and ::singularize() [wb, 2010-03-30]
* @changes 1.0.0b6 Added missing ::compose() method [wb, 2010-03-03]
* @changes 1.0.0b5 Fixed ::reset() to properly reset the singularization and pluralization rules [wb, 2009-10-28]
* @changes 1.0.0b4 Added caching for various methods - provided significant performance boost to ORM [wb, 2009-06-15]
* @changes 1.0.0b3 Changed replacement values in preg_replace() calls to be properly escaped [wb, 2009-06-11]
* @changes 1.0.0b2 Fixed a bug where some words would lose capitalization with ::pluralize() and ::singularize() [wb, 2009-01-25]
* @changes 1.0.0b The initial implementation [wb, 2007-09-25]
*/
class fGrammar
{
// The following constants allow for nice looking callbacks to static methods
const addCamelUnderscoreRule = 'fGrammar::addCamelUnderscoreRule';
const addHumanizeRule = 'fGrammar::addHumanizeRule';
const addSingularPluralRule = 'fGrammar::addSingularPluralRule';
const camelize = 'fGrammar::camelize';
const humanize = 'fGrammar::humanize';
const inflectOnQuantity = 'fGrammar::inflectOnQuantity';
const joinArray = 'fGrammar::joinArray';
const pluralize = 'fGrammar::pluralize';
const registerJoinArrayCallback = 'fGrammar::registerJoinArrayCallback';
const reset = 'fGrammar::reset';
const singularize = 'fGrammar::singularize';
const stem = 'fGrammar::stem';
const underscorize = 'fGrammar::underscorize';
/**
* Cache for plural <-> singular and underscore <-> camelcase
*
* @var array
*/
static private $cache = array(
'camelize' => array(0 => array(), 1 => array()),
'humanize' => array(),
'pluralize' => array(),
'singularize' => array(),
'underscorize' => array()
);
/**
* Custom rules for camelizing a string
*
* @var array
*/
static private $camelize_rules = array();
/**
* Custom rules for humanizing a string
*
* @var array
*/
static private $humanize_rules = array();
/**
* The callback to replace ::joinArray()
*
* @var callback
*/
static private $join_array_callback = NULL;
/**
* Rules for plural to singular inflection of nouns
*
* @var array
*/
static private $plural_to_singular_rules = array(
'([ml])ice$' => '\1ouse',
'(media|info(rmation)?|news)$' => '\1',
'(q)uizzes$' => '\1uiz',
'(c)hildren$' => '\1hild',
'(p)eople$' => '\1erson',
'(m)en$' => '\1an',
'((?!sh).)oes$' => '\1o',
'((?<!o)[ieu]s|[ieuo]x)es$' => '\1',
'([cs]h)es$' => '\1',
'(ss)es$' => '\1',
'([aeo]l)ves$' => '\1f',
'([^d]ea)ves$' => '\1f',
'(ar)ves$' => '\1f',
'([nlw]i)ves$' => '\1fe',
'([aeiou]y)s$' => '\1',
'([^aeiou])ies$' => '\1y',
'(la)ses$' => '\1s',
'(.)s$' => '\1'
);
/**
* Rules for singular to plural inflection of nouns
*
* @var array
*/
static private $singular_to_plural_rules = array(
'([ml])ouse$' => '\1ice',
'(media|info(rmation)?|news)$' => '\1',
'(phot|log|vide)o$' => '\1os',
'^(q)uiz$' => '\1uizzes',
'(c)hild$' => '\1hildren',
'(p)erson$' => '\1eople',
'(m)an$' => '\1en',
'([ieu]s|[ieuo]x)$' => '\1es',
'([cs]h)$' => '\1es',
'(ss)$' => '\1es',
'([aeo]l)f$' => '\1ves',
'([^d]ea)f$' => '\1ves',
'(ar)f$' => '\1ves',
'([nlw]i)fe$' => '\1ves',
'([aeiou]y)$' => '\1s',
'([^aeiou])y$' => '\1ies',
'([^o])o$' => '\1oes',
's$' => 'ses',
'(.)$' => '\1s'
);
/**
* Custom rules for underscorizing a string
*
* @var array
*/
static private $underscorize_rules = array();
/**
* Adds a custom mapping of a non-humanized string to a humanized string for ::humanize()
*
* @param string $non_humanized_string The non-humanized string
* @param string $humanized_string The humanized string
* @return void
*/
static public function addHumanizeRule($non_humanized_string, $humanized_string)
{
self::$humanize_rules[$non_humanized_string] = $humanized_string;
self::$cache['humanize'] = array();
}
/**
* Adds a custom `camelCase` to `underscore_notation` and `underscore_notation` to `camelCase` rule
*
* @param string $camel_case The lower `camelCase` version of the string
* @param string $underscore_notation The `underscore_notation` version of the string
* @return void
*/
static public function addCamelUnderscoreRule($camel_case, $underscore_notation)
{
$camel_case = strtolower($camel_case[0]) . substr($camel_case, 1);
self::$underscorize_rules[$camel_case] = $underscore_notation;
self::$camelize_rules[$underscore_notation] = $camel_case;
self::$cache['camelize'] = array(0 => array(), 1 => array());
self::$cache['underscorize'] = array();
}
/**
* Adds a custom singular to plural and plural to singular rule for ::pluralize() and ::singularize()
*
* @param string $singular The singular version of the noun
* @param string $plural The plural version of the noun
* @return void
*/
static public function addSingularPluralRule($singular, $plural)
{
self::$singular_to_plural_rules = array_merge(
array(
'^(' . preg_quote($singular[0], '#') . ')' . preg_quote(substr($singular, 1), '#') . '$' =>
'\1' . strtr(substr($plural, 1), array('\\' => '\\\\', '$' => '\\$'))
),
self::$singular_to_plural_rules
);
self::$plural_to_singular_rules = array_merge(
array(
'^(' . preg_quote($plural[0], '#') . ')' . preg_quote(substr($plural, 1), '#') . '$' =>
'\1' . strtr(substr($singular, 1), array('\\' => '\\\\', '$' => '\\$'))
),
self::$plural_to_singular_rules
);
self::$cache['pluralize'] = array();
self::$cache['singularize'] = array();
}
/**
* Converts an `underscore_notation`, human-friendly or `camelCase` string to `camelCase`
*
* @param string $string The string to convert
* @param boolean $upper If the camel case should be `UpperCamelCase`
* @return string The converted string
*/
static public function camelize($string, $upper)
{
if (!strlen($string)) {
throw new fProgrammerException(
"An empty string was passed to %s",
__CLASS__ . '::camelize()'
);
}
$upper = (int)(bool) $upper;
if (isset(self::$cache['camelize'][$upper][$string])) {
return self::$cache['camelize'][$upper][$string];
}
$original = $string;
// Handle custom rules
if (isset(self::$camelize_rules[$string])) {
$string = self::$camelize_rules[$string];
if ($upper) {
$string = ucfirst($string);
}
} else {
// Make a humanized string like underscore notation
if (strpos($string, ' ') !== FALSE) {
$string = strtolower(preg_replace('#\s+#', '_', $string));
}
// Check to make sure this is not already camel case
if (strpos($string, '_') === FALSE) {
if ($upper) {
$string = ucfirst($string);
}
// Handle underscore notation
} else {
$string[0] = strtolower($string[0]);
if ($upper) {
$string = ucfirst($string);
}
$string = preg_replace_callback('#_([a-z0-9])#i', array('self', 'camelizeCallback'), $string);
}
}
self::$cache['camelize'][$upper][$original] = $string;
return $string;
}
/**
* A callback used by ::camelize() to handle converting underscore to camelCase
*
* @param array $match The regular expression match
* @return string The value to replace the string with
*/
static private function camelizeCallback($match)
{
return strtoupper($match[1]);
}
/**
* Composes text using fText if loaded
*
* @param string $message The message to compose
* @param mixed $component A string or number to insert into the message
* @param mixed ...
* @return string The composed and possible translated message
*/
static protected function compose($message)
{
$args = array_slice(func_get_args(), 1);
if (class_exists('fText', FALSE)) {
return call_user_func_array(
array('fText', 'compose'),
array($message, $args)
);
} else {
return vsprintf($message, $args);
}
}
/**
* Makes an `underscore_notation`, `camelCase`, or human-friendly string into a human-friendly string
*
* @param string $string The string to humanize
* @return string The converted string
*/
static public function humanize($string)
{
if (!strlen($string)) {
throw new fProgrammerException(
"An empty string was passed to %s",
__CLASS__ . '::humanize()'
);
}
if (isset(self::$cache['humanize'][$string])) {
return self::$cache['humanize'][$string];
}
$original = $string;
if (isset(self::$humanize_rules[$string])) {
$string = self::$humanize_rules[$string];
// If there is no space, it isn't already humanized
} elseif (strpos($string, ' ') === FALSE) {
// If we don't have an underscore we probably have camelCase
if (strpos($string, '_') === FALSE) {
$string = self::underscorize($string);
}
$string = preg_replace_callback(
'/(\b(api|css|gif|html|id|jpg|js|mp3|pdf|php|png|sql|swf|url|xhtml|xml)\b|\b\w)/',
array('self', 'camelizeCallback'),
str_replace('_', ' ', $string)
);
}
self::$cache['humanize'][$original] = $string;
return $string;
}
/**
* Returns the singular or plural form of the word or based on the quantity specified
*
* @param mixed $quantity The quantity (integer) or an array of objects to count
* @param string $singular_form The string to be returned for when `$quantity = 1`
* @param string $plural_form The string to be returned for when `$quantity != 1`, use `%d` to place the quantity in the string
* @param boolean $use_words_for_single_digits If the numbers 0 to 9 should be written out as words
* @return string
*/
static public function inflectOnQuantity($quantity, $singular_form, $plural_form=NULL, $use_words_for_single_digits=FALSE)
{
if ($plural_form === NULL) {
$plural_form = self::pluralize($singular_form);
}
if (is_array($quantity)) {
$quantity = sizeof($quantity);
}
if ($quantity == 1) {
return $singular_form;
} else {
$output = $plural_form;
// Handle placement of the quantity into the output
if (strpos($output, '%d') !== FALSE) {
if ($use_words_for_single_digits && $quantity < 10) {
static $replacements = array();
if (!$replacements) {
$replacements = array(
0 => self::compose('zero'),
1 => self::compose('one'),
2 => self::compose('two'),
3 => self::compose('three'),
4 => self::compose('four'),
5 => self::compose('five'),
6 => self::compose('six'),
7 => self::compose('seven'),
8 => self::compose('eight'),
9 => self::compose('nine')
);
}
$quantity = $replacements[$quantity];
}
$output = str_replace('%d', $quantity, $output);
}
return $output;
}
}
/**
* Returns the passed terms joined together using rule 2 from Strunk & White's 'The Elements of Style'
*
* @param array $strings An array of strings to be joined together
* @param string $type The type of join to perform, `'and'` or `'or'`
* @return string The terms joined together
*/
static public function joinArray($strings, $type)
{
$valid_types = array('and', 'or');
if (!in_array($type, $valid_types)) {
throw new fProgrammerException(
'The type specified, %1$s, is invalid. Must be one of: %2$s.',
$type,
join(', ', $valid_types)
);
}
if (self::$join_array_callback) {
return call_user_func(self::$join_array_callback, $strings, $type);
}
settype($strings, 'array');
$strings = array_values($strings);
switch (sizeof($strings)) {
case 0:
return '';
break;
case 1:
return $strings[0];
break;
case 2:
return $strings[0] . ' ' . $type . ' ' . $strings[1];
break;
default:
$last_string = array_pop($strings);
return join(', ', $strings) . ' ' . $type . ' ' . $last_string;
break;
}
}
/**
* Returns the plural version of a singular noun
*
* @param string $singular_noun The singular noun to pluralize
* @param boolean $return_error If this is `TRUE` and the noun can't be pluralized, `FALSE` will be returned instead
* @return string The pluralized noun
*/
static public function pluralize($singular_noun, $return_error=FALSE)
{
if (!strlen($singular_noun)) {
throw new fProgrammerException(
"An empty string was passed to %s",
__CLASS__ . '::pluralize()'
);
}
if (isset(self::$cache['pluralize'][$singular_noun])) {
return self::$cache['pluralize'][$singular_noun];
}
$original = $singular_noun;
$plural_noun = NULL;
list ($beginning, $singular_noun) = self::splitLastWord($singular_noun);
foreach (self::$singular_to_plural_rules as $from => $to) {
if (preg_match('#' . $from . '#iD', $singular_noun)) {
$plural_noun = $beginning . preg_replace('#' . $from . '#iD', $to, $singular_noun);
break;
}
}
if (!$plural_noun) {
if ($return_error) {
self::$cache['pluralize'][$singular_noun] = FALSE;
return FALSE;
}
throw new fProgrammerException('The noun specified could not be pluralized');
}
self::$cache['pluralize'][$original] = $plural_noun;
return $plural_noun;
}
/**
* Allows replacing the ::joinArray() function with a user defined function
*
* This would be most useful for changing ::joinArray() to work with
* languages other than English.
*
* @param callback $callback The function to replace ::joinArray() with - should accept the same parameters and return the same type
* @return void
*/
static public function registerJoinArrayCallback($callback)
{
if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}
self::$join_array_callback = $callback;
}
/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$cache = array(
'camelize' => array(0 => array(), 1 => array()),
'humanize' => array(),
'pluralize' => array(),
'singularize' => array(),
'underscorize' => array()
);
self::$camelize_rules = array();
self::$humanize_rules = array();
self::$join_array_callback = NULL;
self::$plural_to_singular_rules = array(
'([ml])ice$' => '\1ouse',
'(media|info(rmation)?|news)$' => '\1',
'(q)uizzes$' => '\1uiz',
'(c)hildren$' => '\1hild',
'(p)eople$' => '\1erson',
'(m)en$' => '\1an',
'((?!sh).)oes$' => '\1o',
'((?<!o)[ieu]s|[ieuo]x)es$' => '\1',
'([cs]h)es$' => '\1',
'(ss)es$' => '\1',
'([aeo]l)ves$' => '\1f',
'([^d]ea)ves$' => '\1f',
'(ar)ves$' => '\1f',
'([nlw]i)ves$' => '\1fe',
'([aeiou]y)s$' => '\1',
'([^aeiou])ies$' => '\1y',
'(la)ses$' => '\1s',
'(.)s$' => '\1'
);
self::$singular_to_plural_rules = array(
'([ml])ouse$' => '\1ice',
'(media|info(rmation)?|news)$' => '\1',
'(phot|log|vide)o$' => '\1os',
'^(q)uiz$' => '\1uizzes',
'(c)hild$' => '\1hildren',
'(p)erson$' => '\1eople',
'(m)an$' => '\1en',
'([ieu]s|[ieuo]x)$' => '\1es',
'([cs]h)$' => '\1es',
'(ss)$' => '\1es',
'([aeo]l)f$' => '\1ves',
'([^d]ea)f$' => '\1ves',
'(ar)f$' => '\1ves',
'([nlw]i)fe$' => '\1ves',
'([aeiou]y)$' => '\1s',
'([^aeiou])y$' => '\1ies',
'([^o])o$' => '\1oes',
's$' => 'ses',
'(.)$' => '\1s'
);
}
/**
* Returns the singular version of a plural noun
*
* @param string $plural_noun The plural noun to singularize
* @param boolean $return_error If this is `TRUE` and the noun can't be pluralized, `FALSE` will be returned instead
* @return string The singularized noun
*/
static public function singularize($plural_noun, $return_error=FALSE)
{
if (!strlen($plural_noun)) {
throw new fProgrammerException(
"An empty string was passed to %s",
__CLASS__ . '::singularize()'
);
}
if (isset(self::$cache['singularize'][$plural_noun])) {
return self::$cache['singularize'][$plural_noun];
}
$original = $plural_noun;
$singular_noun = NULL;
list ($beginning, $plural_noun) = self::splitLastWord($plural_noun);
foreach (self::$plural_to_singular_rules as $from => $to) {
if (preg_match('#' . $from . '#iD', $plural_noun)) {
$singular_noun = $beginning . preg_replace('#' . $from . '#iD', $to, $plural_noun);
break;
}
}
if (!$singular_noun) {
if ($return_error) {
self::$cache['singularize'][$plural_noun] = FALSE;
return FALSE;
}
throw new fProgrammerException('The noun specified could not be singularized');
}
self::$cache['singularize'][$original] = $singular_noun;
return $singular_noun;
}
/**
* Splits the last word off of a `camelCase` or `underscore_notation` string
*
* @param string $string The string to split the word from
* @return array The first element is the beginning part of the string, the second element is the last word
*/
static private function splitLastWord($string)
{
// Handle strings with spaces in them
if (strpos($string, ' ') !== FALSE) {
return array(substr($string, 0, strrpos($string, ' ')+1), substr($string, strrpos($string, ' ')+1));
}
// Handle underscore notation
if ($string == self::underscorize($string)) {
if (strpos($string, '_') === FALSE) { return array('', $string); }
return array(substr($string, 0, strrpos($string, '_')+1), substr($string, strrpos($string, '_')+1));
}
// Handle camel case
if (preg_match('#(.*)((?<=[a-zA-Z_]|^)(?:[0-9]+|[A-Z][a-z]*)|(?<=[0-9A-Z_]|^)(?:[A-Z][a-z]*))$#D', $string, $match)) {
return array($match[1], $match[2]);
}
return array('', $string);
}
/**
* Uses the Porter Stemming algorithm to create the stem of a word, which is useful for searching
*
* See http://tartarus.org/~martin/PorterStemmer/ for details about the
* algorithm.
*
* @param string $word The word to get the stem of
* @return string The stem of the word
*/
static public function stem($word)
{
$s_v = '^([^aeiou][^aeiouy]*)?[aeiouy]';
$mgr0 = $s_v . '[aeiou]*[^aeiou][^aeiouy]*';
$s_v_regex = '#' . $s_v . '#';
$mgr0_regex = '#' . $mgr0 . '#';
$meq1_regex = '#' . $mgr0 . '([aeiouy][aeiou]*)?$#';
$mgr1_regex = '#' . $mgr0 . '[aeiouy][aeiou]*[^aeiou][^aeiouy]*#';
$word = fUTF8::ascii($word);
$word = strtolower($word);
if (strlen($word) < 3) {
return $word;
}
if ($word[0] == 'y') {
$word = 'Y' . substr($word, 1);
}
// Step 1a
$word = preg_replace('#^(.+?)(?:(ss|i)es|([^s])s)$#', '\1\2\3', $word);
// Step 1b
if (preg_match('#^(.+?)eed$#', $word, $match)) {
if (preg_match($mgr0_regex, $match[1])) {
$word = substr($word, 0, -1);
}
} elseif (preg_match('#^(.+?)(ed|ing)$#', $word, $match)) {
if (preg_match($s_v_regex, $match[1])) {
$word = $match[1];
if (preg_match('#(at|bl|iz)$#', $word)) {
$word .= 'e';
} elseif (preg_match('#([^aeiouylsz])\1$#', $word)) {
$word = substr($word, 0, -1);
} elseif (preg_match('#^[^aeiou][^aeiouy]*[aeiouy][^aeiouwxy]$#', $word)) {
$word .= 'e';
}
}
}
// Step 1c
if (substr($word, -1) == 'y') {
$stem = substr($word, 0, -1);
if (preg_match($s_v_regex, $stem)) {
$word = $stem . 'i';
}
}
// Step 2
if (preg_match('#^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$#', $word, $match)) {
if (preg_match($mgr0_regex, $match[1])) {
$word = $match[1] . strtr(
$match[2],
array(
'ational' => 'ate', 'tional' => 'tion', 'enci' => 'ence',
'anci' => 'ance', 'izer' => 'ize', 'bli' => 'ble',
'alli' => 'al', 'entli' => 'ent', 'eli' => 'e',
'ousli' => 'ous', 'ization' => 'ize', 'ation' => 'ate',
'ator' => 'ate', 'alism' => 'al', 'iveness' => 'ive',
'fulness' => 'ful', 'ousness' => 'ous', 'aliti' => 'al',
'iviti' => 'ive', 'biliti' => 'ble', 'logi' => 'log'
)
);
}
}
// Step 3
if (preg_match('#^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$#', $word, $match)) {
if (preg_match($mgr0_regex, $match[1])) {
$word = $match[1] . strtr(
$match[2],
array(
'icate' => 'ic', 'ative' => '', 'alize' => 'al', 'iciti' => 'ic',
'ical' => 'ic', 'ful' => '', 'ness' => ''
)
);
}
}
// Step 4
if (preg_match('#^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize|(?<=[st])ion)$#', $word, $match) && preg_match($mgr1_regex, $match[1])) {
$word = $match[1];
}
// Step 5
if (substr($word, -1) == 'e') {
$stem = substr($word, 0, -1);
if (preg_match($mgr1_regex, $stem)) {
$word = $stem;
} elseif (preg_match($meq1_regex, $stem) && !preg_match('#^[^aeiou][^aeiouy]*[aeiouy][^aeiouwxy]$#', $stem)) {
$word = $stem;
}
}
if (preg_match('#ll$#', $word) && preg_match($mgr1_regex, $word)) {
$word = substr($word, 0, -1);
}
if ($word[0] == 'Y') {
$word = 'y' . substr($word, 1);
}
return $word;
}
/**
* Converts a `camelCase`, human-friendly or `underscore_notation` string to `underscore_notation`
*
* @param string $string The string to convert
* @return string The converted string
*/
static public function underscorize($string)
{
if (!strlen($string)) {
throw new fProgrammerException(
"An empty string was passed to %s",
__CLASS__ . '::underscorize()'
);
}
if (isset(self::$cache['underscorize'][$string])) {
return self::$cache['underscorize'][$string];
}
$original = $string;
$string = strtolower($string[0]) . substr($string, 1);
// Handle custom rules
if (isset(self::$underscorize_rules[$string])) {
$string = self::$underscorize_rules[$string];
// If the string is already underscore notation then leave it
} elseif (strpos($string, '_') !== FALSE && strtolower($string) == $string) {
// Allow humanized string to be passed in
} elseif (strpos($string, ' ') !== FALSE) {
$string = strtolower(preg_replace('#\s+#', '_', $string));
} else {
do {
$old_string = $string;
$string = preg_replace('/([a-zA-Z])([0-9])/', '\1_\2', $string);
$string = preg_replace('/([a-z0-9A-Z])([A-Z])/', '\1_\2', $string);
} while ($old_string != $string);
$string = strtolower($string);
}
self::$cache['underscorize'][$original] = $string;
return $string;
}
/**
* Forces use as a static class
*
* @return fGrammar
*/
private function __construct() { }
}
/**
* Copyright (c) 2007-2011 Will Bond <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/