9
9
10
10
namespace Toolkit \Stdlib \Str \Traits ;
11
11
12
+ use JetBrains \PhpStorm \Pure ;
12
13
use function function_exists ;
13
14
use function is_array ;
14
15
use function is_string ;
15
16
use function mb_strpos ;
16
17
use function mb_strrpos ;
17
18
use function preg_match ;
19
+ use function str_ends_with ;
20
+ use function str_starts_with ;
18
21
use function stripos ;
19
22
use function strpos ;
20
23
use function strrpos ;
@@ -47,6 +50,16 @@ public static function optional(string $string, string $prefix = ' ', string $su
47
50
return $ prefix . $ string . $ suffix ;
48
51
}
49
52
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
+
50
63
/**
51
64
* @param string $string
52
65
* @param string|array $needle
@@ -65,12 +78,12 @@ public static function contains(string $string, $needle): bool
65
78
public static function has (string $ string , $ needle ): bool
66
79
{
67
80
if (is_string ($ needle )) {
68
- return strpos ($ string , $ needle ) !== false ;
81
+ return str_contains ($ string , $ needle );
69
82
}
70
83
71
84
if (is_array ($ needle )) {
72
85
foreach ((array )$ needle as $ item ) {
73
- if (strpos ($ string , $ item ) !== false ) {
86
+ if (str_contains ($ string , $ item )) {
74
87
return true ;
75
88
}
76
89
}
@@ -191,26 +204,46 @@ public static function strrpos(string $str, string $find, int $offset = 0, strin
191
204
192
205
/**
193
206
* @param string $str
194
- * @param string $prefix
207
+ * @param string $needle
195
208
*
196
209
* @return bool
197
210
*/
198
- public static function hasPrefix (string $ str , string $ prefix ): bool
211
+ public static function isStartWiths (string $ str , string $ needle ): bool
199
212
{
200
- return self ::strpos ($ str , $ prefix ) === 0 ;
213
+ return self ::hasPrefix ($ str , $ needle ) ;
201
214
}
202
215
203
216
/**
204
217
* @param string $str
205
- * @param string $suffix
218
+ * @param string $needle
206
219
*
207
220
* @return bool
208
221
*/
209
- public static function hasSuffix (string $ str , string $ suffix ): bool
222
+ public static function hasPrefix (string $ str , string $ needle ): bool
210
223
{
211
- $ pos = self ::strpos ($ str , $ suffix );
224
+ return str_starts_with ($ str , $ needle );
225
+ }
212
226
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 );
214
247
}
215
248
216
249
/**
0 commit comments