14
14
use Ramsey \Uuid \Generator \CombGenerator ;
15
15
use Ramsey \Uuid \Uuid ;
16
16
use Ramsey \Uuid \UuidFactory ;
17
+ use Traversable ;
17
18
use voku \helper \ASCII ;
18
19
19
20
class Str
@@ -218,22 +219,25 @@ public static function camel($value)
218
219
* Determine if a given string contains a given substring.
219
220
*
220
221
* @param string $haystack
221
- * @param string|string[]|Enumerable<array-key, string> $needles
222
+ * @param string|iterable< string> $needles
222
223
* @param bool $ignoreCase
223
224
* @return bool
224
225
*/
225
226
public static function contains ($ haystack , $ needles , $ ignoreCase = false )
226
227
{
227
- if ($ needles instanceof Enumerable) {
228
- $ needles = $ needles ->toArray ();
229
- }
230
-
231
228
if ($ ignoreCase ) {
232
229
$ haystack = mb_strtolower ($ haystack );
233
- $ needles = array_map ('mb_strtolower ' , (array ) $ needles );
234
230
}
235
231
236
- foreach ((array ) $ needles as $ needle ) {
232
+ if (! is_iterable ($ needles )) {
233
+ $ needles = (array ) $ needles ;
234
+ }
235
+
236
+ foreach ($ needles as $ needle ) {
237
+ if ($ ignoreCase ) {
238
+ $ needle = mb_strtolower ($ needle );
239
+ }
240
+
237
241
if ($ needle !== '' && str_contains ($ haystack , $ needle )) {
238
242
return true ;
239
243
}
@@ -246,23 +250,14 @@ public static function contains($haystack, $needles, $ignoreCase = false)
246
250
* Determine if a given string contains all array values.
247
251
*
248
252
* @param string $haystack
249
- * @param string[]|Enumerable<array-key, string> $needles
253
+ * @param iterable< string> $needles
250
254
* @param bool $ignoreCase
251
255
* @return bool
252
256
*/
253
257
public static function containsAll ($ haystack , $ needles , $ ignoreCase = false )
254
258
{
255
- if ($ needles instanceof Enumerable) {
256
- $ needles = $ needles ->toArray ();
257
- }
258
-
259
- if ($ ignoreCase ) {
260
- $ haystack = mb_strtolower ($ haystack );
261
- $ needles = array_map ('mb_strtolower ' , $ needles );
262
- }
263
-
264
259
foreach ($ needles as $ needle ) {
265
- if (! static ::contains ($ haystack , $ needle )) {
260
+ if (! static ::contains ($ haystack , $ needle, $ ignoreCase )) {
266
261
return false ;
267
262
}
268
263
}
@@ -274,12 +269,16 @@ public static function containsAll($haystack, $needles, $ignoreCase = false)
274
269
* Determine if a given string ends with a given substring.
275
270
*
276
271
* @param string $haystack
277
- * @param string|string[]|Enumerable<array-key, string> $needles
272
+ * @param string|iterable< string> $needles
278
273
* @return bool
279
274
*/
280
275
public static function endsWith ($ haystack , $ needles )
281
276
{
282
- foreach ((array ) $ needles as $ needle ) {
277
+ if (! is_iterable ($ needles )) {
278
+ $ needles = (array ) $ needles ;
279
+ }
280
+
281
+ foreach ($ needles as $ needle ) {
283
282
if ((string ) $ needle !== '' && str_ends_with ($ haystack , $ needle )) {
284
283
return true ;
285
284
}
@@ -341,21 +340,19 @@ public static function finish($value, $cap)
341
340
/**
342
341
* Determine if a given string matches a given pattern.
343
342
*
344
- * @param string|array $pattern
343
+ * @param string|iterable<string> $pattern
345
344
* @param string $value
346
345
* @return bool
347
346
*/
348
347
public static function is ($ pattern , $ value )
349
348
{
350
- $ patterns = Arr::wrap ($ pattern );
351
-
352
349
$ value = (string ) $ value ;
353
350
354
- if (empty ( $ patterns )) {
355
- return false ;
351
+ if (! is_iterable ( $ pattern )) {
352
+ $ pattern = [ $ pattern ] ;
356
353
}
357
354
358
- foreach ($ patterns as $ pattern ) {
355
+ foreach ($ pattern as $ pattern ) {
359
356
$ pattern = (string ) $ pattern ;
360
357
361
358
// If the given value is an exact match we can of course return true right
@@ -789,14 +786,14 @@ public static function repeat(string $string, int $times)
789
786
* Replace a given value in the string sequentially with an array.
790
787
*
791
788
* @param string $search
792
- * @param string[]|Enumerable<array-key, string> $replace
789
+ * @param iterable< string> $replace
793
790
* @param string $subject
794
791
* @return string
795
792
*/
796
793
public static function replaceArray ($ search , $ replace , $ subject )
797
794
{
798
- if ($ replace instanceof Enumerable ) {
799
- $ replace = $ replace-> toArray ();
795
+ if ($ replace instanceof Traversable ) {
796
+ $ replace = collect ( $ replace)-> all ();
800
797
}
801
798
802
799
$ segments = explode ($ search , $ subject );
@@ -813,23 +810,23 @@ public static function replaceArray($search, $replace, $subject)
813
810
/**
814
811
* Replace the given value in the given string.
815
812
*
816
- * @param string|string[]|Enumerable<array-key, string> $search
817
- * @param string|string[]|Enumerable<array-key, string> $replace
818
- * @param string|string[]|Enumerable<array-key, string> $subject
813
+ * @param string|iterable< string> $search
814
+ * @param string|iterable< string> $replace
815
+ * @param string|iterable< string> $subject
819
816
* @return string
820
817
*/
821
818
public static function replace ($ search , $ replace , $ subject )
822
819
{
823
- if ($ search instanceof Enumerable ) {
824
- $ search = $ search-> toArray ();
820
+ if ($ search instanceof Traversable ) {
821
+ $ search = collect ( $ search)-> all ();
825
822
}
826
823
827
- if ($ replace instanceof Enumerable ) {
828
- $ replace = $ replace-> toArray ();
824
+ if ($ replace instanceof Traversable ) {
825
+ $ replace = collect ( $ replace)-> all ();
829
826
}
830
827
831
- if ($ subject instanceof Enumerable ) {
832
- $ subject = $ subject-> toArray ();
828
+ if ($ subject instanceof Traversable ) {
829
+ $ subject = collect ( $ subject)-> all ();
833
830
}
834
831
835
832
return str_replace ($ search , $ replace , $ subject );
@@ -886,15 +883,15 @@ public static function replaceLast($search, $replace, $subject)
886
883
/**
887
884
* Remove any occurrence of the given string in the subject.
888
885
*
889
- * @param string|string[]|Enumerable<array-key, string> $search
886
+ * @param string|iterable< string> $search
890
887
* @param string $subject
891
888
* @param bool $caseSensitive
892
889
* @return string
893
890
*/
894
891
public static function remove ($ search , $ subject , $ caseSensitive = true )
895
892
{
896
- if ($ search instanceof Enumerable ) {
897
- $ search = $ search-> toArray ();
893
+ if ($ search instanceof Traversable ) {
894
+ $ search = collect ( $ search)-> all ();
898
895
}
899
896
900
897
$ subject = $ caseSensitive
@@ -1049,12 +1046,16 @@ public static function squish($value)
1049
1046
* Determine if a given string starts with a given substring.
1050
1047
*
1051
1048
* @param string $haystack
1052
- * @param string|string[]|Enumerable<array-key, string> $needles
1049
+ * @param string|iterable< string> $needles
1053
1050
* @return bool
1054
1051
*/
1055
1052
public static function startsWith ($ haystack , $ needles )
1056
1053
{
1057
- foreach ((array ) $ needles as $ needle ) {
1054
+ if (! is_iterable ($ needles )) {
1055
+ $ needles = [$ needles ];
1056
+ }
1057
+
1058
+ foreach ($ needles as $ needle ) {
1058
1059
if ((string ) $ needle !== '' && str_starts_with ($ haystack , $ needle )) {
1059
1060
return true ;
1060
1061
}
@@ -1118,11 +1119,11 @@ public static function substrCount($haystack, $needle, $offset = 0, $length = nu
1118
1119
/**
1119
1120
* Replace text within a portion of a string.
1120
1121
*
1121
- * @param string|array $string
1122
- * @param string|array $replace
1123
- * @param array |int $offset
1124
- * @param array |int|null $length
1125
- * @return string|array
1122
+ * @param string|string[] $string
1123
+ * @param string|string[] $replace
1124
+ * @param int |int[] $offset
1125
+ * @param int |int[] |null $length
1126
+ * @return string|string[]
1126
1127
*/
1127
1128
public static function substrReplace ($ string , $ replace , $ offset = 0 , $ length = null )
1128
1129
{
@@ -1171,7 +1172,7 @@ public static function ucfirst($string)
1171
1172
* Split a string into pieces by uppercase characters.
1172
1173
*
1173
1174
* @param string $string
1174
- * @return array
1175
+ * @return string[]
1175
1176
*/
1176
1177
public static function ucsplit ($ string )
1177
1178
{
0 commit comments