Skip to content

Commit 14da44a

Browse files
committed
feat: add more str and url helper methods, add more tests
1 parent be542ca commit 14da44a

6 files changed

+137
-12
lines changed

src/Str/Traits/StringCaseHelperTrait.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public static function nameChange(string $str, bool $toCamelCase = true): string
286286

287287
// 默认 :下划线式 =>驼峰式
288288
if ($toCamelCase) {
289-
if (strpos($str, '_') === false) {
289+
if (!str_contains($str, '_')) {
290290
return $str;
291291
}
292292

@@ -304,6 +304,17 @@ public static function nameChange(string $str, bool $toCamelCase = true): string
304304
return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $str));
305305
}
306306

307+
/**
308+
* @param string $str
309+
* @param string $dstCase
310+
*
311+
* @return string
312+
*/
313+
public static function changeCase(string $str, string $dstCase = 'auto'): string
314+
{
315+
return $str;
316+
}
317+
307318
/**
308319
* Convert \n and \r\n and \r to <br />
309320
*

src/Str/Traits/StringCheckHelperTrait.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,25 @@ public static function strrpos(string $str, string $find, int $offset = 0, strin
255255
}
256256

257257
/**
258+
* Assert is start withs one
259+
*
258260
* @param string $str
259-
* @param string $needle
261+
* @param string|array $needle
260262
*
261263
* @return bool
262264
*/
263-
public static function isStartWiths(string $str, string $needle): bool
265+
public static function isStartWiths(string $str, $needle): bool
264266
{
265-
return self::hasPrefix($str, $needle);
267+
if (is_array($needle)) {
268+
foreach ($needle as $sub) {
269+
if (str_starts_with($str, $sub)) {
270+
return true;
271+
}
272+
}
273+
return false;
274+
}
275+
276+
return str_starts_with($str, $needle);
266277
}
267278

268279
/**
@@ -276,6 +287,17 @@ public static function hasPrefix(string $str, string $needle): bool
276287
return str_starts_with($str, $needle);
277288
}
278289

290+
/**
291+
* @param string $str
292+
* @param string[] $needles
293+
*
294+
* @return bool
295+
*/
296+
public static function hasPrefixes(string $str, array $needles): bool
297+
{
298+
return self::isStartWiths($str, $needles);
299+
}
300+
279301
/**
280302
* @param string $str
281303
* @param string $needle

src/Str/Traits/StringConvertTrait.php

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

1212
use Toolkit\Stdlib\Helper\DataHelper;
13-
use Toolkit\Stdlib\Str;
1413
use function array_map;
1514
use function array_values;
1615
use function count;
@@ -136,6 +135,20 @@ public static function split2Array(string $str, string $delimiter = ',', int $li
136135
return self::splitTrimFiltered($str, $delimiter, $limit);
137136
}
138137

138+
/**
139+
* Like explode, but will trim each item and filter empty item.
140+
*
141+
* @param string $str
142+
* @param string $delimiter
143+
* @param int $limit
144+
*
145+
* @return array
146+
*/
147+
public static function toTrimFilteredArray(string $str, string $delimiter = ',', int $limit = 0): array
148+
{
149+
return self::splitTrimFiltered($str, $delimiter, $limit);
150+
}
151+
139152
/**
140153
* Like explode, but will trim each item and filter empty item.
141154
*
@@ -160,6 +173,20 @@ public static function splitTrimFiltered(string $str, string $delimiter = ',', i
160173
return array_values(array_filter(array_map('trim', $list), 'strlen'));
161174
}
162175

176+
/**
177+
* Like explode, but will trim each item.
178+
*
179+
* @param string $str
180+
* @param string $delimiter
181+
* @param int $limit
182+
*
183+
* @return array
184+
*/
185+
public static function toTrimmedArray(string $str, string $delimiter = ',', int $limit = 0): array
186+
{
187+
return self::splitTrimmed($str, $delimiter, $limit);
188+
}
189+
163190
/**
164191
* Like explode, but will trim each item.
165192
*
@@ -181,6 +208,28 @@ public static function splitTrimmed(string $str, string $delimiter = ',', int $l
181208
return array_map('trim', $list);
182209
}
183210

211+
/**
212+
* @param string $str
213+
* @param string $delimiter
214+
*
215+
* @return array
216+
*/
217+
public static function toTypedArray(string $str, string $delimiter = ','): array
218+
{
219+
return self::splitTypedList($str, $delimiter);
220+
}
221+
222+
/**
223+
* @param string $str
224+
* @param string $delimiter
225+
*
226+
* @return array
227+
*/
228+
public static function toTypedList(string $str, string $delimiter = ','): array
229+
{
230+
return self::splitTypedList($str, $delimiter);
231+
}
232+
184233
/**
185234
* @param string $str
186235
* @param string $delimiter

src/Str/UrlHelper.php

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

1212
use InvalidArgumentException;
13+
use function implode;
1314
use function preg_match;
1415
use function http_build_query;
1516
use function curl_init;
@@ -47,7 +48,7 @@ class UrlHelper
4748
*/
4849
public static function isRelative(string $url): bool
4950
{
50-
return false === strpos($url, '//') && strpos($url, '://') === false;
51+
return !str_contains($url, '//') && !str_contains($url, '://');
5152
}
5253

