Skip to content

Commit 41c0764

Browse files
committed
new: add more string check methods
1 parent bf3d34c commit 41c0764

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
],
2222
"require": {
2323
"php": ">7.1.0",
24-
"ext-mbstring": "*"
24+
"ext-mbstring": "*",
25+
"symfony/polyfill-php80": "~1.0"
2526
},
2627
"autoload": {
2728
"psr-4": {

src/Str/Traits/StringCheckHelperTrait.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
namespace Toolkit\Stdlib\Str\Traits;
1111

12+
use JetBrains\PhpStorm\Pure;
1213
use function function_exists;
1314
use function is_array;
1415
use function is_string;
1516
use function mb_strpos;
1617
use function mb_strrpos;
1718
use function preg_match;
19+
use function str_ends_with;
20+
use function str_starts_with;
1821
use function stripos;
1922
use function strpos;
2023
use function strrpos;
@@ -47,6 +50,16 @@ public static function optional(string $string, string $prefix = ' ', string $su
4750
return $prefix . $string . $suffix;
4851
}
4952

53+
/**
54+
* @param string $string
55+
* @param string|array $needle
56+
* @return bool
57+
*/
58+
public static function notContains(string $string, $needle): bool
59+
{
60+
return !self::has($string, $needle);
61+
}
62+
5063
/**
5164
* @param string $string
5265
* @param string|array $needle
@@ -65,12 +78,12 @@ public static function contains(string $string, $needle): bool
6578
public static function has(string $string, $needle): bool
6679
{
6780
if (is_string($needle)) {
68-
return strpos($string, $needle) !== false;
81+
return str_contains($string, $needle);
6982
}
7083

7184
if (is_array($needle)) {
7285
foreach ((array)$needle as $item) {
73-
if (strpos($string, $item) !== false) {
86+
if (str_contains($string, $item)) {
7487
return true;
7588
}
7689
}
@@ -191,26 +204,46 @@ public static function strrpos(string $str, string $find, int $offset = 0, strin
191204

192205
/**
193206
* @param string $str
194-
* @param string $prefix
207+
* @param string $needle
195208
*
196209
* @return bool
197210
*/
198-
public static function hasPrefix(string $str, string $prefix): bool
211+
public static function isStartWiths(string $str, string $needle): bool
199212
{
200-
return self::strpos($str, $prefix) === 0;
213+
return self::hasPrefix($str, $needle);
201214
}
202215

203216
/**
204217
* @param string $str
205-
* @param string $suffix
218+
* @param string $needle
206219
*
207220
* @return bool
208221
*/
209-
public static function hasSuffix(string $str, string $suffix): bool
222+
public static function hasPrefix(string $str, string $needle): bool
210223
{
211-
$pos = self::strpos($str, $suffix);
224+
return str_starts_with($str, $needle);
225+
}
212226

213-
return $pos !== false && self::substr($str, - self::strlen($suffix)) === $suffix;
227+
/**
228+
* @param string $str
229+
* @param string $needle
230+
*
231+
* @return bool
232+
*/
233+
public static function isEndWiths(string $str, string $needle): bool
234+
{
235+
return self::hasSuffix($str, $needle);
236+
}
237+
238+
/**
239+
* @param string $str
240+
* @param string $needle
241+
*
242+
* @return bool
243+
*/
244+
public static function hasSuffix(string $str, string $needle): bool
245+
{
246+
return str_ends_with($str, $needle);
214247
}
215248

216249
/**

test/Str/StringHelperTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public function testStrLen(): void
4545
}
4646
}
4747

48+
public function testHasPrefix(): void
49+
{
50+
self::assertTrue(Str::hasPrefix('abc', 'a'));
51+
self::assertFalse(Str::hasPrefix('abc', 'c'));
52+
self::assertTrue(Str::hasSuffix('abc', 'c'));
53+
self::assertTrue(Str::hasSuffix('abc', 'bc'));
54+
self::assertFalse(Str::hasSuffix('abc', 'b'));
55+
}
56+
4857
public function testStrpos(): void
4958
{
5059
$tests = [
@@ -59,10 +68,7 @@ public function testStrpos(): void
5968
$this->assertTrue(Str::icontains($case, $want));
6069
}
6170

62-
self::assertTrue(Str::hasPrefix('abc', 'a'));
63-
self::assertFalse(Str::hasPrefix('abc', 'c'));
64-
self::assertTrue(Str::hasSuffix('abc', 'c'));
65-
self::assertTrue(Str::hasSuffix('abc', 'bc'));
66-
self::assertFalse(Str::hasSuffix('abc', 'b'));
71+
self::assertTrue(Str::notContains('abc', 'd'));
72+
self::assertFalse(Str::notContains('abc', 'b'));
6773
}
6874
}

0 commit comments

Comments
 (0)