Skip to content

Commit a5a3660

Browse files
committed
feat: add method Str::toNoEmptyArray instead splitTrimFiltered
1 parent 14da44a commit a5a3660

File tree

4 files changed

+133
-36
lines changed

4 files changed

+133
-36
lines changed

src/OS.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,31 @@ public static function isRootUser(): bool
124124
return getmyuid() === 0;
125125
}
126126

127+
/**
128+
* @return string
129+
*/
130+
public static function getUserName(): string
131+
{
132+
if (isset($_SERVER['USER'])) {
133+
return $_SERVER['USER'];
134+
}
135+
136+
if (isset($_ENV['USER'])) {
137+
return $_ENV['USER'];
138+
}
139+
140+
$user = self::getCurrentUser();
141+
return $user['name'] ?? '';
142+
}
143+
127144
/**
128145
* Get unix user of current process.
129146
*
130-
* @return array
147+
* @return array{name:string, uid: int, gid: int, dir: string, shell: string}
131148
*/
132149
public static function getCurrentUser(): array
133150
{
134-
return posix_getpwuid(posix_getuid());
151+
return (array)posix_getpwuid(posix_getuid());
135152
}
136153

137154
/**************************************************************************

src/Str/Traits/StringCaseHelperTrait.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static function toLower($str): string
7272
*/
7373
public static function strtolower(string $str): string
7474
{
75-
if (!is_scalar($str)) {
75+
if (!$str || !is_scalar($str)) {
7676
return '';
7777
}
7878

@@ -104,7 +104,7 @@ public static function upper($str): string
104104
*/
105105
public static function toUpper($str): string
106106
{
107-
if (!is_scalar($str)) {
107+
if (!$str || !is_scalar($str)) {
108108
return '';
109109
}
110110

@@ -126,22 +126,21 @@ public static function strtoupper($str): string
126126
}
127127

128128
/**
129-
* @param $str
129+
* @param string|int $str
130130
*
131131
* @return string
132132
*/
133133
public static function upFirst($str): string
134134
{
135-
136-
if (!is_scalar($str)) {
135+
if (!$str || !is_scalar($str)) {
137136
return '';
138137
}
139138

140139
if (!is_string($str)) {
141140
return (string)$str;
142141
}
143142

144-
return self::strtoupper(Str::substr($str, 0, 1)) . Str::substr($str, 1);
143+
return self::toUpper(Str::substr($str, 0, 1)) . Str::substr($str, 1);
145144
}
146145

147146
/**
@@ -222,7 +221,7 @@ public static function camelCase(string $name, bool $upFirst = false, string $se
222221
*/
223222
public static function toCamelCase(string $str, bool $upperFirst = false): string
224223
{
225-
$str = self::strtolower($str);
224+
$str = self::toLower($str);
226225

227226
if ($upperFirst) {
228227
$str = self::ucfirst($str);

src/Str/Traits/StringConvertTrait.php

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,35 +71,66 @@ public static function toInts(string $string, string $delimiter = ',', int $limi
7171
*/
7272
public static function str2ints(string $str, string $delimiter = ',', int $limit = 0): array
7373
{
74-
$values = self::splitTrimFiltered($str, $delimiter, $limit);
74+
$intArr = [];
75+
// $values = self::splitTrimFiltered($str, $delimiter, $limit);
76+
$values = self::toNoEmptyArray($str, $delimiter, $limit);
7577

76-
return array_map('intval', $values);
78+
foreach ($values as $value) {
79+
if (is_numeric($value)) {
80+
$intArr[] = (int)$value;
81+
}
82+
}
83+
84+
return $intArr;
7785
}
7886

7987
/**
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()
94+
*
8095
* @param string $str
81-
* @param string $delimiter
82-
* @param int $limit
96+
* @param string $sep
97+
* @param int $limit
8398
*
8499
* @return array
85100
*/
86-
public static function toArray(string $str, string $delimiter = ',', int $limit = 0): array
101+
public static function toNoEmptyArray(string $str, string $sep = ',', int $limit = -1): array
87102
{
88-
return self::splitTrimFiltered($str, $delimiter, $limit);;
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);
89110
}
90111

91112
/**
92-
* Like explode, but will trim each item and filter empty item.
113+
* @param string $str
114+
* @param string $sep
115+
* @param int $limit
93116
*
117+
* @return array
118+
*/
119+
public static function splitNoEmptyArray(string $str, string $sep = ',', int $limit = 0): array
120+
{
121+
return self::toNoEmptyArray($str, $sep, $limit);;
122+
}
123+
124+
/**
94125
* @param string $str
95-
* @param string $separator
126+
* @param string $sep
96127
* @param int $limit
97128
*
98129
* @return array
99130
*/
100-
public static function explode(string $str, string $separator = '.', int $limit = 0): array
131+
public static function toArray(string $str, string $sep = ',', int $limit = 0): array
101132
{
102-
return self::splitTrimFiltered($str, $separator, $limit);
133+
return self::toNoEmptyArray($str, $sep, $limit);;
103134
}
104135

105136
/**
@@ -113,26 +144,22 @@ public static function explode(string $str, string $separator = '.', int $limit
113144
*/
114145
public static function str2array(string $str, string $sep = ',', int $limit = 0): array
115146
{
116-
// $str = trim($str, "$sep ");
117-
// if (!$str) {
118-
// return [];
119-
// }
120-
// return preg_split("/\s*$sep\s*/", $str, -1, PREG_SPLIT_NO_EMPTY);
121-
return self::splitTrimFiltered($str, $sep, $limit);
147+
return self::toNoEmptyArray($str, $sep, $limit);
122148
}
123149

124150
/**
125151
* Like explode, but will trim each item and filter empty item.
126152
*
127153
* @param string $str
128-
* @param string $delimiter
154+
* @param string $separator
129155
* @param int $limit
130156
*
131157
* @return array
132158
*/
133-
public static function split2Array(string $str, string $delimiter = ',', int $limit = 0): array
159+
public static function explode(string $str, string $separator = '.', int $limit = 0): array
134160
{
135-
return self::splitTrimFiltered($str, $delimiter, $limit);
161+
return self::toNoEmptyArray($str, $separator, $limit);
162+
// return self::splitTrimFiltered($str, $separator, $limit);
136163
}
137164

138165
/**
@@ -144,9 +171,9 @@ public static function split2Array(string $str, string $delimiter = ',', int $li
144171
*
145172
* @return array
146173
*/
147-
public static function toTrimFilteredArray(string $str, string $delimiter = ',', int $limit = 0): array
174+
public static function split2Array(string $str, string $delimiter = ',', int $limit = 0): array
148175
{
149-
return self::splitTrimFiltered($str, $delimiter, $limit);
176+
return self::toNoEmptyArray($str, $delimiter, $limit);
150177
}
151178

152179
/**
@@ -242,7 +269,8 @@ public static function splitTypedList(string $str, string $delimiter = ','): arr
242269
return [];
243270
}
244271

245-
$arr = self::splitTrimFiltered($str, $delimiter);
272+
// $arr = self::splitTrimFiltered($str, $delimiter);
273+
$arr = self::toNoEmptyArray($str, $delimiter);
246274
foreach ($arr as &$val) {
247275
if (is_numeric($val) && strlen($val) < 11) {
248276
$val = str_contains($val, '.') ? (float)$val : (int)$val;

test/Str/StringHelperTest.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,74 @@ public function testStrpos(): void
7979
self::assertFalse(Str::notContains('abc', 'b'));
8080
}
8181

82-
public function testToArray(): void
82+
public function testToArray_no_limit(): void
8383
{
8484
$tests = [
85-
['34,56,678, 678, 89, ', ['34', '56' , '678', '678', '89']],
85+
['34,56,678, 678, 89, ', ',', ['34', '56', '678', '678', '89']],
86+
[' fieldName some desc', ' ', ['fieldName', 'some', 'desc']],
87+
[' ab 0 cd ', ' ', ['ab', '0', 'cd']],
8688
];
8789

88-
foreach ($tests as [$given, $want]) {
89-
$this->assertEquals($want, Str::toArray($given));
90+
foreach ($tests as [$given, $sep, $want]) {
91+
$this->assertEquals($want, Str::toNoEmptyArray($given, $sep));
92+
$this->assertEquals($want, Str::splitTrimFiltered($given, $sep));
93+
}
94+
}
95+
96+
public function testToArray_limit(): void
97+
{
98+
$tests = [
99+
[
100+
' fieldName some desc msg ',
101+
' ',
102+
2,
103+
['fieldName', 'some desc msg']
104+
],
105+
[
106+
' ab 0 cd ',
107+
' ',
108+
2,
109+
['ab', '0 cd']
110+
],
111+
];
112+
113+
foreach ($tests as [$given, $sep, $limit, $want]) {
114+
$this->assertEquals($want, Str::splitTrimFiltered($given, $sep, $limit));
115+
$this->assertEquals($want, Str::toNoEmptyArray($given, $sep, $limit));
90116
}
91117
}
92118

119+
/**
120+
* TIP: recommend use Str::toNoEmptyArray()
121+
*/
122+
public function testDiff_splitTrimFiltered_toNoEmptyArray(): void
123+
{
124+
$str = ' fieldName,, some, desc, msg ';
125+
$this->assertEquals(
126+
['fieldName', 'some', 'desc, msg'], // better
127+
Str::toNoEmptyArray($str, ',', 3)
128+
);
129+
$this->assertEquals(
130+
['fieldName', 'some, desc, msg'], // only two elem
131+
Str::splitTrimFiltered($str, ',', 3)
132+
);
133+
134+
$sep = ' ';
135+
$str = ' fieldName some desc msg ';
136+
$this->assertEquals(
137+
['fieldName', 'some', 'desc msg'], // better
138+
Str::toNoEmptyArray($str, $sep, 3)
139+
);
140+
$this->assertEquals(
141+
['fieldName', 'some desc msg'], // only two elem
142+
Str::splitTrimFiltered($str, $sep, 3)
143+
);
144+
}
145+
93146
public function testSplitTypedList(): void
94147
{
95148
$tests = [
96-
['34,56,678, 678, 89, ', [34,56,678, 678, 89]],
149+
['34,56,678, 678, 89, ', [34, 56, 678, 678, 89]],
97150
['a,,34, 3.4 ', ['a', 34, 3.4]],
98151
];
99152

0 commit comments

Comments
 (0)