99
1010namespace Toolkit \Stdlib \Str \Traits ;
1111
12+ use JetBrains \PhpStorm \Pure ;
1213use function function_exists ;
1314use function is_array ;
1415use function is_string ;
1516use function mb_strpos ;
1617use function mb_strrpos ;
1718use function preg_match ;
19+ use function str_ends_with ;
20+ use function str_starts_with ;
1821use function stripos ;
1922use function strpos ;
2023use 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 /**
0 commit comments