Skip to content

Commit ac5e1f0

Browse files
committed
prof: update some string check methods
1 parent a540641 commit ac5e1f0

File tree

2 files changed

+156
-9
lines changed

2 files changed

+156
-9
lines changed

src/Str/Traits/StringCheckHelperTrait.php

Lines changed: 127 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use function str_ends_with;
2020
use function str_starts_with;
2121
use function stripos;
22+
use function strlen;
2223
use function strpos;
2324
use function strrpos;
2425
use function strtolower;
@@ -276,7 +277,64 @@ public static function strrpos(string $str, string $find, int $offset = 0, strin
276277
}
277278

278279
/**
279-
* Assert is start withs one
280+
* @param string $str
281+
* @param string $needle
282+
*
283+
* @return bool
284+
*/
285+
public static function startWith(string $str, string $needle): bool
286+
{
287+
return self::hasPrefix($str, $needle);
288+
}
289+
290+
/**
291+
* @param string $str
292+
* @param string $needle
293+
*
294+
* @return bool
295+
*/
296+
public static function hasPrefix(string $str, string $needle): bool
297+
{
298+
return str_starts_with($str, $needle);
299+
}
300+
301+
/**
302+
* @param string $str
303+
* @param string $needle
304+
*
305+
* @return bool
306+
*/
307+
public static function isStartWithIC(string $str, string $needle): bool
308+
{
309+
return self::hasPrefixIC($str, $needle);
310+
}
311+
312+
/**
313+
* @param string $str
314+
* @param string $needle
315+
*
316+
* @return bool
317+
*/
318+
public static function startWithIC(string $str, string $needle): bool
319+
{
320+
return self::hasPrefixIC($str, $needle);
321+
}
322+
323+
/**
324+
* ignore case, the passed $str ends with the $needle string
325+
*
326+
* @param string $str
327+
* @param string $needle
328+
*
329+
* @return bool
330+
*/
331+
public static function hasPrefixIC(string $str, string $needle): bool
332+
{
333+
return stripos($str, $needle) === 0;
334+
}
335+
336+
/**
337+
* check $str is start withs one of $needle
280338
*
281339
* @param string $str
282340
* @param string|array $needle
@@ -299,13 +357,13 @@ public static function isStartWiths(string $str, $needle): bool
299357

300358
/**
301359
* @param string $str
302-
* @param string $needle
360+
* @param string|array $needle
303361
*
304362
* @return bool
305363
*/
306-
public static function hasPrefix(string $str, string $needle): bool
364+
public static function startWiths(string $str, $needle): bool
307365
{
308-
return str_starts_with($str, $needle);
366+
return self::isStartWiths($str, $needle);
309367
}
310368

311369
/**
@@ -320,14 +378,16 @@ public static function hasPrefixes(string $str, array $needles): bool
320378
}
321379

322380
/**
381+
* the passed $str ends with the $needle string
382+
*
323383
* @param string $str
324384
* @param string $needle
325385
*
326386
* @return bool
327387
*/
328-
public static function isEndWiths(string $str, string $needle): bool
388+
public static function hasSuffix(string $str, string $needle): bool
329389
{
330-
return self::hasSuffix($str, $needle);
390+
return str_ends_with($str, $needle);
331391
}
332392

333393
/**
@@ -336,9 +396,68 @@ public static function isEndWiths(string $str, string $needle): bool
336396
*
337397
* @return bool
338398
*/
339-
public static function hasSuffix(string $str, string $needle): bool
399+
public static function endWithIC(string $str, string $needle): bool
340400
{
341-
return str_ends_with($str, $needle);
401+
return self::hasSuffixIC($str, $needle);
402+
}
403+
404+
/**
405+
* ignore case, check $str is ends with the $needle string
406+
*
407+
* @param string $str
408+
* @param string $needle
409+
*
410+
* @return bool
411+
*/
412+
public static function hasSuffixIC(string $str, string $needle): bool
413+
{
414+
$pos = stripos($str, $needle);
415+
416+
return $pos !== false && $pos + strlen($needle) === strlen($str);
417+
}
418+
419+
/**
420+
* @param string $str
421+
* @param string|array $needle
422+
*
423+
* @return bool
424+
*/
425+
public static function endWiths(string $str, $needle): bool
426+
{
427+
return self::hasSuffixes($str, $needle);
428+
}
429+
430+
/**
431+
* @param string $str
432+
* @param string|array $needle
433+
*
434+
* @return bool
435+
*/
436+
public static function isEndWiths(string $str, $needle): bool
437+
{
438+
return self::hasSuffixes($str, $needle);
439+
}
440+
441+
/**
442+
* Assert is start withs one
443+
*
444+
* @param string $str
445+
* @param string|array $needles
446+
*
447+
* @return bool
448+
*/
449+
public static function hasSuffixes(string $str, $needles): bool
450+
{
451+
if (is_array($needles)) {
452+
foreach ($needles as $needle) {
453+
if (str_ends_with($str, $needle)) {
454+
return true;
455+
}
456+
}
457+
return false;
458+
}
459+
460+
return str_ends_with($str, $needles);
342461
}
343462

344463
/**

test/Str/StringHelperTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,39 @@ public function testHasPrefix(): void
9595
{
9696
self::assertTrue(Str::hasPrefix('abc', 'a'));
9797
self::assertFalse(Str::hasPrefix('abc', 'c'));
98-
self::assertTrue(Str::hasSuffix('abc', 'c'));
98+
self::assertTrue(Str::endWiths('abc', 'c'));
9999
self::assertTrue(Str::hasSuffix('abc', 'bc'));
100100
self::assertFalse(Str::hasSuffix('abc', 'b'));
101101
}
102102

103+
public function testIEndWiths(): void
104+
{
105+
self::assertTrue(Str::endWithIC('abC', 'C'));
106+
self::assertTrue(Str::endWithIC('abC', 'c'));
107+
self::assertFalse(Str::endWithIC('abc', 'b'));
108+
self::assertFalse(Str::hasPrefixIC('abc', 'a'));
109+
110+
self::assertFalse(Str::hasPrefixIC('abc', 'a'));
111+
}
112+
113+
public function testHasPrefixIC(): void
114+
{
115+
$tests = [
116+
['abc', 'a', true],
117+
['23ab', 'a', false],
118+
];
119+
120+
foreach ($tests as [$case, $want, $yes]) {
121+
if ($yes) {
122+
$this->assertTrue(Str::hasPrefixIC($case, $want));
123+
$this->assertTrue(Str::startWithIC($case, $want));
124+
$this->assertTrue(Str::isStartWithIC($case, $want));
125+
} else {
126+
$this->assertFalse(Str::hasPrefixIC($case, $want));
127+
}
128+
}
129+
}
130+
103131
public function testStrpos(): void
104132
{
105133
$tests = [

0 commit comments

Comments
 (0)