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