Skip to content

Commit 90022b2

Browse files
committed
fix composer.json format error
1 parent d7bd02a commit 90022b2

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

libs/str-utils/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
}
2828
},
2929
"suggest": {
30-
"inhere/php-validate": "Very lightweight data validate tool",
30+
"inhere/php-validate": "Very lightweight data validate tool"
3131
}
3232
}

libs/str-utils/src/StringHelper.php

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -468,28 +468,6 @@ public static function nameChange(string $str, bool $toCamelCase = true): string
468468
/// Convert to array
469469
////////////////////////////////////////////////////////////////////////
470470

471-
/**
472-
* @param string $str
473-
* @param string $sep
474-
* @return array
475-
*/
476-
public static function toArray(string $str, string $sep = ','): array
477-
{
478-
$array = [];
479-
480-
if (\is_string($str)) {
481-
$str = \trim($str, "$sep ");
482-
483-
if (!$str) {
484-
return [];
485-
}
486-
487-
$array = \strpos($str, $sep) !== false ? \array_map('trim', \explode($sep, $str)) : [$str];
488-
}
489-
490-
return $array;
491-
}
492-
493471
/**
494472
* var_dump(str2array('34,56,678, 678, 89, '));
495473
* @param string $str
@@ -507,19 +485,51 @@ public static function str2array(string $str, string $sep = ','): array
507485
return \preg_split("/\s*$sep\s*/", $str, -1, \PREG_SPLIT_NO_EMPTY);
508486
}
509487

510-
public static function explode(string $str, string $separator = '.'): array
488+
public static function toArray(string $string, string $delimiter = ',', int $limit = 0): array
489+
{
490+
$string = \trim($string, "$delimiter ");
491+
if ($string === '') {
492+
return [];
493+
}
494+
495+
$values = [];
496+
$rawList = $limit < 1 ? \explode($delimiter, $string) : \explode($delimiter, $string, $limit);
497+
498+
foreach ($rawList as $val) {
499+
if (($val = \trim($val)) !== '') {
500+
$values[] = $val;
501+
}
502+
}
503+
504+
return $values;
505+
}
506+
507+
public static function explode(string $str, string $separator = '.', int $limit = 0): array
511508
{
512-
return static::split2Array($str, $separator);
509+
return static::split2Array($str, $separator, $limit);
513510
}
514511

515512
/**
516-
* @param string $str
517-
* @param string $separator
518-
* @return array
513+
* @param string $string
514+
* @param string $delimiter
515+
* @param int $limit
516+
* @return array
519517
*/
520-
public static function split2Array(string $str, string $separator = '.'): array
518+
public static function split2Array(string $string, string $delimiter = ',', int $limit = 0): array
521519
{
522-
return \array_values(\array_filter(\explode($separator, $str), '\trim'));
520+
$string = \trim($string, "$delimiter ");
521+
522+
if (!\strpos($string, $delimiter)) {
523+
return [$string];
524+
}
525+
526+
if ($limit < 1) {
527+
$list = \explode($delimiter, $string);
528+
} else {
529+
$list = \explode($delimiter, $string, $limit);
530+
}
531+
532+
return \array_values(array_filter(\array_map('trim', $list), 'strlen'));
523533
}
524534

525535
/**

0 commit comments

Comments
 (0)