5354
/**
@@ -69,7 +70,7 @@ public static function isUrl(string $str): bool
6970
*/
7071
public static function isGitUrl(string $str): bool
7172
{
72-
if (strpos($str, 'git@') === 0) {
73+
if (str_starts_with($str, 'git@')) {
7374
$str = 'ssh://' . $str;
7475
}
7576

@@ -99,12 +100,23 @@ public static function isFullUrl(string $url): bool
99100
}
100101

101102
/**
102-
* @param string $baseUrl
103-
* @param array|object $data
103+
* @param string $baseUri
104+
* @param string ...$paths
104105
*
105106
* @return string
106107
*/
107-
public static function build(string $baseUrl, $data = null): string
108+
public static function joinPath(string $baseUri, string ...$paths): string
109+
{
110+
return $baseUri . ($paths ? '/' . implode('/', $paths) : '');
111+
}
112+
113+
/**
114+
* @param string $baseUrl
115+
* @param array $data
116+
*
117+
* @return string
118+
*/
119+
public static function build(string $baseUrl, array $data = []): string
108120
{
109121
if ($data && ($param = http_build_query($data))) {
110122
if ($baseUrl) {
@@ -154,7 +166,7 @@ public static function canAccessed(string $url): bool
154166
}
155167

156168
// Build arrays of values we need to decode before parsing
157-
protected static $entities = [
169+
protected static array $entities = [
158170
'%21',
159171
'%2A',
160172
'%27',
@@ -174,7 +186,7 @@ public static function canAccessed(string $url): bool
174186
'%5D'
175187
];
176188

177-
protected static $replacements = [
189+
protected static array $replacements = [
178190
'!',
179191
'*',
180192
"'",

test/BaseLibTestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* class BaseLibTestCase
9+
*/
10+
abstract class BaseLibTestCase extends TestCase
11+
{
12+
13+
}

test/Str/UrlHelperTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest\Str;
4+
5+
use Toolkit\Stdlib\Str\UrlHelper;
6+
use Toolkit\StdlibTest\BaseLibTestCase;
7+
8+
/**
9+
* class UrlHelperTest
10+
*/
11+
class UrlHelperTest extends BaseLibTestCase
12+
{
13+
public function testJoinPath(): void
14+
{
15+
$this->assertEquals('a', UrlHelper::joinPath('a'));
16+
$this->assertEquals('a/b/cd', UrlHelper::joinPath('a', 'b', 'cd'));
17+
}
18+
}

0 commit comments

Comments
 (0)