Skip to content

Commit d44c345

Browse files
authored
Support Enumerable in Stringable (#44012)
1 parent 78289b4 commit d44c345

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

src/Illuminate/Support/Str.php

+39-11
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,16 @@ public static function camel($value)
218218
* Determine if a given string contains a given substring.
219219
*
220220
* @param string $haystack
221-
* @param string|string[] $needles
221+
* @param string|string[]|Enumerable<array-key, string> $needles
222222
* @param bool $ignoreCase
223223
* @return bool
224224
*/
225225
public static function contains($haystack, $needles, $ignoreCase = false)
226226
{
227+
if ($needles instanceof Enumerable) {
228+
$needles = $needles->toArray();
229+
}
230+
227231
if ($ignoreCase) {
228232
$haystack = mb_strtolower($haystack);
229233
$needles = array_map('mb_strtolower', (array) $needles);
@@ -242,12 +246,16 @@ public static function contains($haystack, $needles, $ignoreCase = false)
242246
* Determine if a given string contains all array values.
243247
*
244248
* @param string $haystack
245-
* @param string[] $needles
249+
* @param string[]|Enumerable<array-key, string> $needles
246250
* @param bool $ignoreCase
247251
* @return bool
248252
*/
249-
public static function containsAll($haystack, array $needles, $ignoreCase = false)
253+
public static function containsAll($haystack, $needles, $ignoreCase = false)
250254
{
255+
if ($needles instanceof Enumerable) {
256+
$needles = $needles->toArray();
257+
}
258+
251259
if ($ignoreCase) {
252260
$haystack = mb_strtolower($haystack);
253261
$needles = array_map('mb_strtolower', $needles);
@@ -266,7 +274,7 @@ public static function containsAll($haystack, array $needles, $ignoreCase = fals
266274
* Determine if a given string ends with a given substring.
267275
*
268276
* @param string $haystack
269-
* @param string|string[] $needles
277+
* @param string|string[]|Enumerable<array-key, string> $needles
270278
* @return bool
271279
*/
272280
public static function endsWith($haystack, $needles)
@@ -781,12 +789,16 @@ public static function repeat(string $string, int $times)
781789
* Replace a given value in the string sequentially with an array.
782790
*
783791
* @param string $search
784-
* @param array<int|string, string> $replace
792+
* @param string[]|Enumerable<array-key, string> $replace
785793
* @param string $subject
786794
* @return string
787795
*/
788-
public static function replaceArray($search, array $replace, $subject)
796+
public static function replaceArray($search, $replace, $subject)
789797
{
798+
if ($replace instanceof Enumerable) {
799+
$replace = $replace->toArray();
800+
}
801+
790802
$segments = explode($search, $subject);
791803

792804
$result = array_shift($segments);
@@ -801,13 +813,25 @@ public static function replaceArray($search, array $replace, $subject)
801813
/**
802814
* Replace the given value in the given string.
803815
*
804-
* @param string|string[] $search
805-
* @param string|string[] $replace
806-
* @param string|string[] $subject
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
807819
* @return string
808820
*/
809821
public static function replace($search, $replace, $subject)
810822
{
823+
if ($search instanceof Enumerable) {
824+
$search = $search->toArray();
825+
}
826+
827+
if ($replace instanceof Enumerable) {
828+
$replace = $replace->toArray();
829+
}
830+
831+
if ($subject instanceof Enumerable) {
832+
$subject = $subject->toArray();
833+
}
834+
811835
return str_replace($search, $replace, $subject);
812836
}
813837

@@ -862,13 +886,17 @@ public static function replaceLast($search, $replace, $subject)
862886
/**
863887
* Remove any occurrence of the given string in the subject.
864888
*
865-
* @param string|array<string> $search
889+
* @param string|string[]|Enumerable<array-key, string> $search
866890
* @param string $subject
867891
* @param bool $caseSensitive
868892
* @return string
869893
*/
870894
public static function remove($search, $subject, $caseSensitive = true)
871895
{
896+
if ($search instanceof Enumerable) {
897+
$search = $search->toArray();
898+
}
899+
872900
$subject = $caseSensitive
873901
? str_replace($search, '', $subject)
874902
: str_ireplace($search, '', $subject);
@@ -1021,7 +1049,7 @@ public static function squish($value)
10211049
* Determine if a given string starts with a given substring.
10221050
*
10231051
* @param string $haystack
1024-
* @param string|string[] $needles
1052+
* @param string|string[]|Enumerable<array-key, string> $needles
10251053
* @return bool
10261054
*/
10271055
public static function startsWith($haystack, $needles)

src/Illuminate/Support/Stringable.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,20 @@ public function repeat(int $times)
576576
/**
577577
* Replace the given value in the given string.
578578
*
579-
* @param string|string[] $search
580-
* @param string|string[] $replace
579+
* @param string|string[]|Enumerable<array-key, string> $search
580+
* @param string|string[]|Enumerable<array-key, string> $replace
581581
* @return static
582582
*/
583583
public function replace($search, $replace)
584584
{
585+
if ($search instanceof Enumerable) {
586+
$search = $search->toArray();
587+
}
588+
589+
if ($replace instanceof Enumerable) {
590+
$replace = $replace->toArray();
591+
}
592+
585593
return new static(str_replace($search, $replace, $this->value));
586594
}
587595

tests/Support/SupportStrTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ public function testReplace()
524524
$this->assertSame('foo bar baz 8.x', Str::replace('?', '8.x', 'foo bar baz ?'));
525525
$this->assertSame('foo/bar/baz', Str::replace(' ', '/', 'foo bar baz'));
526526
$this->assertSame('foo bar baz', Str::replace(['?1', '?2', '?3'], ['foo', 'bar', 'baz'], '?1 ?2 ?3'));
527+
$this->assertSame(['foo', 'bar', 'baz'], Str::replace(collect(['?1', '?2', '?3']), collect(['foo', 'bar', 'baz']), collect(['?1', '?2', '?3'])));
527528
}
528529

529530
public function testReplaceArray()

tests/Support/SupportStringableTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ public function testReplace()
766766
$this->assertSame('bar/bar', (string) $this->stringable('?/?')->replace('?', 'bar'));
767767
$this->assertSame('?/?/?', (string) $this->stringable('? ? ?')->replace(' ', '/'));
768768
$this->assertSame('foo/bar/baz/bam', (string) $this->stringable('?1/?2/?3/?4')->replace(['?1', '?2', '?3', '?4'], ['foo', 'bar', 'baz', 'bam']));
769+
$this->assertSame('foo/bar/baz/bam', (string) $this->stringable('?1/?2/?3/?4')->replace(collect(['?1', '?2', '?3', '?4']), collect(['foo', 'bar', 'baz', 'bam'])));
769770
}
770771

771772
public function testReplaceArray()

0 commit comments

Comments
 (0)