1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace __\Traits; |
4
|
|
|
|
5
|
|
|
use __; |
6
|
|
|
|
7
|
|
|
trait Strings |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Split a string by string. |
11
|
|
|
* |
12
|
|
|
* @link http://php.net/manual/en/function.explode.php Based on explode |
13
|
|
|
* @usage __::split('a-b-c', '-', 2); |
14
|
|
|
* >> ['a', 'b-c'] |
15
|
|
|
* |
16
|
|
|
* @param string $input The string to split. |
17
|
|
|
* @param string $delimiter The boundary string. |
18
|
|
|
* @param int $limit (optional) If limit is set and positive, the returned array |
19
|
|
|
* will contain a maximum of limit elements with the last element containing the |
20
|
|
|
* rest of string. |
21
|
|
|
* If the limit parameter is negative, all components except the last -limit are returned. |
22
|
|
|
* If the limit parameter is zero, then this is treated as 1. |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
18 |
|
public static function split(string $input, string $delimiter, int $limit = PHP_INT_MAX) |
27
|
|
|
{ |
28
|
18 |
|
return explode($delimiter, $input, $limit); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Converts string to [camel case](https://en.wikipedia.org/wiki/CamelCase). |
33
|
|
|
* |
34
|
|
|
* @usage __::camelCase('Foo Bar'); |
35
|
|
|
* >> 'fooBar' |
36
|
|
|
* |
37
|
|
|
* @param string $input |
38
|
|
|
* |
39
|
|
|
* @return string|array |
40
|
|
|
*/ |
41
|
1 |
|
public static function camelCase(string $input) |
42
|
|
|
{ |
43
|
1 |
|
$words = __::words(preg_replace("/['\x{2019}]/u", '', $input)); |
44
|
|
|
|
45
|
1 |
|
return array_reduce( |
46
|
1 |
|
$words, |
47
|
|
|
function ($result, $word) use ($words) { |
48
|
1 |
|
$isFirst = __::first($words) === $word; |
49
|
1 |
|
$word = __::toLower($word); |
50
|
|
|
|
51
|
1 |
|
return $result . (!$isFirst ? __::capitalize($word) : $word); |
52
|
1 |
|
}, |
53
|
1 |
|
'' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Converts the first character of string to upper case and the remaining to lower case. |
59
|
|
|
* |
60
|
|
|
* @usage __::capitalize('FRED'); |
61
|
|
|
* >> 'Fred' |
62
|
|
|
* |
63
|
|
|
* @param string $input |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
2 |
|
public static function capitalize(string $input): string |
68
|
|
|
{ |
69
|
2 |
|
return __::upperFirst(__::toLower($input)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Converts string to kebab case. |
74
|
|
|
* |
75
|
|
|
* @link https://en.wikipedia.org/wiki/Letter_case#Special_case_styles kebab case |
76
|
|
|
* |
77
|
|
|
* @usage __::kebabCase('Foo Bar'); |
78
|
|
|
* >> 'foo-bar' |
79
|
|
|
* |
80
|
|
|
* @param string $input |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
1 |
|
public static function kebabCase(string $input): string |
85
|
|
|
{ |
86
|
1 |
|
$words = __::words(preg_replace("/['\x{2019}]/u", '', $input)); |
87
|
|
|
|
88
|
1 |
|
return array_reduce( |
89
|
1 |
|
$words, |
90
|
|
|
function ($result, $word) use ($words) { |
91
|
1 |
|
$isFirst = __::first($words) === $word; |
92
|
|
|
|
93
|
1 |
|
return $result . (!$isFirst ? '-' : '') . __::toLower($word); |
94
|
1 |
|
}, |
95
|
1 |
|
'' |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Converts the first character of string to lower case, like lcfirst. |
101
|
|
|
* |
102
|
|
|
* @usage __::lowerFirst('Fred'); |
103
|
|
|
* >> 'fred' |
104
|
|
|
* |
105
|
|
|
* @param string $input |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
1 |
|
public static function lowerFirst(string $input): string |
110
|
|
|
{ |
111
|
1 |
|
return lcfirst($input); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Converts string to snake case. |
116
|
|
|
* |
117
|
|
|
* @link https://en.wikipedia.org/wiki/Snake_case snake case |
118
|
|
|
* |
119
|
|
|
* @usage __::snakeCase('Foo Bar'); |
120
|
|
|
* >> 'foo_bar' |
121
|
|
|
* |
122
|
|
|
* @param string $input |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
1 |
|
public static function snakeCase(string $input): string |
127
|
|
|
{ |
128
|
1 |
|
$words = __::words(preg_replace("/['\x{2019}]/u", '', $input)); |
129
|
|
|
|
130
|
1 |
|
return array_reduce( |
131
|
1 |
|
$words, |
132
|
|
|
function ($result, $word) use ($words) { |
133
|
1 |
|
$isFirst = __::first($words) === $word; |
134
|
|
|
|
135
|
1 |
|
return $result . (!$isFirst ? '_' : '') . __::toLower($word); |
136
|
1 |
|
}, |
137
|
1 |
|
'' |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Converts string to start case. |
143
|
|
|
* |
144
|
|
|
* @link https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage start case |
145
|
|
|
* |
146
|
|
|
* @usage __::startCase('--foo-bar--'); |
147
|
|
|
* >> 'Foo Bar' |
148
|
|
|
* |
149
|
|
|
* @param string $input |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
1 |
|
public static function startCase(string $input): string |
154
|
|
|
{ |
155
|
1 |
|
$words = __::words(preg_replace("/['\x{2019}]/u", '', $input)); |
156
|
|
|
|
157
|
1 |
|
return array_reduce( |
158
|
1 |
|
$words, |
159
|
|
|
function ($result, $word) use ($words) { |
160
|
1 |
|
$isFirst = __::first($words) === $word; |
161
|
|
|
|
162
|
1 |
|
return $result . (!$isFirst ? ' ' : '') . __::upperFirst($word); |
163
|
1 |
|
}, |
164
|
1 |
|
'' |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Converts string, as a whole, to lower case just like strtolower. |
170
|
|
|
* |
171
|
|
|
* @usage __::toLower('fooBar'); |
172
|
|
|
* >> 'foobar' |
173
|
|
|
* |
174
|
|
|
* @param string $input |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
6 |
|
public static function toLower(string $input): string |
179
|
|
|
{ |
180
|
6 |
|
return strtolower($input); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Converts string, as a whole, to lower case just like strtoupper. |
185
|
|
|
* |
186
|
|
|
* @usage __::toUpper('fooBar'); |
187
|
|
|
* >> 'FOOBAR' |
188
|
|
|
* |
189
|
|
|
* @param string $input |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
2 |
|
public static function toUpper(string $input): string |
194
|
|
|
{ |
195
|
2 |
|
return strtoupper($input); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Converts string, as space separated words, to upper case. |
200
|
|
|
* |
201
|
|
|
* @usage __::upperCase('--foo-bar'); |
202
|
|
|
* >> 'FOO BAR' |
203
|
|
|
* |
204
|
|
|
* @param string $input |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
1 |
|
public static function upperCase(string $input): string |
209
|
|
|
{ |
210
|
1 |
|
$words = __::words(preg_replace("/['\x{2019}]/u", '', $input)); |
211
|
|
|
|
212
|
1 |
|
return array_reduce( |
213
|
1 |
|
$words, |
214
|
|
|
function ($result, $word) use ($words) { |
215
|
1 |
|
$isFirst = __::first($words) === $word; |
216
|
|
|
|
217
|
1 |
|
return $result . (!$isFirst ? ' ' : '') . __::toUpper($word); |
218
|
1 |
|
}, |
219
|
1 |
|
'' |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Converts the first character of string to upper case, like ucfirst. |
225
|
|
|
* |
226
|
|
|
* @usage __::upperFirst('fred'); |
227
|
|
|
* >> 'Fred' |
228
|
|
|
* |
229
|
|
|
* @param string $input |
230
|
|
|
* |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
4 |
|
public static function upperFirst(string $input): string |
234
|
|
|
{ |
235
|
4 |
|
return ucfirst($input); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Splits string into an array of its words. |
240
|
|
|
* |
241
|
|
|
* @usage __::words('fred, barney, & pebbles'); |
242
|
|
|
* >> ['fred', 'barney', 'pebbles'] |
243
|
|
|
* |
244
|
|
|
* __::words('fred, barney, & pebbles', '/[^, ]+/'); |
245
|
|
|
* >> ['fred', 'barney', '&', 'pebbles'] |
246
|
|
|
* |
247
|
|
|
* @param string|null $input |
248
|
|
|
* @param string $pattern : The pattern to match words. |
249
|
|
|
* |
250
|
|
|
* @return array |
251
|
|
|
*/ |
252
|
7 |
|
public static function words(?string $input, $pattern = null) |
253
|
|
|
{ |
254
|
|
|
/** Used to compose unicode character classes. */ |
255
|
7 |
|
$rsAstralRange = '\x{e800}-\x{efff}'; |
256
|
7 |
|
$rsComboMarksRange = '\x{0300}-\x{036f}'; |
257
|
7 |
|
$reComboHalfMarksRange = '\x{fe20}-\x{fe2f}'; |
258
|
7 |
|
$rsComboSymbolsRange = '\x{20d0}-\x{20ff}'; |
259
|
7 |
|
$rsComboRange = $rsComboMarksRange . $reComboHalfMarksRange . $rsComboSymbolsRange; |
260
|
7 |
|
$rsDingbatRange = '\x{2700}-\x{27bf}'; |
261
|
7 |
|
$rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff'; |
262
|
7 |
|
$rsMathOpRange = '\\xac\\xb1\\xd7\\xf7'; |
263
|
7 |
|
$rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf'; |
264
|
7 |
|
$rsPunctuationRange = '\x{2000}-\x{206f}'; |
265
|
7 |
|
$rsSpaceRange = ' \\t\\x0b\\f\\xa0\x{feff}\\n\\r\x{2028}\x{2029}\x{1680}\x{180e}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200a}\x{202f}\x{205f}\x{3000}'; |
266
|
7 |
|
$rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde'; |
267
|
7 |
|
$rsVarRange = '\x{fe0e}\x{fe0f}'; |
268
|
7 |
|
$rsBreakRange = $rsMathOpRange . $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange; |
269
|
|
|
/** Used to compose unicode capture groups. */ |
270
|
7 |
|
$rsApos = "['\x{2019}]"; |
271
|
7 |
|
$rsBreak = '[' . $rsBreakRange . ']'; |
272
|
7 |
|
$rsCombo = '[' . $rsComboRange . ']'; |
273
|
7 |
|
$rsDigits = '\\d+'; |
274
|
7 |
|
$rsDingbat = '[' . $rsDingbatRange . ']'; |
275
|
7 |
|
$rsLower = '[' . $rsLowerRange . ']'; |
276
|
7 |
|
$rsMisc = '[^' . $rsAstralRange . $rsBreakRange . $rsDigits . $rsDingbatRange . $rsLowerRange . $rsUpperRange . ']'; |
277
|
7 |
|
$rsFitz = '\\x{e83c}[\x{effb}-\x{efff}]'; |
278
|
7 |
|
$rsModifier = '(?:' . $rsCombo . '|' . $rsFitz . ')'; |
279
|
7 |
|
$rsNonAstral = '[^' . $rsAstralRange . ']'; |
280
|
7 |
|
$rsRegional = '(?:\x{e83c}[\x{ede6}-\x{edff}]){2}'; |
281
|
7 |
|
$rsSurrPair = '[\x{e800}-\x{ebff}][\x{ec00}-\x{efff}]'; |
282
|
7 |
|
$rsUpper = '[' . $rsUpperRange . ']'; |
283
|
7 |
|
$rsZWJ = '\x{200d}'; |
284
|
|
|
/** Used to compose unicode regexes. */ |
285
|
7 |
|
$rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')'; |
286
|
7 |
|
$rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')'; |
287
|
7 |
|
$rsOptContrLower = '(?:' . $rsApos . '(?:d|ll|m|re|s|t|ve))?'; |
288
|
7 |
|
$rsOptContrUpper = '(?:' . $rsApos . '(?:D|LL|M|RE|S|T|VE))?'; |
289
|
7 |
|
$reOptMod = $rsModifier . '?'; |
290
|
7 |
|
$rsOptVar = '[' . $rsVarRange . ']?'; |
291
|
7 |
|
$rsOrdLower = '\\d*(?:(?:1st|2nd|3rd|(?![123])\\dth)\\b)'; |
292
|
7 |
|
$rsOrdUpper = '\\d*(?:(?:1ST|2ND|3RD|(?![123])\\dTH)\\b)'; |
293
|
7 |
|
$asciiWords = '/[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/'; |
294
|
7 |
|
$hasUnicodeWordRegex = '/[a-z][A-Z]|[A-Z]{2,}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/'; |
295
|
7 |
|
$rsOptJoin = '(?:' . $rsZWJ . '(?:' . join( |
296
|
7 |
|
'|', |
297
|
7 |
|
[$rsNonAstral, $rsRegional, $rsSurrPair] |
298
|
7 |
|
) . ')' . $rsOptVar . $reOptMod . ')*'; |
299
|
7 |
|
$rsSeq = $rsOptVar . $reOptMod . $rsOptJoin; |
300
|
7 |
|
$rsEmoji = '(?:' . join('|', [$rsDingbat, $rsRegional, $rsSurrPair]) . ')' . $rsSeq; |
301
|
|
|
|
302
|
|
|
/** @var string $unicodeWords unicode words patterns to be used in preg_match */ |
303
|
7 |
|
$unicodeWords = '/' . join('|', [ |
304
|
7 |
|
$rsUpper . '?' . $rsLower . '+' . $rsOptContrLower . '(?=' . join( |
305
|
7 |
|
'|', |
306
|
7 |
|
[$rsBreak, $rsUpper, '$'] |
307
|
7 |
|
) . ')', |
308
|
7 |
|
$rsMiscUpper . '+' . $rsOptContrUpper . '(?=' . join( |
309
|
7 |
|
'|', |
310
|
7 |
|
[$rsBreak, $rsUpper . $rsMiscLower, '$'] |
311
|
7 |
|
) . ')', |
312
|
7 |
|
$rsUpper . '?' . $rsMiscLower . '+' . $rsOptContrLower, |
313
|
7 |
|
$rsUpper . '+' . $rsOptContrUpper, |
314
|
7 |
|
$rsOrdUpper, |
315
|
7 |
|
$rsOrdLower, |
316
|
7 |
|
$rsDigits, |
317
|
7 |
|
$rsEmoji, |
318
|
7 |
|
]) . '/u'; |
319
|
7 |
|
if ($pattern === null) { |
320
|
7 |
|
$hasUnicodeWord = preg_match($hasUnicodeWordRegex, $input); |
321
|
7 |
|
$pattern = $hasUnicodeWord ? $unicodeWords : $asciiWords; |
322
|
|
|
} |
323
|
7 |
|
$r = preg_match_all($pattern, $input, $matches, PREG_PATTERN_ORDER); |
324
|
7 |
|
if ($r === false) { |
325
|
|
|
throw new \RuntimeException('Regex exception'); |
326
|
|
|
} |
327
|
|
|
|
328
|
7 |
|
return count($matches[0]) > 0 ? $matches[0] : []; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Converts string, as space separated words, to lower case. |
333
|
|
|
* |
334
|
|
|
* @usage __::lowerCase('--Foo-Bar--'); |
335
|
|
|
* >> 'foo bar' |
336
|
|
|
* |
337
|
|
|
* @param string $input |
338
|
|
|
* |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
1 |
|
public static function lowerCase(string $input): string |
342
|
|
|
{ |
343
|
1 |
|
$words = __::words(preg_replace("/['\x{2019}]/u", '', $input)); |
344
|
|
|
|
345
|
1 |
|
return array_reduce( |
346
|
1 |
|
$words, |
347
|
|
|
function ($result, $word) use ($words) { |
348
|
1 |
|
$isFirst = __::first($words) === $word; |
349
|
|
|
|
350
|
1 |
|
return $result . (!$isFirst ? ' ' : '') . __::toLower($word); |
351
|
1 |
|
}, |
352
|
1 |
|
'' |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|