10
10
namespace Toolkit \Stdlib \Str \Traits ;
11
11
12
12
use Toolkit \Stdlib \Helper \DataHelper ;
13
+ use Toolkit \Stdlib \Str \StringHelper ;
13
14
use function array_filter ;
14
15
use function array_map ;
15
16
use function array_values ;
16
17
use function count ;
17
18
use function explode ;
19
+ use function is_bool ;
18
20
use function is_numeric ;
19
21
use function mb_convert_encoding ;
20
22
use function mb_convert_variables ;
24
26
use function str_contains ;
25
27
use function str_pad ;
26
28
use function str_split ;
29
+ use function stripos ;
27
30
use function strlen ;
28
31
use function strpos ;
29
- use function substr ;
30
32
use function trim ;
31
33
use const PREG_SPLIT_NO_EMPTY ;
32
34
37
39
*/
38
40
trait StringConvertTrait
39
41
{
42
+ /**
43
+ * @param string $val
44
+ *
45
+ * @return bool|string
46
+ */
47
+ public static function tryToBool (string $ val )
48
+ {
49
+ // check it is a bool value.
50
+ if (false !== stripos (StringHelper::TRUE_WORDS , "| $ val| " )) {
51
+ return true ;
52
+ }
53
+
54
+ if (false !== stripos (StringHelper::FALSE_WORDS , "| $ val| " )) {
55
+ return false ;
56
+ }
57
+
58
+ return $ val ;
59
+ }
60
+
40
61
/**
41
62
* @param string $str
42
63
*
@@ -47,14 +68,64 @@ public static function toBool(string $str): bool
47
68
return DataHelper::boolean ($ str );
48
69
}
49
70
71
+ /**
72
+ * @param string $val
73
+ *
74
+ * @return bool
75
+ */
76
+ public static function toBool2 (string $ val ): bool
77
+ {
78
+ // check it is a bool value.
79
+ return false !== stripos (StringHelper::TRUE_WORDS , "| $ val| " );
80
+ }
81
+
82
+ /**
83
+ * @param string $str
84
+ *
85
+ * @return bool
86
+ */
87
+ public static function toBoolTrue (string $ str ): bool
88
+ {
89
+ return DataHelper::boolean ($ str );
90
+ }
91
+
92
+ /**
93
+ * auto convert string to typed value
94
+ *
95
+ * @param string $str
96
+ * @param bool $parseBool
97
+ * @param int $intMaxLen
98
+ *
99
+ * @return float|int|string|bool
100
+ */
101
+ public static function toTyped (string $ str , bool $ parseBool = false , int $ intMaxLen = 11 )
102
+ {
103
+ if (is_numeric ($ str ) && strlen ($ str ) <= $ intMaxLen ) {
104
+ if (str_contains ($ str , '. ' )) {
105
+ $ val = (float )$ str ;
106
+ } else {
107
+ $ val = (int )$ str ;
108
+ }
109
+
110
+ return $ val ;
111
+ }
112
+
113
+ // parse bool: true, false
114
+ if ($ parseBool && strlen ($ str ) < 6 ) {
115
+ return self ::tryToBool ($ str );
116
+ }
117
+
118
+ return $ str ;
119
+ }
120
+
50
121
////////////////////////////////////////////////////////////////////////
51
122
/// split to array
52
123
////////////////////////////////////////////////////////////////////////
53
124
54
125
/**
55
126
* @param string $string
56
127
* @param string $delimiter
57
- * @param int $limit
128
+ * @param int $limit
58
129
*
59
130
* @return array
60
131
*/
@@ -66,7 +137,7 @@ public static function toInts(string $string, string $delimiter = ',', int $limi
66
137
/**
67
138
* @param string $str
68
139
* @param string $delimiter
69
- * @param int $limit
140
+ * @param int $limit
70
141
*
71
142
* @return array
72
143
*/
@@ -91,7 +162,7 @@ public static function str2ints(string $str, string $delimiter = ',', int $limit
91
162
*
92
163
* @param string $str
93
164
* @param string $separator
94
- * @param int $limit
165
+ * @param int $limit
95
166
*
96
167
* @return array
97
168
*/
@@ -106,7 +177,7 @@ public static function explode(string $str, string $separator = '.', int $limit
106
177
*
107
178
* @param string $str
108
179
* @param string $sep
109
- * @param int $limit
180
+ * @param int $limit
110
181
*
111
182
* @return array
112
183
*/
@@ -134,7 +205,7 @@ public static function str2array(string $str, string $sep = ',', int $limit = 0)
134
205
*
135
206
* @param string $str
136
207
* @param string $delimiter
137
- * @param int $limit
208
+ * @param int $limit
138
209
*
139
210
* @return array
140
211
*/
@@ -171,7 +242,7 @@ public static function toNoEmptyArray(string $str, string $sep = ',', int $limit
171
242
/**
172
243
* @param string $str
173
244
* @param string $sep
174
- * @param int $limit
245
+ * @param int $limit
175
246
*
176
247
* @return array
177
248
*/
@@ -199,7 +270,7 @@ public static function toTrimmedArray(string $str, string $delimiter = ',', int
199
270
*
200
271
* @param string $str
201
272
* @param string $delimiter
202
- * @param int $limit
273
+ * @param int $limit
203
274
*
204
275
* @return array
205
276
*/
@@ -218,43 +289,44 @@ public static function splitTrimmed(string $str, string $delimiter = ',', int $l
218
289
/**
219
290
* @param string $str
220
291
* @param string $delimiter
292
+ * @param int $intMaxLen
221
293
*
222
294
* @return array
223
295
*/
224
- public static function toTypedArray (string $ str , string $ delimiter = ', ' ): array
296
+ public static function toTypedArray (string $ str , string $ delimiter = ', ' , int $ intMaxLen = 11 ): array
225
297
{
226
- return self ::splitTypedList ($ str , $ delimiter );
298
+ return self ::toTypedList ($ str , $ delimiter, $ intMaxLen );
227
299
}
228
300
229
301
/**
230
302
* @param string $str
231
303
* @param string $delimiter
304
+ * @param int $intMaxLen
232
305
*
233
306
* @return array
234
307
*/
235
- public static function toTypedList (string $ str , string $ delimiter = ', ' ): array
308
+ public static function splitTypedList (string $ str , string $ delimiter = ', ' , int $ intMaxLen = 11 ): array
236
309
{
237
- return self ::splitTypedList ($ str , $ delimiter );
310
+ return self ::toTypedList ($ str , $ delimiter, $ intMaxLen );
238
311
}
239
312
240
313
/**
241
314
* @param string $str
242
- * @param string $delimiter
315
+ * @param string $sep
316
+ * @param int $intMaxLen
243
317
*
244
318
* @return array
245
319
*/
246
- public static function splitTypedList (string $ str , string $ delimiter = ', ' ): array
320
+ public static function toTypedList (string $ str , string $ sep = ', ' , int $ intMaxLen = 11 ): array
247
321
{
248
322
if (!$ str ) {
249
323
return [];
250
324
}
251
325
252
326
// $arr = self::splitTrimFiltered($str, $delimiter);
253
- $ arr = self ::toNoEmptyArray ($ str , $ delimiter );
327
+ $ arr = self ::toNoEmptyArray ($ str , $ sep );
254
328
foreach ($ arr as &$ val ) {
255
- if (is_numeric ($ val ) && strlen ($ val ) < 11 ) {
256
- $ val = str_contains ($ val , '. ' ) ? (float )$ val : (int )$ val ;
257
- }
329
+ $ val = self ::toTyped ($ val , true , $ intMaxLen );
258
330
}
259
331
260
332
return $ arr ;
@@ -265,7 +337,7 @@ public static function splitTypedList(string $str, string $delimiter = ','): arr
265
337
*
266
338
* @param string $str
267
339
* @param string $delimiter
268
- * @param int $limit
340
+ * @param int $limit
269
341
*
270
342
* @return array
271
343
*/
@@ -280,13 +352,12 @@ public static function splitTrimFiltered(string $str, string $delimiter = ',', i
280
352
}
281
353
282
354
$ list = $ limit < 1 ? explode ($ delimiter , $ str ) : explode ($ delimiter , $ str , $ limit );
283
-
284
355
return array_values (array_filter (array_map ('trim ' , $ list ), 'strlen ' ));
285
356
}
286
357
287
358
/**
288
359
* @param string $string
289
- * @param int $width
360
+ * @param int $width
290
361
*
291
362
* @return array
292
363
*/
@@ -320,20 +391,19 @@ public static function splitByWidth(string $string, int $width = 1): array
320
391
}
321
392
322
393
mb_convert_variables ($ encoding , 'utf8 ' , $ lines );
323
-
324
394
return $ lines ;
325
395
}
326
396
327
397
/**
328
398
* @param string $str
329
- * @param int $length
399
+ * @param int $length
330
400
*
331
- * @return array| string[]
401
+ * @return string[]
332
402
* @link https://www.php.net/manual/zh/function.str-split.php
333
403
*/
334
404
public static function splitUnicode (string $ str , int $ length = 1 ): array
335
405
{
336
- if ($ length > 0 ) {
406
+ if ($ length > 1 ) {
337
407
$ ret = [];
338
408
$ len = mb_strlen ($ str , 'UTF-8 ' );
339
409
for ($ i = 0 ; $ i < $ len ; $ i += $ length ) {
@@ -345,4 +415,26 @@ public static function splitUnicode(string $str, int $length = 1): array
345
415
346
416
return preg_split ('//u ' , $ str , -1 , PREG_SPLIT_NO_EMPTY );
347
417
}
418
+
419
+ /**
420
+ * @param string $str
421
+ * @param int $length
422
+ *
423
+ * @return string[]
424
+ * @link https://www.php.net/manual/zh/function.str-split.php
425
+ */
426
+ public static function splitUnicode2 (string $ str , int $ length = 1 ): array
427
+ {
428
+ $ tmp = preg_split ('~~u ' , $ str , -1 , PREG_SPLIT_NO_EMPTY );
429
+
430
+ if ($ length > 1 ) {
431
+ $ chunks = array_chunk ($ tmp , $ length );
432
+ foreach ($ chunks as $ i => $ chunk ) {
433
+ $ chunks [$ i ] = implode ('' , (array )$ chunk );
434
+ }
435
+ $ tmp = $ chunks ;
436
+ }
437
+
438
+ return $ tmp ;
439
+ }
348
440
}
0 commit comments