Skip to content

Commit 02b79c5

Browse files
committed
feat: add some new string uitl methods and tests
1 parent f12bd5e commit 02b79c5

File tree

4 files changed

+173
-29
lines changed

4 files changed

+173
-29
lines changed

src/Str/StringHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
*/
5656
abstract class StringHelper
5757
{
58+
// These words will be as a Boolean value
59+
public const TRUE_WORDS = '|on|yes|true|';
60+
public const FALSE_WORDS = '|off|no|false|';
61+
5862
public static $defaultEncoding = 'UTF-8';
5963

6064
use StringCaseHelperTrait;

src/Str/Traits/StringCheckHelperTrait.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Toolkit\Stdlib\Str\Traits;
1111

12-
use JetBrains\PhpStorm\Pure;
12+
use Toolkit\Stdlib\Str\StringHelper;
1313
use function function_exists;
1414
use function is_array;
1515
use function is_string;
@@ -31,9 +31,30 @@
3131
*/
3232
trait StringCheckHelperTrait
3333
{
34-
////////////////////////////////////////////////////////////////////////
35-
/// Check value
36-
////////////////////////////////////////////////////////////////////////
34+
/**
35+
* check is bool string. eg: 'true', 'false'
36+
*
37+
* @param string $str
38+
*
39+
* @return bool
40+
*/
41+
public static function isBool(string $str): bool
42+
{
43+
return false !== stripos(StringHelper::TRUE_WORDS, "|$str|")
44+
|| false !== stripos(StringHelper::FALSE_WORDS, "|$str|");
45+
}
46+
47+
/**
48+
* check is null string. eg: 'Null', 'null'
49+
*
50+
* @param string $str
51+
*
52+
* @return bool
53+
*/
54+
public static function isNull(string $str): bool
55+
{
56+
return false !== stripos('|null|', "|$str|");
57+
}
3758

3859
/**
3960
* @param string $string

src/Str/Traits/StringConvertTrait.php

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
namespace Toolkit\Stdlib\Str\Traits;
1111

1212
use Toolkit\Stdlib\Helper\DataHelper;
13+
use Toolkit\Stdlib\Str\StringHelper;
1314
use function array_filter;
1415
use function array_map;
1516
use function array_values;
1617
use function count;
1718
use function explode;
19+
use function is_bool;
1820
use function is_numeric;
1921
use function mb_convert_encoding;
2022
use function mb_convert_variables;
@@ -24,9 +26,9 @@
2426
use function str_contains;
2527
use function str_pad;
2628
use function str_split;
29+
use function stripos;
2730
use function strlen;
2831
use function strpos;
29-
use function substr;
3032
use function trim;
3133
use const PREG_SPLIT_NO_EMPTY;
3234

@@ -37,6 +39,25 @@
3739
*/
3840
trait StringConvertTrait
3941
{
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+
4061
/**
4162
* @param string $str
4263
*
@@ -47,14 +68,64 @@ public static function toBool(string $str): bool
4768
return DataHelper::boolean($str);
4869
}
4970

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+
50121
////////////////////////////////////////////////////////////////////////
51122
/// split to array
52123
////////////////////////////////////////////////////////////////////////
53124

54125
/**
55126
* @param string $string
56127
* @param string $delimiter
57-
* @param int $limit
128+
* @param int $limit
58129
*
59130
* @return array
60131
*/
@@ -66,7 +137,7 @@ public static function toInts(string $string, string $delimiter = ',', int $limi
66137
/**
67138
* @param string $str
68139
* @param string $delimiter
69-
* @param int $limit
140+
* @param int $limit
70141
*
71142
* @return array
72143
*/
@@ -91,7 +162,7 @@ public static function str2ints(string $str, string $delimiter = ',', int $limit
91162
*
92163
* @param string $str
93164
* @param string $separator
94-
* @param int $limit
165+
* @param int $limit
95166
*
96167
* @return array
97168
*/
@@ -106,7 +177,7 @@ public static function explode(string $str, string $separator = '.', int $limit
106177
*
107178
* @param string $str
108179
* @param string $sep
109-
* @param int $limit
180+
* @param int $limit
110181
*
111182
* @return array
112183
*/
@@ -134,7 +205,7 @@ public static function str2array(string $str, string $sep = ',', int $limit = 0)
134205
*
135206
* @param string $str
136207
* @param string $delimiter
137-
* @param int $limit
208+
* @param int $limit
138209
*
139210
* @return array
140211
*/
@@ -171,7 +242,7 @@ public static function toNoEmptyArray(string $str, string $sep = ',', int $limit
171242
/**
172243
* @param string $str
173244
* @param string $sep
174-
* @param int $limit
245+
* @param int $limit
175246
*
176247
* @return array
177248
*/
@@ -199,7 +270,7 @@ public static function toTrimmedArray(string $str, string $delimiter = ',', int
199270
*
200271
* @param string $str
201272
* @param string $delimiter
202-
* @param int $limit
273+
* @param int $limit
203274
*
204275
* @return array
205276
*/
@@ -218,43 +289,44 @@ public static function splitTrimmed(string $str, string $delimiter = ',', int $l
218289
/**
219290
* @param string $str
220291
* @param string $delimiter
292+
* @param int $intMaxLen
221293
*
222294
* @return array
223295
*/
224-
public static function toTypedArray(string $str, string $delimiter = ','): array
296+
public static function toTypedArray(string $str, string $delimiter = ',', int $intMaxLen = 11): array
225297
{
226-
return self::splitTypedList($str, $delimiter);
298+
return self::toTypedList($str, $delimiter, $intMaxLen);
227299
}
228300

229301
/**
230302
* @param string $str
231303
* @param string $delimiter
304+
* @param int $intMaxLen
232305
*
233306
* @return array
234307
*/
235-
public static function toTypedList(string $str, string $delimiter = ','): array
308+
public static function splitTypedList(string $str, string $delimiter = ',', int $intMaxLen = 11): array
236309
{
237-
return self::splitTypedList($str, $delimiter);
310+
return self::toTypedList($str, $delimiter, $intMaxLen);
238311
}
239312

240313
/**
241314
* @param string $str
242-
* @param string $delimiter
315+
* @param string $sep
316+
* @param int $intMaxLen
243317
*
244318
* @return array
245319
*/
246-
public static function splitTypedList(string $str, string $delimiter = ','): array
320+
public static function toTypedList(string $str, string $sep = ',', int $intMaxLen = 11): array
247321
{
248322
if (!$str) {
249323
return [];
250324
}
251325

252326
// $arr = self::splitTrimFiltered($str, $delimiter);
253-
$arr = self::toNoEmptyArray($str, $delimiter);
327+
$arr = self::toNoEmptyArray($str, $sep);
254328
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);
258330
}
259331

260332
return $arr;
@@ -265,7 +337,7 @@ public static function splitTypedList(string $str, string $delimiter = ','): arr
265337
*
266338
* @param string $str
267339
* @param string $delimiter
268-
* @param int $limit
340+
* @param int $limit
269341
*
270342
* @return array
271343
*/
@@ -280,13 +352,12 @@ public static function splitTrimFiltered(string $str, string $delimiter = ',', i
280352
}
281353

282354
$list = $limit < 1 ? explode($delimiter, $str) : explode($delimiter, $str, $limit);
283-
284355
return array_values(array_filter(array_map('trim', $list), 'strlen'));
285356
}
286357

287358
/**
288359
* @param string $string
289-
* @param int $width
360+
* @param int $width
290361
*
291362
* @return array
292363
*/
@@ -320,20 +391,19 @@ public static function splitByWidth(string $string, int $width = 1): array
320391
}
321392

322393
mb_convert_variables($encoding, 'utf8', $lines);
323-
324394
return $lines;
325395
}
326396

327397
/**
328398
* @param string $str
329-
* @param int $length
399+
* @param int $length
330400
*
331-
* @return array|string[]
401+
* @return string[]
332402
* @link https://www.php.net/manual/zh/function.str-split.php
333403
*/
334404
public static function splitUnicode(string $str, int $length = 1): array
335405
{
336-
if ($length > 0) {
406+
if ($length > 1) {
337407
$ret = [];
338408
$len = mb_strlen($str, 'UTF-8');
339409
for ($i = 0; $i < $len; $i += $length) {
@@ -345,4 +415,26 @@ public static function splitUnicode(string $str, int $length = 1): array
345415

346416
return preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
347417
}
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+
}
348440
}

test/Str/StringHelperTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ public function testBasicStrMethods(): void
2424
$this->assertEquals('', Str::wrap('', '"'));
2525
$this->assertEquals('"a"', Str::wrap('a', '"'));
2626
$this->assertEquals(['"a"', '"b"'], Str::wrapList(['a', 'b'], '"'));
27+
28+
$this->assertTrue(Str::isNull('null'));
29+
$this->assertFalse(Str::isNull('abc'));
30+
}
31+
32+
public function testIsBool(): void
33+
{
34+
$this->assertTrue(Str::isBool('true'));
35+
$this->assertTrue(Str::isBool('false'));
36+
$this->assertFalse(Str::isBool('abc'));
37+
}
38+
39+
public function testToTyped(): void
40+
{
41+
$tests = [
42+
['abc', 'abc'],
43+
['true', true],
44+
['23', 23],
45+
['23.4', 23.4],
46+
];
47+
foreach ($tests as [$in, $out]) {
48+
$this->assertEquals($out, Str::toTyped($in, true));
49+
}
50+
51+
$this->assertEquals('true', Str::toTyped('true'));
2752
}
2853

2954
public function testShellQuote(): void
@@ -148,10 +173,12 @@ public function testSplitTypedList(): void
148173
$tests = [
149174
['34,56,678, 678, 89, ', [34, 56, 678, 678, 89]],
150175
['a,,34, 3.4 ', ['a', 34, 3.4]],
176+
['ab,,true ', ['ab', true]],
151177
];
152178

153179
foreach ($tests as [$given, $want]) {
154180
$this->assertEquals($want, Str::splitTypedList($given));
181+
$this->assertEquals($want, Str::toTypedArray($given));
155182
}
156183
}
157184

0 commit comments

Comments
 (0)