|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Ntentan Framework |
|
4
|
|
|
* Copyright (c) 2008-2015 James Ekow Abaka Ainooson |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
7
|
|
|
* a copy of this software and associated documentation files (the |
|
8
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
9
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
10
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
11
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
12
|
|
|
* the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be |
|
15
|
|
|
* included in all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
18
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
19
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
20
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
21
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
22
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
23
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace ntentan\utils; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* A couple of utility functions for manipulating strings. |
|
31
|
|
|
*/ |
|
32
|
|
|
class Text |
|
33
|
|
|
{ |
|
34
|
|
|
private static $pluralRules = [ |
|
35
|
|
|
['/child/', 'ren'], |
|
36
|
|
|
['/^ox$/', 'en'], |
|
37
|
|
|
['/(.*)(a|e|i|o|u)(?<remove>y)$/', 'ys'], |
|
38
|
|
|
['/(.*)(?<remove>y)$/', 'ies'], |
|
39
|
|
|
['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>us)$/', 'i'], |
|
40
|
|
|
['/(.*)(d|r)(?<remove>ex|ix)$/', 'ices'], |
|
41
|
|
|
['/(.*)(s|x)(?<remove>is)$/', 'es'], |
|
42
|
|
|
['/(.*)(?<remove>sh)$/', 'shes'], |
|
43
|
|
|
['/(.*)(?<remove>eau)$/', 'eaux'], |
|
44
|
|
|
['/(.*)(?<remove>um)$/', 'a'], |
|
45
|
|
|
['/(.*)(?<remove>tooth)$/', 'teeth'], |
|
46
|
|
|
['/(.*)(?<remove>h)$/', 'hes'], |
|
47
|
|
|
['/(formul|alumn|nebul)(?<remove>a)$/', 'ae'], |
|
48
|
|
|
['/(.*)(?<remove>x)$/', 'xes'], |
|
49
|
|
|
['/(.+)(?<remove>ion)$/', 'ia'], |
|
50
|
|
|
['/(.*)(?<remove>roof)$/', 'roofs'], |
|
51
|
|
|
['/(.*)[^f](?<remove>f|fe)$/', 'ves'], |
|
52
|
|
|
['/(.*)(m|l)(?<remove>ouse)$/', 'ice'], |
|
53
|
|
|
['/(.*)(?<remove>man)$/', 'men'], |
|
54
|
|
|
['/(.*)(?<remove>foot)$/', 'feet'], |
|
55
|
|
|
['/(.*)(disc|phot|pian)(?<remove>o)$/', 'os'], |
|
56
|
|
|
['/(.*)(?<remove>goose)$/', 'geese'], |
|
57
|
|
|
['/(.*)(?<remove>person)$/', 'people'], |
|
58
|
|
|
['/(.*)(?<remove>quiz)$/', 'quizzes'], |
|
59
|
|
|
['/.*(s|o|z)$/', 'es'], |
|
60
|
|
|
['/.*/', 's'] |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
private static $singularRules = [ |
|
64
|
|
|
['/^axe(?<remove>s)$/', ''], |
|
65
|
|
|
['/(.*)(?<remove>a)$/', 'um'], |
|
66
|
|
|
['/(.*)(dev|v|pr)(?<remove>ices)$/', 'ice'], |
|
67
|
|
|
['/(.*)(?<remove>ices)$/', 'ix'], |
|
68
|
|
|
['/(.*)(?<remove>movies)$/', 'movie'], |
|
69
|
|
|
['/(.*)(?<remove>ies)$/', 'y'], |
|
70
|
|
|
['/(.*)(?<remove>shoes)$/', 'shoe'], |
|
71
|
|
|
['/(.*)(?<remove>oes)$/', 'o'], |
|
72
|
|
|
['/(.*)(?<remove>bases)$/', 'base'], |
|
73
|
|
|
['/(.*)(?<remove>cheeses)$/', 'cheese'], |
|
74
|
|
|
['/(.*)(?<remove>children)$/', 'child'], |
|
75
|
|
|
['/(.*)(?<remove>men)$/', 'man'], |
|
76
|
|
|
['/(.*)(?<remove>feet)$/', 'foot'], |
|
77
|
|
|
['/(.*)(?<remove>geese)$/', 'goose'], |
|
78
|
|
|
['/(.*)(?<remove>atlases)$/', 'atlas'], |
|
79
|
|
|
['/(.*)(?<remove>people)$/', 'person'], |
|
80
|
|
|
['/(.*)(?<remove>teeth)$/', 'tooth'], |
|
81
|
|
|
['/(.*)(iri)(?<remove>ses)$/', 's'], |
|
82
|
|
|
['/(.*)(h|l|p)(ou)(?<remove>ses)$/', 'se'], |
|
83
|
|
|
['/(.*)(ro|po|ca)(?<remove>ses)$/', 'se'], |
|
84
|
|
|
['/(.*)(?<remove>quizzes)$/', 'quiz'], |
|
85
|
|
|
['/(.*)(?<remove>zes)$/', 'z'], |
|
86
|
|
|
['/(.*)(y|i|a|o|e)(?<remove>ses)$/', 'sis'], |
|
87
|
|
|
['/(.*)(?<remove>ses)$/', 's'], |
|
88
|
|
|
['/(.*)(?<remove>ice)$/', 'ouse'], |
|
89
|
|
|
['/(.*)(?<remove>xes)$/', 'x'], |
|
90
|
|
|
['/(.*)(?<remove>eaux)$/', 'eau'], |
|
91
|
|
|
['/(formul|alumn|nebul)(?<remove>ae)$/', 'a'], |
|
92
|
|
|
['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>i)$/', 'us'], |
|
93
|
|
|
['/(.*)(?<remove>hes)$/', 'h'], |
|
94
|
|
|
['/(.*)(ca|mo|lo)(?<remove>ves)$/', 've'], |
|
95
|
|
|
['/(.*)(l|r|o|a|e)(?<remove>ves)$/', 'f'], |
|
96
|
|
|
['/(.*)(li|ni|wi)(?<remove>ves)$/', 'fe'], |
|
97
|
|
|
['/(.*)(?<remove>s)$/', ''], |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
private static $noPlurals = [ |
|
101
|
|
|
'cod', 'deer', 'feedback', 'fish', 'moose', 'news', 'species', 'series', 'sheep', 'rice' |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Converts text separated by a specified separator to camel case. |
|
106
|
|
|
* This function converts the entire text into lower case before performing the |
|
107
|
|
|
* camel case conversion. Due to this the first character would be lowercased. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $string The text to be converted. |
|
110
|
|
|
* @param string $separator The separator to consider for camel casing |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public static function camelize($string, $separator = '_') : string |
|
114
|
|
|
{ |
|
115
|
1 |
|
if(is_array($separator)) |
|
116
|
|
|
{ |
|
117
|
1 |
|
$separator = "(\\" . implode("|\\", $separator) . ")"; |
|
118
|
|
|
} |
|
119
|
|
|
else |
|
120
|
|
|
{ |
|
121
|
1 |
|
$separator = '\\' . $separator; |
|
122
|
|
|
} |
|
123
|
1 |
|
return preg_replace_callback( |
|
124
|
1 |
|
"/{$separator}[a-zA-Z]/", |
|
125
|
1 |
|
function ($matches) |
|
126
|
|
|
{ |
|
127
|
1 |
|
return strtoupper($matches[0][1]); |
|
128
|
1 |
|
}, |
|
129
|
1 |
|
strtolower($string) |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Converts text separated by a specified separator to camel case. |
|
135
|
|
|
* This method works just as the Text::camelize method except that it converts |
|
136
|
|
|
* the first character to uppercase. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $string The text to be converted. |
|
139
|
|
|
* @param string $separator The separator to consider for camel casing |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public static function ucamelize($string, $separator = '_') : string |
|
143
|
|
|
{ |
|
144
|
1 |
|
return ucfirst(self::camelize($string, $separator)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Converts camel case text into regular text separated with an arbitrary separator. |
|
149
|
|
|
* By default the seperator is an underscore. A space can also be used as the |
|
150
|
|
|
* seperator in cases where the conversion is to an English sentence. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $string The text to be converted. |
|
153
|
|
|
* @param string $separator The separator to be used. |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public static function deCamelize($string, $separator = '_') : string |
|
157
|
|
|
{ |
|
158
|
1 |
|
return preg_replace_callback( |
|
159
|
1 |
|
"/[A-Z][a-z]/", |
|
160
|
1 |
|
function ($matches) use($separator) |
|
161
|
|
|
{ |
|
162
|
1 |
|
return $separator . strtolower($matches[0]); |
|
163
|
1 |
|
}, |
|
164
|
1 |
|
lcfirst($string) |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Generates the english plural of a given word. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $text |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
166 |
View Code Duplication |
public static function pluralize($text) : string |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
166 |
|
if(in_array($text, self::$noPlurals)) { |
|
177
|
9 |
|
return $text; |
|
178
|
|
|
} |
|
179
|
157 |
|
foreach(self::$pluralRules as $rule) { |
|
180
|
157 |
|
if(preg_match($rule[0], $text, $matches)) { |
|
181
|
157 |
|
return substr($text, 0, strlen($text) - strlen($matches['remove'] ?? '')) . $rule[1]; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Generates the english singular of a given word. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $text |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
154 |
View Code Duplication |
public static function singularize($text) : string |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
154 |
|
if(in_array($text, self::$noPlurals)) { |
|
195
|
9 |
|
return $text; |
|
196
|
|
|
} |
|
197
|
145 |
|
foreach(self::$singularRules as $rule) { |
|
198
|
145 |
|
if(preg_match($rule[0], $text, $matches)) { |
|
199
|
145 |
|
return substr($text, 0, strlen($text) - strlen($matches['remove'] ?? '')) . $rule[1]; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
return $text; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.