|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bonfim\ActiveRecord; |
|
4
|
|
|
|
|
5
|
|
|
// original source: http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/ |
|
6
|
|
|
|
|
7
|
|
|
class Inflect |
|
8
|
|
|
{ |
|
9
|
|
|
private static $plural = array( |
|
10
|
|
|
'/(quiz)$/i' => "$1zes", |
|
11
|
|
|
'/^(ox)$/i' => "$1en", |
|
12
|
|
|
'/([m|l])ouse$/i' => "$1ice", |
|
13
|
|
|
'/(matr|vert|ind)ix|ex$/i' => "$1ices", |
|
14
|
|
|
'/(x|ch|ss|sh)$/i' => "$1es", |
|
15
|
|
|
'/([^aeiouy]|qu)y$/i' => "$1ies", |
|
16
|
|
|
'/(hive)$/i' => "$1s", |
|
17
|
|
|
'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves", |
|
18
|
|
|
'/(shea|lea|loa|thie)f$/i' => "$1ves", |
|
19
|
|
|
'/sis$/i' => "ses", |
|
20
|
|
|
'/([ti])um$/i' => "$1a", |
|
21
|
|
|
'/(tomat|potat|ech|her|vet)o$/i'=> "$1oes", |
|
22
|
|
|
'/(bu)s$/i' => "$1ses", |
|
23
|
|
|
'/(alias)$/i' => "$1es", |
|
24
|
|
|
'/(octop)us$/i' => "$1i", |
|
25
|
|
|
'/(ax|test)is$/i' => "$1es", |
|
26
|
|
|
'/(us)$/i' => "$1es", |
|
27
|
|
|
'/s$/i' => "s", |
|
28
|
|
|
'/$/' => "s" |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
private static $singular = array( |
|
32
|
|
|
'/(quiz)zes$/i' => "$1", |
|
33
|
|
|
'/(matr)ices$/i' => "$1ix", |
|
34
|
|
|
'/(vert|ind)ices$/i' => "$1ex", |
|
35
|
|
|
'/^(ox)en$/i' => "$1", |
|
36
|
|
|
'/(alias)es$/i' => "$1", |
|
37
|
|
|
'/(octop|vir)i$/i' => "$1us", |
|
38
|
|
|
'/(cris|ax|test)es$/i' => "$1is", |
|
39
|
|
|
'/(shoe)s$/i' => "$1", |
|
40
|
|
|
'/(o)es$/i' => "$1", |
|
41
|
|
|
'/(bus)es$/i' => "$1", |
|
42
|
|
|
'/([m|l])ice$/i' => "$1ouse", |
|
43
|
|
|
'/(x|ch|ss|sh)es$/i' => "$1", |
|
44
|
|
|
'/(m)ovies$/i' => "$1ovie", |
|
45
|
|
|
'/(s)eries$/i' => "$1eries", |
|
46
|
|
|
'/([^aeiouy]|qu)ies$/i' => "$1y", |
|
47
|
|
|
'/([lr])ves$/i' => "$1f", |
|
48
|
|
|
'/(tive)s$/i' => "$1", |
|
49
|
|
|
'/(hive)s$/i' => "$1", |
|
50
|
|
|
'/(li|wi|kni)ves$/i' => "$1fe", |
|
51
|
|
|
'/(shea|loa|lea|thie)ves$/i'=> "$1f", |
|
52
|
|
|
'/(^analy)ses$/i' => "$1sis", |
|
53
|
|
|
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis", |
|
54
|
|
|
'/([ti])a$/i' => "$1um", |
|
55
|
|
|
'/(n)ews$/i' => "$1ews", |
|
56
|
|
|
'/(h|bl)ouses$/i' => "$1ouse", |
|
57
|
|
|
'/(corpse)s$/i' => "$1", |
|
58
|
|
|
'/(us)es$/i' => "$1", |
|
59
|
|
|
'/s$/i' => "" |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
private static $irregular = array( |
|
63
|
|
|
'move' => 'moves', |
|
64
|
|
|
'foot' => 'feet', |
|
65
|
|
|
'goose' => 'geese', |
|
66
|
|
|
'sex' => 'sexes', |
|
67
|
|
|
'child' => 'children', |
|
68
|
|
|
'man' => 'men', |
|
69
|
|
|
'tooth' => 'teeth', |
|
70
|
|
|
'person' => 'people', |
|
71
|
|
|
'valve' => 'valves' |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
private static $uncountable = array( |
|
75
|
|
|
'sheep', |
|
76
|
|
|
'fish', |
|
77
|
|
|
'deer', |
|
78
|
|
|
'series', |
|
79
|
|
|
'species', |
|
80
|
|
|
'money', |
|
81
|
|
|
'rice', |
|
82
|
|
|
'information', |
|
83
|
|
|
'equipment' |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
27 |
|
public static function pluralize($string) |
|
87
|
|
|
{ |
|
88
|
|
|
// save some time in the case that singular and plural are the same |
|
89
|
27 |
|
if (in_array(strtolower($string), self::$uncountable)) { |
|
90
|
|
|
return $string; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
// check for irregular singular forms |
|
95
|
27 |
|
foreach (self::$irregular as $pattern => $result) { |
|
96
|
27 |
|
$pattern = '/' . $pattern . '$/i'; |
|
97
|
|
|
|
|
98
|
27 |
|
if (preg_match($pattern, $string)) { |
|
99
|
9 |
|
return preg_replace($pattern, $result, $string); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// check for matches using regular expressions |
|
104
|
27 |
|
foreach (self::$plural as $pattern => $result) { |
|
105
|
27 |
|
if (preg_match($pattern, $string)) { |
|
106
|
27 |
|
return preg_replace($pattern, $result, $string); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $string; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $string |
|
115
|
|
|
* @return null|string|string[] |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function singularize($string) |
|
118
|
|
|
{ |
|
119
|
|
|
// save some time in the case that singular and plural are the same |
|
120
|
|
|
if (in_array(strtolower($string), self::$uncountable)) { |
|
121
|
|
|
return $string; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// check for irregular plural forms |
|
125
|
|
|
foreach (self::$irregular as $result => $pattern) { |
|
126
|
|
|
$pattern = '/' . $pattern . '$/i'; |
|
127
|
|
|
|
|
128
|
|
|
if (preg_match($pattern, $string)) { |
|
129
|
|
|
return preg_replace($pattern, $result, $string); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// check for matches using regular expressions |
|
134
|
|
|
foreach (self::$singular as $pattern => $result) { |
|
135
|
|
|
if (preg_match($pattern, $string)) { |
|
136
|
|
|
return preg_replace($pattern, $result, $string); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $string; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|