1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | // ------------------------------------------------------------------------ |
||
12 | |||
13 | if ( ! function_exists('readable')) { |
||
14 | /** |
||
15 | * readable |
||
16 | * |
||
17 | * @param $string |
||
18 | * @param bool $capitalize |
||
19 | * |
||
20 | * @return mixed|string |
||
21 | */ |
||
22 | function readable($string, $capitalize = false) |
||
23 | { |
||
24 | $string = trim($string); |
||
25 | $string = str_replace(['-', '_'], ' ', $string); |
||
26 | |||
27 | if ($capitalize == true) { |
||
0 ignored issues
–
show
|
|||
28 | return ucwords($string); |
||
29 | } |
||
30 | |||
31 | return $string; |
||
32 | } |
||
33 | } |
||
34 | |||
35 | // ------------------------------------------------------------------------ |
||
36 | |||
37 | if ( ! function_exists('singular')) { |
||
38 | /** |
||
39 | * singular |
||
40 | * |
||
41 | * Takes a plural word and makes it singular |
||
42 | * |
||
43 | * @param string $string Input string |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | function singular($string) |
||
48 | { |
||
49 | $result = strval($string); |
||
50 | |||
51 | if ( ! is_countable($result)) { |
||
52 | return $result; |
||
53 | } |
||
54 | |||
55 | $rules = [ |
||
56 | '/(matr)ices$/' => '\1ix', |
||
57 | '/(vert|ind)ices$/' => '\1ex', |
||
58 | '/^(ox)en/' => '\1', |
||
59 | '/(alias)es$/' => '\1', |
||
60 | '/([octop|vir])i$/' => '\1us', |
||
61 | '/(cris|ax|test)es$/' => '\1is', |
||
62 | '/(shoe)s$/' => '\1', |
||
63 | '/(o)es$/' => '\1', |
||
64 | '/(bus|campus)es$/' => '\1', |
||
65 | '/([m|l])ice$/' => '\1ouse', |
||
66 | '/(x|ch|ss|sh)es$/' => '\1', |
||
67 | '/(m)ovies$/' => '\1\2ovie', |
||
68 | '/(s)eries$/' => '\1\2eries', |
||
69 | '/([^aeiouy]|qu)ies$/' => '\1y', |
||
70 | '/([lr])ves$/' => '\1f', |
||
71 | '/(tive)s$/' => '\1', |
||
72 | '/(hive)s$/' => '\1', |
||
73 | '/([^f])ves$/' => '\1fe', |
||
74 | '/(^analy)ses$/' => '\1sis', |
||
75 | '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis', |
||
76 | '/([ti])a$/' => '\1um', |
||
77 | '/(p)eople$/' => '\1\2erson', |
||
78 | '/(m)en$/' => '\1an', |
||
79 | '/(s)tatuses$/' => '\1\2tatus', |
||
80 | '/(c)hildren$/' => '\1\2hild', |
||
81 | '/(n)ews$/' => '\1\2ews', |
||
82 | '/([^us])s$/' => '\1', |
||
83 | ]; |
||
84 | |||
85 | foreach ($rules as $rule => $replacement) { |
||
86 | if (preg_match($rule, $result)) { |
||
87 | $result = preg_replace($rule, $replacement, $result); |
||
88 | break; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | return $result; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // ------------------------------------------------------------------------ |
||
97 | |||
98 | if ( ! function_exists('plural')) { |
||
99 | /** |
||
100 | * plural |
||
101 | * |
||
102 | * Takes a singular word and makes it plural |
||
103 | * |
||
104 | * @param string $string Input string |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | function plural($string) |
||
109 | { |
||
110 | $result = strval($string); |
||
111 | |||
112 | if ( ! is_countable($result)) { |
||
113 | return $result; |
||
114 | } |
||
115 | |||
116 | $rules = [ |
||
117 | '/^(ox)$/' => '\1\2en', // ox |
||
118 | '/([m|l])ouse$/' => '\1ice', // mouse, louse |
||
119 | '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index |
||
120 | '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address |
||
121 | '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency |
||
122 | '/(hive)$/' => '\1s', // archive, hive |
||
123 | '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife |
||
124 | '/sis$/' => 'ses', // basis, diagnosis |
||
125 | '/([ti])um$/' => '\1a', // datum, medium |
||
126 | '/(p)erson$/' => '\1eople', // person, salesperson |
||
127 | '/(m)an$/' => '\1en', // man, woman, spokesman |
||
128 | '/(c)hild$/' => '\1hildren', // child |
||
129 | '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato |
||
130 | '/(bu|campu)s$/' => '\1\2ses', // bus, campus |
||
131 | '/(alias|status|virus)$/' => '\1es', // alias |
||
132 | '/(octop)us$/' => '\1i', // octopus |
||
133 | '/(ax|cris|test)is$/' => '\1es', // axis, crisis |
||
134 | '/s$/' => 's', // no change (compatibility) |
||
135 | '/$/' => 's', |
||
136 | ]; |
||
137 | |||
138 | foreach ($rules as $rule => $replacement) { |
||
139 | if (preg_match($rule, $result)) { |
||
140 | $result = preg_replace($rule, $replacement, $result); |
||
141 | break; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | return $result; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // ------------------------------------------------------------------------ |
||
150 | |||
151 | if ( ! function_exists('studlycase')) { |
||
152 | /** |
||
153 | * studlycase |
||
154 | * |
||
155 | * Convert a value to studly caps case (StudlyCapCase). |
||
156 | * |
||
157 | * @param string $string |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | function studlycase($string) |
||
162 | { |
||
163 | return ucfirst(camelcase($string)); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | // ------------------------------------------------------------------------ |
||
168 | |||
169 | if ( ! function_exists('camelcase')) { |
||
170 | /** |
||
171 | * camelcase |
||
172 | * |
||
173 | * Takes multiple words separated by spaces, underscores or dashes and camelizes them. |
||
174 | * |
||
175 | * @param string $string Input string |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | function camelcase($string) |
||
180 | { |
||
181 | $string = trim($string); |
||
182 | |||
183 | if (strtoupper($string) === $string) { |
||
184 | return (string)$string; |
||
185 | } |
||
186 | |||
187 | return lcfirst(str_replace(' ', '', ucwords(preg_replace('/[\s_-]+/', ' ', $string)))); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // ------------------------------------------------------------------------ |
||
192 | |||
193 | if ( ! function_exists('snakecase')) { |
||
194 | /** |
||
195 | * snakecase |
||
196 | * |
||
197 | * Convert camelCase into camel_case. |
||
198 | * |
||
199 | * @param $string |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | function snakecase($string) |
||
204 | { |
||
205 | return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string)); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | // ------------------------------------------------------------------------ |
||
210 | |||
211 | if ( ! function_exists('underscore')) { |
||
212 | /** |
||
213 | * underscore |
||
214 | * |
||
215 | * Takes multiple words separated by spaces and underscores them |
||
216 | * |
||
217 | * @param string $string Input string |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | function underscore($string) |
||
222 | { |
||
223 | $string = trim($string); |
||
224 | $string = str_replace(['/', '\\'], '-', snakecase($string)); |
||
225 | |||
226 | $string = strtolower(preg_replace( |
||
227 | ['#[\\s-]+#', '#[^A-Za-z0-9\. -]+#'], |
||
228 | ['-', ''], |
||
229 | $string |
||
230 | )); |
||
231 | |||
232 | return str_replace('-', '_', $string); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | |||
237 | // ------------------------------------------------------------------------ |
||
238 | |||
239 | if ( ! function_exists('dash')) { |
||
240 | /** |
||
241 | * dash |
||
242 | * |
||
243 | * Takes multiple words separated by spaces and dashes them |
||
244 | * |
||
245 | * @param string $string Input string |
||
246 | * |
||
247 | * @access public |
||
248 | * @return string |
||
249 | */ |
||
250 | function dash($string) |
||
251 | { |
||
252 | $string = trim($string); |
||
253 | $string = str_replace(['/', '\\'], '-', snakecase($string)); |
||
254 | |||
255 | $string = strtolower(preg_replace( |
||
256 | ['#[\\s_-]+#', '#[^A-Za-z0-9\. _-]+#'], |
||
257 | ['_', ''], |
||
258 | $string |
||
259 | )); |
||
260 | |||
261 | return str_replace('_', '-', $string); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | |||
266 | // ------------------------------------------------------------------------ |
||
267 | |||
268 | if ( ! function_exists('is_countable')) { |
||
269 | /** |
||
270 | * is_countable |
||
271 | * |
||
272 | * Checks if the given word has a plural version. |
||
273 | * |
||
274 | * @param string $string Word to check |
||
275 | * |
||
276 | * @access public |
||
277 | * @return bool |
||
278 | */ |
||
279 | function is_countable($string) |
||
280 | { |
||
281 | return ! in_array( |
||
282 | strtolower($string), |
||
283 | [ |
||
284 | 'equipment', |
||
285 | 'information', |
||
286 | 'rice', |
||
287 | 'money', |
||
288 | 'species', |
||
289 | 'series', |
||
290 | 'fish', |
||
291 | 'meta', |
||
292 | ] |
||
293 | ); |
||
294 | } |
||
295 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.