Skip to content

Commit f12bd5e

Browse files
committed
fix: fix str util unit tests error
1 parent a5a3660 commit f12bd5e

File tree

2 files changed

+59
-54
lines changed

2 files changed

+59
-54
lines changed

src/Str/StringHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public static function wrapList(array $list, string $wrapChar): array
341341
{
342342
$new = [];
343343
foreach ($list as $val) {
344-
$new = self::wrap($val, $wrapChar);
344+
$new[] = self::wrap($val, $wrapChar);
345345
}
346346

347347
return $new;

src/Str/Traits/StringConvertTrait.php

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Toolkit\Stdlib\Str\Traits;
1111

1212
use Toolkit\Stdlib\Helper\DataHelper;
13+
use function array_filter;
1314
use function array_map;
1415
use function array_values;
1516
use function count;
@@ -85,43 +86,24 @@ public static function str2ints(string $str, string $delimiter = ',', int $limit
8586
}
8687

8788
/**
88-
* like explode, split string to no empty array
89-
*
90-
* - Difference toNoEmptyArray() and splitTrimFiltered().
91-
*
92-
* Please see {@see StringHelperTest::testDiff_splitTrimFiltered_toNoEmptyArray()}
93-
* So, recommend use toNoEmptyArray() instead splitTrimFiltered()
89+
* Like explode, but will trim each item and filter empty item.
90+
* - alias of toNoEmptyArray()
9491
*
9592
* @param string $str
96-
* @param string $sep
97-
* @param int $limit
98-
*
99-
* @return array
100-
*/
101-
public static function toNoEmptyArray(string $str, string $sep = ',', int $limit = -1): array
102-
{
103-
$str = trim($str, "$sep ");
104-
if (!$str) {
105-
return [];
106-
}
107-
108-
$pattern = $sep === ' ' ? '/\s+/' : "/\s*$sep\s*/";
109-
return preg_split($pattern, $str, $limit, PREG_SPLIT_NO_EMPTY);
110-
}
111-
112-
/**
113-
* @param string $str
114-
* @param string $sep
93+
* @param string $separator
11594
* @param int $limit
11695
*
11796
* @return array
11897
*/
119-
public static function splitNoEmptyArray(string $str, string $sep = ',', int $limit = 0): array
98+
public static function explode(string $str, string $separator = '.', int $limit = 0): array
12099
{
121-
return self::toNoEmptyArray($str, $sep, $limit);;
100+
return self::toNoEmptyArray($str, $separator, $limit);
101+
// return self::splitTrimFiltered($str, $separator, $limit);
122102
}
123103

124104
/**
105+
* alias of toNoEmptyArray()
106+
*
125107
* @param string $str
126108
* @param string $sep
127109
* @param int $limit
@@ -134,7 +116,7 @@ public static function toArray(string $str, string $sep = ',', int $limit = 0):
134116
}
135117

136118
/**
137-
* split to array.
119+
* alias of toNoEmptyArray()
138120
*
139121
* @param string $str
140122
* @param string $sep
@@ -148,56 +130,54 @@ public static function str2array(string $str, string $sep = ',', int $limit = 0)
148130
}
149131

150132
/**
151-
* Like explode, but will trim each item and filter empty item.
133+
* alias of toNoEmptyArray()
152134
*
153135
* @param string $str
154-
* @param string $separator
136+
* @param string $delimiter
155137
* @param int $limit
156138
*
157139
* @return array
158140
*/
159-
public static function explode(string $str, string $separator = '.', int $limit = 0): array
141+
public static function split2Array(string $str, string $delimiter = ',', int $limit = 0): array
160142
{
161-
return self::toNoEmptyArray($str, $separator, $limit);
162-
// return self::splitTrimFiltered($str, $separator, $limit);
143+
return self::toNoEmptyArray($str, $delimiter, $limit);
163144
}
164145

165146
/**
166-
* Like explode, but will trim each item and filter empty item.
147+
* like explode, split string to no empty array
148+
*
149+
* - Difference toNoEmptyArray() and splitTrimFiltered().
150+
*
151+
* Please see {@see StringHelperTest::testDiff_splitTrimFiltered_toNoEmptyArray()}
152+
* So, recommend use toNoEmptyArray() instead splitTrimFiltered()
167153
*
168154
* @param string $str
169-
* @param string $delimiter
170-
* @param int $limit
155+
* @param string $sep
156+
* @param int $limit
171157
*
172158
* @return array
173159
*/
174-
public static function split2Array(string $str, string $delimiter = ',', int $limit = 0): array
160+
public static function toNoEmptyArray(string $str, string $sep = ',', int $limit = -1): array
175161
{
176-
return self::toNoEmptyArray($str, $delimiter, $limit);
162+
$str = trim($str, "$sep ");
163+
if (!$str) {
164+
return [];
165+
}
166+
167+
$pattern = $sep === ' ' ? '/\s+/' : "/\s*$sep\s*/";
168+
return preg_split($pattern, $str, $limit, PREG_SPLIT_NO_EMPTY);
177169
}
178170

179171
/**
180-
* Like explode, but will trim each item and filter empty item.
181-
*
182172
* @param string $str
183-
* @param string $delimiter
173+
* @param string $sep
184174
* @param int $limit
185175
*
186176
* @return array
187177
*/
188-
public static function splitTrimFiltered(string $str, string $delimiter = ',', int $limit = 0): array
178+
public static function splitNoEmptyArray(string $str, string $sep = ',', int $limit = 0): array
189179
{
190-
if (!$str = trim($str)) {
191-
return [];
192-
}
193-
194-
if (!strpos($str, $delimiter)) {
195-
return [$str];
196-
}
197-
198-
$list = $limit < 1 ? explode($delimiter, $str) : explode($delimiter, $str, $limit);
199-
200-
return array_values(array_filter(array_map('trim', $list), 'strlen'));
180+
return self::toNoEmptyArray($str, $sep, $limit);;
201181
}
202182

203183
/**
@@ -279,6 +259,31 @@ public static function splitTypedList(string $str, string $delimiter = ','): arr
279259

280260
return $arr;
281261
}
262+
263+
/**
264+
* Like explode, but will trim each item and filter empty item.
265+
*
266+
* @param string $str
267+
* @param string $delimiter
268+
* @param int $limit
269+
*
270+
* @return array
271+
*/
272+
public static function splitTrimFiltered(string $str, string $delimiter = ',', int $limit = 0): array
273+
{
274+
if (!$str = trim($str)) {
275+
return [];
276+
}
277+
278+
if (!strpos($str, $delimiter)) {
279+
return [$str];
280+
}
281+
282+
$list = $limit < 1 ? explode($delimiter, $str) : explode($delimiter, $str, $limit);
283+
284+
return array_values(array_filter(array_map('trim', $list), 'strlen'));
285+
}
286+
282287
/**
283288
* @param string $string
284289
* @param int $width

0 commit comments

Comments
 (0)