| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Support; |
| 4 |
|
| 5 |
class Pluralizer { |
| 6 |
|
| 7 |
/** |
| 8 |
* Plural word form rules. |
| 9 |
* |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
public static $plural = array( |
| 13 |
'/(quiz)$/i' => "$1zes", |
| 14 |
'/^(ox)$/i' => "$1en", |
| 15 |
'/([m|l])ouse$/i' => "$1ice", |
| 16 |
'/(matr|vert|ind)ix$|ex$/i' => "$1ices", |
| 17 |
'/(stoma|epo|monar|matriar|patriar|oligar|eunu)ch$/i' => "$1chs", |
| 18 |
'/(x|ch|ss|sh)$/i' => "$1es", |
| 19 |
'/([^aeiouy]|qu)y$/i' => "$1ies", |
| 20 |
'/(hive)$/i' => "$1s", |
| 21 |
'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves", |
| 22 |
'/(shea|lea|loa|thie)f$/i' => "$1ves", |
| 23 |
'/sis$/i' => "ses", |
| 24 |
'/([ti])um$/i' => "$1a", |
| 25 |
'/(torped|embarg|tomat|potat|ech|her|vet)o$/i' => "$1oes", |
| 26 |
'/(bu)s$/i' => "$1ses", |
| 27 |
'/(alias)$/i' => "$1es", |
| 28 |
'/(fung)us$/i' => "$1i", |
| 29 |
'/(ax|test)is$/i' => "$1es", |
| 30 |
'/(us)$/i' => "$1es", |
| 31 |
'/s$/i' => "s", |
| 32 |
'/$/' => "s", |
| 33 |
); |
| 34 |
|
| 35 |
/** |
| 36 |
* Singular word form rules. |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
public static $singular = array( |
| 41 |
'/(quiz)zes$/i' => "$1", |
| 42 |
'/(matr)ices$/i' => "$1ix", |
| 43 |
'/(vert|vort|ind)ices$/i' => "$1ex", |
| 44 |
'/^(ox)en$/i' => "$1", |
| 45 |
'/(alias)es$/i' => "$1", |
| 46 |
'/(octop|vir|fung)i$/i' => "$1us", |
| 47 |
'/(cris|ax|test)es$/i' => "$1is", |
| 48 |
'/(shoe)s$/i' => "$1", |
| 49 |
'/(o)es$/i' => "$1", |
| 50 |
'/(bus)es$/i' => "$1", |
| 51 |
'/([m|l])ice$/i' => "$1ouse", |
| 52 |
'/(x|ch|ss|sh)es$/i' => "$1", |
| 53 |
'/(m)ovies$/i' => "$1ovie", |
| 54 |
'/(s)eries$/i' => "$1eries", |
| 55 |
'/([^aeiouy]|qu)ies$/i' => "$1y", |
| 56 |
'/([lr])ves$/i' => "$1f", |
| 57 |
'/(tive)s$/i' => "$1", |
| 58 |
'/(hive)s$/i' => "$1", |
| 59 |
'/(li|wi|kni)ves$/i' => "$1fe", |
| 60 |
'/(shea|loa|lea|thie)ves$/i' => "$1f", |
| 61 |
'/(^analy)ses$/i' => "$1sis", |
| 62 |
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis", |
| 63 |
'/([ti])a$/i' => "$1um", |
| 64 |
'/(n)ews$/i' => "$1ews", |
| 65 |
'/(h|bl)ouses$/i' => "$1ouse", |
| 66 |
'/(corpse)s$/i' => "$1", |
| 67 |
'/(gallows|headquarters)$/i' => "$1", |
| 68 |
'/(us)es$/i' => "$1", |
| 69 |
'/(us|ss)$/i' => "$1", |
| 70 |
'/s$/i' => "", |
| 71 |
); |
| 72 |
|
| 73 |
/** |
| 74 |
* Irregular word forms. |
| 75 |
* |
| 76 |
* @var array |
| 77 |
*/ |
| 78 |
public static $irregular = array( |
| 79 |
'child' => 'children', |
| 80 |
'corpus' => 'corpora', |
| 81 |
'criterion' => 'criteria', |
| 82 |
'foot' => 'feet', |
| 83 |
'freshman' => 'freshmen', |
| 84 |
'goose' => 'geese', |
| 85 |
'genus' => 'genera', |
| 86 |
'human' => 'humans', |
| 87 |
'man' => 'men', |
| 88 |
'move' => 'moves', |
| 89 |
'nucleus' => 'nuclei', |
| 90 |
'ovum' => 'ova', |
| 91 |
'person' => 'people', |
| 92 |
'phenomenon' => 'phenomena', |
| 93 |
'radius' => 'radii', |
| 94 |
'sex' => 'sexes', |
| 95 |
'stimulus' => 'stimuli', |
| 96 |
'syllabus' => 'syllabi', |
| 97 |
'tax' => 'taxes', |
| 98 |
'tech' => 'techs', |
| 99 |
'tooth' => 'teeth', |
| 100 |
'viscus' => 'viscera', |
| 101 |
); |
| 102 |
|
| 103 |
/** |
| 104 |
* Uncountable word forms. |
| 105 |
* |
| 106 |
* @var array |
| 107 |
*/ |
| 108 |
public static $uncountable = array( |
| 109 |
'audio', |
| 110 |
'bison', |
| 111 |
'chassis', |
| 112 |
'compensation', |
| 113 |
'coreopsis', |
| 114 |
'data', |
| 115 |
'deer', |
| 116 |
'education', |
| 117 |
'equipment', |
| 118 |
'fish', |
| 119 |
'gold', |
| 120 |
'information', |
| 121 |
'money', |
| 122 |
'moose', |
| 123 |
'offspring', |
| 124 |
'plankton', |
| 125 |
'police', |
| 126 |
'rice', |
| 127 |
'series', |
| 128 |
'sheep', |
| 129 |
'species', |
| 130 |
'swine', |
| 131 |
'traffic', |
| 132 |
); |
| 133 |
|
| 134 |
/** |
| 135 |
* The cached copies of the plural inflections. |
| 136 |
* |
| 137 |
* @var array |
| 138 |
*/ |
| 139 |
protected static $pluralCache = array(); |
| 140 |
|
| 141 |
/** |
| 142 |
* The cached copies of the singular inflections. |
| 143 |
* |
| 144 |
* @var array |
| 145 |
*/ |
| 146 |
protected static $singularCache = array(); |
| 147 |
|
| 148 |
/** |
| 149 |
* Get the singular form of the given word. |
| 150 |
* |
| 151 |
* @param string $value |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
public static function singular($value) |
| 155 |
{ |
| 156 |
if (isset(static::$singularCache[$value])) |
| 157 |
{ |
| 158 |
return static::$singularCache[$value]; |
| 159 |
} |
| 160 |
|
| 161 |
$result = static::inflect($value, static::$singular, static::$irregular); |
| 162 |
|
| 163 |
return static::$singularCache[$value] = $result ?: $value; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the plural form of the given word. |
| 168 |
* |
| 169 |
* @param string $value |
| 170 |
* @param int $count |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
public static function plural($value, $count = 2) |
| 174 |
{ |
| 175 |
if ($count == 1) return $value; |
| 176 |
|
| 177 |
if (in_array($value, static::$irregular)) return $value; |
| 178 |
|
| 179 |
// First we'll check the cache of inflected values. We cache each word that |
| 180 |
// is inflected so we don't have to spin through the regular expressions |
| 181 |
// on each subsequent method calls for this word by the app developer. |
| 182 |
if (isset(static::$pluralCache[$value])) |
| 183 |
{ |
| 184 |
return static::$pluralCache[$value]; |
| 185 |
} |
| 186 |
|
| 187 |
$irregular = array_flip(static::$irregular); |
| 188 |
|
| 189 |
// When doing the singular to plural transformation, we'll flip the irregular |
| 190 |
// array since we need to swap sides on the keys and values. After we have |
| 191 |
// the transformed value we will cache it in memory for faster look-ups. |
| 192 |
$plural = static::$plural; |
| 193 |
|
| 194 |
$result = static::inflect($value, $plural, $irregular); |
| 195 |
|
| 196 |
return static::$pluralCache[$value] = $result; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Perform auto inflection on an English word. |
| 201 |
* |
| 202 |
* @param string $value |
| 203 |
* @param array $source |
| 204 |
* @param array $irregular |
| 205 |
* @return string |
| 206 |
*/ |
| 207 |
protected static function inflect($value, $source, $irregulars) |
| 208 |
{ |
| 209 |
if (static::uncountable($value)) return $value; |
| 210 |
|
| 211 |
// Next, we will check the "irregular" patterns which contain words that are |
| 212 |
// not easily summarized in regular expression rules, like "children" and |
| 213 |
// "teeth", both of which cannot get inflected using our typical rules. |
| 214 |
foreach ($irregulars as $irregular => $pattern) { |
| 215 |
|
| 216 |
if (preg_match($pattern = '/'.$pattern.'$/i', $value)) { |
| 217 |
|
| 218 |
$irregular = static::matchCase($irregular, $value); |
| 219 |
|
| 220 |
return preg_replace($pattern, $irregular, $value); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// Finally, we'll spin through the array of regular expressions and look for |
| 225 |
// matches for the word. If we find a match, we will cache and return the |
| 226 |
// transformed value so we will quickly look it up on subsequent calls. |
| 227 |
foreach ($source as $pattern => $inflected) |
| 228 |
{ |
| 229 |
if (preg_match($pattern, $value)) |
| 230 |
{ |
| 231 |
$inflected = preg_replace($pattern, $inflected, $value); |
| 232 |
|
| 233 |
return static::matchCase($inflected, $value); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Determine if the given value is uncountable. |
| 240 |
* |
| 241 |
* @param string $value |
| 242 |
* @return bool |
| 243 |
*/ |
| 244 |
protected static function uncountable($value) |
| 245 |
{ |
| 246 |
return in_array(strtolower($value), static::$uncountable); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Attempt to match the case on two strings. |
| 251 |
* |
| 252 |
* @param string $value |
| 253 |
* @param string $comparison |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
protected static function matchCase($value, $comparison) |
| 257 |
{ |
| 258 |
$functions = array('mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords'); |
| 259 |
|
| 260 |
foreach ($functions as $function) |
| 261 |
{ |
| 262 |
if (call_user_func($function, $comparison) === $comparison) |
| 263 |
{ |
| 264 |
return call_user_func($function, $value); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return $value; |
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|