|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Inflector |
|
7
|
|
|
* |
|
8
|
|
|
* Changes words to plural, singular, camel case, etc. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Wildfire |
|
11
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Inflector |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Takes a plural word and makes it singular. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $string |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
18 |
|
public static function singular($string) |
|
22
|
|
|
{ |
|
23
|
18 |
|
$result = strval($string); |
|
24
|
|
|
|
|
25
|
18 |
|
if ( ! self::isCountable($result)) { |
|
26
|
3 |
|
return $result; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$singular_rules = [ |
|
30
|
15 |
|
'/(matr)ices$/' => '\1ix', |
|
31
|
15 |
|
'/(vert|ind)ices$/' => '\1ex', |
|
32
|
15 |
|
'/^(ox)en/' => '\1', |
|
33
|
15 |
|
'/(alias)es$/' => '\1', |
|
34
|
15 |
|
'/([octop|vir])i$/' => '\1us', |
|
35
|
15 |
|
'/(cris|ax|test)es$/' => '\1is', |
|
36
|
15 |
|
'/(shoe)s$/' => '\1', |
|
37
|
15 |
|
'/(o)es$/' => '\1', |
|
38
|
15 |
|
'/(bus|campus)es$/' => '\1', |
|
39
|
15 |
|
'/([m|l])ice$/' => '\1ouse', |
|
40
|
15 |
|
'/(x|ch|ss|sh)es$/' => '\1', |
|
41
|
15 |
|
'/(m)ovies$/' => '\1\2ovie', |
|
42
|
15 |
|
'/(s)eries$/' => '\1\2eries', |
|
43
|
15 |
|
'/([^aeiouy]|qu)ies$/' => '\1y', |
|
44
|
15 |
|
'/([lr])ves$/' => '\1f', |
|
45
|
15 |
|
'/(tive)s$/' => '\1', |
|
46
|
15 |
|
'/(hive)s$/' => '\1', |
|
47
|
15 |
|
'/([^f])ves$/' => '\1fe', |
|
48
|
15 |
|
'/(^analy)ses$/' => '\1sis', |
|
49
|
15 |
|
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis', |
|
50
|
15 |
|
'/([ti])a$/' => '\1um', |
|
51
|
15 |
|
'/(p)eople$/' => '\1\2erson', |
|
52
|
15 |
|
'/(m)en$/' => '\1an', |
|
53
|
15 |
|
'/(s)tatuses$/' => '\1\2tatus', |
|
54
|
15 |
|
'/(c)hildren$/' => '\1\2hild', |
|
55
|
15 |
|
'/(n)ews$/' => '\1\2ews', |
|
56
|
|
|
'/([^us])s$/' => '\1' |
|
57
|
15 |
|
]; |
|
58
|
|
|
|
|
59
|
15 |
|
foreach ($singular_rules as $rule => $replacement) { |
|
60
|
15 |
|
if (preg_match($rule, $result)) { |
|
61
|
3 |
|
$result = preg_replace($rule, $replacement, $result); |
|
62
|
|
|
|
|
63
|
3 |
|
break; |
|
64
|
|
|
} |
|
65
|
15 |
|
} |
|
66
|
|
|
|
|
67
|
15 |
|
return $result; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Takes multiple words separated by spaces or underscores and camelizes them. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $string |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
6 |
|
public static function camelize($string) |
|
77
|
|
|
{ |
|
78
|
6 |
|
$ucwords = ucwords(preg_replace('/[\s_]+/', ' ', $string)); |
|
79
|
6 |
|
$strReplace = str_replace(' ', '', $ucwords); |
|
80
|
6 |
|
$subString = substr($strReplace, 1); |
|
81
|
|
|
|
|
82
|
6 |
|
return strtolower($string[0]) . $subString; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Checks if the given word has a plural version. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $word |
|
89
|
|
|
* @return boolean |
|
90
|
|
|
*/ |
|
91
|
18 |
|
protected static function isCountable($word) |
|
92
|
|
|
{ |
|
93
|
18 |
|
return ! in_array( |
|
94
|
18 |
|
strtolower($word), |
|
95
|
|
|
[ |
|
96
|
18 |
|
'equipment', 'information', 'rice', 'money', |
|
97
|
18 |
|
'species', 'series', 'fish', 'meta' |
|
98
|
18 |
|
] |
|
99
|
18 |
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|