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 | /** |
||
35 | * Regexes for plurals |
||
36 | * @var array |
||
37 | */ |
||
38 | private static $pluralRules = [ |
||
39 | ['/child/', 'ren'], |
||
40 | ['/^ox$/', 'en'], |
||
41 | ['/(.*)(a|e|i|o|u)(?<remove>y)$/', 'ys'], |
||
42 | ['/(.*)(?<remove>y)$/', 'ies'], |
||
43 | ['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>us)$/', 'i'], |
||
44 | ['/(.*)(d|r)(?<remove>ex|ix)$/', 'ices'], |
||
45 | ['/(.*)(s|x)(?<remove>is)$/', 'es'], |
||
46 | ['/(.*)(?<remove>sh)$/', 'shes'], |
||
47 | ['/(.*)(?<remove>eau)$/', 'eaux'], |
||
48 | ['/(.*)(?<remove>um)$/', 'a'], |
||
49 | ['/(.*)(?<remove>tooth)$/', 'teeth'], |
||
50 | ['/(.*)(?<remove>h)$/', 'hes'], |
||
51 | ['/(formul|alumn|nebul)(?<remove>a)$/', 'ae'], |
||
52 | ['/(.*)(?<remove>x)$/', 'xes'], |
||
53 | ['/(.+)(?<remove>rion)$/', 'ria'], |
||
54 | ['/(.*)(?<remove>roof)$/', 'roofs'], |
||
55 | ['/(.*)[^f](?<remove>f|fe)$/', 'ves'], |
||
56 | ['/(.*)(m|l)(?<remove>ouse)$/', 'ice'], |
||
57 | ['/(.*)(?<remove>man)$/', 'men'], |
||
58 | ['/(.*)(?<remove>foot)$/', 'feet'], |
||
59 | ['/(.*)(disc|phot|pian)(?<remove>o)$/', 'os'], |
||
60 | ['/(.*)(?<remove>goose)$/', 'geese'], |
||
61 | ['/(.*)(?<remove>person)$/', 'people'], |
||
62 | ['/(.*)(?<remove>quiz)$/', 'quizzes'], |
||
63 | ['/.*(s|[bcdfghjklmnpqrstvwxyz]o|z)$/', 'es'], |
||
64 | ['/.*/', 's'] |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Regexes for singulars |
||
69 | * @var array |
||
70 | */ |
||
71 | private static $singularRules = [ |
||
72 | ['/^axe(?<remove>s)$/', ''], |
||
73 | ['/(.*)(?<remove>a)$/', 'um'], |
||
74 | ['/(.*)(dev|v|pr)(?<remove>ices)$/', 'ice'], |
||
75 | ['/(.*)(?<remove>ices)$/', 'ix'], |
||
76 | ['/(.*)(?<remove>movies)$/', 'movie'], |
||
77 | ['/(.*)(?<remove>ies)$/', 'y'], |
||
78 | ['/(.*)(?<remove>shoes)$/', 'shoe'], |
||
79 | ['/(.*)(?<remove>oes)$/', 'o'], |
||
80 | ['/(.*)(?<remove>bases)$/', 'base'], |
||
81 | ['/(.*)(?<remove>cheeses)$/', 'cheese'], |
||
82 | ['/(.*)(?<remove>children)$/', 'child'], |
||
83 | ['/(.*)(?<remove>men)$/', 'man'], |
||
84 | ['/(.*)(?<remove>feet)$/', 'foot'], |
||
85 | ['/(.*)(?<remove>geese)$/', 'goose'], |
||
86 | ['/(.*)(?<remove>atlases)$/', 'atlas'], |
||
87 | ['/(.*)(?<remove>people)$/', 'person'], |
||
88 | ['/(.*)(?<remove>teeth)$/', 'tooth'], |
||
89 | ['/(.*)(iri)(?<remove>ses)$/', 's'], |
||
90 | ['/(.*)(h|l|p)(ou)(?<remove>ses)$/', 'se'], |
||
91 | ['/(.*)(ro|po|ca)(?<remove>ses)$/', 'se'], |
||
92 | ['/(.*)(?<remove>quizzes)$/', 'quiz'], |
||
93 | ['/(.*)(?<remove>zes)$/', 'z'], |
||
94 | ['/(.*)(y|i|a|o|e)(?<remove>ses)$/', 'sis'], |
||
95 | ['/(.*)(?<remove>ses)$/', 's'], |
||
96 | ['/(.*)(?<remove>ice)$/', 'ouse'], |
||
97 | ['/(.*)(?<remove>xes)$/', 'x'], |
||
98 | ['/(.*)(?<remove>eaux)$/', 'eau'], |
||
99 | ['/(formul|alumn|nebul)(?<remove>ae)$/', 'a'], |
||
100 | ['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>i)$/', 'us'], |
||
101 | ['/(.*)(?<remove>hes)$/', 'h'], |
||
102 | ['/(.*)(ca|mo|lo)(?<remove>ves)$/', 've'], |
||
103 | ['/(.*)(l|r|o|a|e)(?<remove>ves)$/', 'f'], |
||
104 | ['/(.*)(li|ni|wi)(?<remove>ves)$/', 'fe'], |
||
105 | ['/(.*)(?<remove>s)$/', ''], |
||
106 | ]; |
||
107 | |||
108 | private static $noPlurals = [ |
||
109 | 'cod', 'deer', 'feedback', 'fish', 'moose', 'news', 'species', 'series', 'sheep', 'rice' |
||
110 | ]; |
||
111 | |||
112 | /** |
||
113 | * Converts text separated by a specified separator to camel case. |
||
114 | * This function converts the entire text into lower case before performing the |
||
115 | * camel case conversion. Due to this the first character would be lowercased. |
||
116 | * |
||
117 | * @param string $string The text to be converted. |
||
118 | * @param string $separator The separator to consider for camel casing |
||
119 | * @return string |
||
120 | */ |
||
121 | 1 | public static function camelize($string, $separator = '_') : string |
|
122 | { |
||
123 | 1 | if(is_array($separator)) |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
124 | { |
||
125 | 1 | $separator = "(\\" . implode("|\\", $separator) . ")"; |
|
126 | } |
||
127 | else |
||
128 | { |
||
129 | 1 | $separator = '\\' . $separator; |
|
130 | } |
||
131 | 1 | return preg_replace_callback( |
|
132 | 1 | "/{$separator}[a-zA-Z]/", |
|
133 | 1 | function ($matches) |
|
134 | 1 | { |
|
135 | 1 | return strtoupper($matches[0][1]); |
|
136 | 1 | }, |
|
137 | 1 | strtolower($string) |
|
138 | 1 | ); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Converts text separated by a specified separator to camel case. |
||
143 | * This method works just as the Text::camelize method except that it converts |
||
144 | * the first character to uppercase. |
||
145 | * |
||
146 | * @param string $string The text to be converted. |
||
147 | * @param string $separator The separator to consider for camel casing |
||
148 | * @return string |
||
149 | */ |
||
150 | 1 | public static function ucamelize($string, $separator = '_') : string |
|
151 | { |
||
152 | 1 | return ucfirst(self::camelize($string, $separator)); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Converts camel case text into regular text separated with an arbitrary separator. |
||
157 | * By default the seperator is an underscore. A space can also be used as the |
||
158 | * seperator in cases where the conversion is to an English sentence. |
||
159 | * |
||
160 | * @param string $string The text to be converted. |
||
161 | * @param string $separator The separator to be used. |
||
162 | * @return string |
||
163 | */ |
||
164 | 1 | public static function deCamelize($string, $separator = '_') : string |
|
165 | { |
||
166 | 1 | return preg_replace_callback( |
|
167 | 1 | "/[A-Z][a-z]/", |
|
168 | 1 | function ($matches) use($separator) |
|
169 | 1 | { |
|
170 | 1 | return $separator . strtolower($matches[0]); |
|
171 | 1 | }, |
|
172 | 1 | lcfirst($string) |
|
173 | 1 | ); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Run through the rules and generate a text transformation. |
||
178 | * |
||
179 | * @param string $text |
||
180 | * @param array $rules |
||
181 | * @return string|null |
||
182 | */ |
||
183 | 320 | private static function runInflection($text, $rules) |
|
184 | { |
||
185 | 320 | if(in_array($text, self::$noPlurals)) { |
|
186 | 18 | return $text; |
|
187 | } |
||
188 | 302 | foreach($rules as $rule) { |
|
189 | 302 | if(preg_match($rule[0], $text, $matches)) { |
|
190 | 302 | return substr($text, 0, strlen($text) - strlen($matches['remove'] ?? '')) . $rule[1]; |
|
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Generates the english plural of a given word. |
||
197 | * |
||
198 | * @param string $text |
||
199 | * @return string |
||
200 | */ |
||
201 | 166 | public static function pluralize($text) : string |
|
202 | { |
||
203 | 166 | return self::runInflection($text, self::$pluralRules); |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Generates the english singular of a given word. |
||
208 | * |
||
209 | * @param string $text |
||
210 | * @return string |
||
211 | */ |
||
212 | 154 | public static function singularize($text) : string |
|
213 | { |
||
214 | 154 | return self::runInflection($text, self::$singularRules) ?? $text; |
|
215 | } |
||
216 | } |
||
217 |