-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inflector.php
172 lines (143 loc) · 4.57 KB
/
Inflector.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
<?php
class Inflector {
// Cached inflections
protected static $cache = array();
// Uncountable and irregular words
protected static $uncountable;
protected static $irregular;
/**
* Checks if a word is defined as uncountable.
*
* @param string $str word to check
* @return boolean
*/
public static function uncountable($str) {
if (self::$uncountable === NULL) {
// Cache uncountables
self::$uncountable =
AetherDatabaseConfig::retrieve('inflector.uncountable');
// Make uncountables mirroed
self::$uncountable = array_combine(self::$uncountable,
self::$uncountable);
}
return isset(self::$uncountable[strtolower($str)]);
}
/**
* Makes a plural word singular.
*
* @param string $str word to singularize
* @param integer $count number of things
* @return string
*/
public static function singular($str, $count = NULL) {
// Remove garbage
$str = strtolower(trim($str));
if (is_string($count)) {
// Convert to integer when using a digit string
$count = (int)$count;
}
// Do nothing with a single count
if ($count === 0 || $count > 1)
return $str;
// Cache key name
$key = 'singular_' . $str . $count;
if (isset(self::$cache[$key]))
return self::$cache[$key];
if (self::uncountable($str))
return self::$cache[$key] = $str;
if (empty(self::$irregular)) {
// Cache irregular words
self::$irregular =
AetherDatabaseConfig::retrieve('inflector.irregular');
}
if ($irregular = array_search($str, self::$irregular)) {
$str = $irregular;
}
elseif (preg_match('/[sxz]es$/', $str) ||
preg_match('/[^aeioudgkprt]hes$/', $str)) {
// Remove "es"
$str = substr($str, 0, -2);
}
elseif (preg_match('/[^aeiou]ies$/', $str)) {
$str = substr($str, 0, -3).'y';
}
elseif (substr($str, -1) === 's' && substr($str, -2) !== 'ss') {
$str = substr($str, 0, -1);
}
return self::$cache[$key] = $str;
}
/**
* Makes a singular word plural.
*
* @param string $str word to pluralize
* @param int $count
* @return string
*/
public static function plural($str, $count = NULL) {
// Remove garbage
$str = strtolower(trim($str));
if (is_string($count)) {
// Convert to integer when using a digit string
$count = (int)$count;
}
// Do nothing with singular
if ($count === 1)
return $str;
// Cache key name
$key = 'plural_' . $str . $count;
if (isset(self::$cache[$key]))
return self::$cache[$key];
if (self::uncountable($str))
return self::$cache[$key] = $str;
if (empty(self::$irregular)) {
// Cache irregular words
self::$irregular =
AetherDatabaseConfig::retrieve('inflector.irregular');
}
if (isset(self::$irregular[$str])) {
$str = self::$irregular[$str];
}
elseif (preg_match('/[sxz]$/', $str) ||
preg_match('/[^aeioudgkprt]h$/', $str)) {
$str .= 'es';
}
elseif (preg_match('/[^aeiou]y$/', $str)) {
// Change "y" to "ies"
$str = substr_replace($str, 'ies', -1);
}
else {
$str .= 's';
}
// Set the cache and return
return self::$cache[$key] = $str;
}
/**
* Makes a phrase camel case.
*
* @param string $str phrase to camelize
* @return string
*/
public static function camelize($str) {
$str = 'x' . strtolower(trim($str));
$str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
return substr(str_replace(' ', '', $str), 1);
}
/**
* Makes a phrase underscored instead of spaced.
*
* @param string phrase to underscore
* @return string
*/
public static function underscore($str) {
return preg_replace('/\s+/', '_', trim($str));
}
/**
* Makes an underscored or dashed phrase human-reable.
*
* @param string phrase to make human-reable
* @return string
*/
public static function humanize($str) {
return preg_replace('/[_-]+/', ' ', trim($str));
}
} // End inflector