diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 382fcb5554ae..f84e16388eaf 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -218,12 +218,16 @@ public static function camel($value) * Determine if a given string contains a given substring. * * @param string $haystack - * @param string|string[] $needles + * @param string|string[]|Enumerable $needles * @param bool $ignoreCase * @return bool */ public static function contains($haystack, $needles, $ignoreCase = false) { + if ($needles instanceof Enumerable) { + $needles = $needles->toArray(); + } + if ($ignoreCase) { $haystack = mb_strtolower($haystack); $needles = array_map('mb_strtolower', (array) $needles); @@ -242,12 +246,16 @@ public static function contains($haystack, $needles, $ignoreCase = false) * Determine if a given string contains all array values. * * @param string $haystack - * @param string[] $needles + * @param string[]|Enumerable $needles * @param bool $ignoreCase * @return bool */ - public static function containsAll($haystack, array $needles, $ignoreCase = false) + public static function containsAll($haystack, $needles, $ignoreCase = false) { + if ($needles instanceof Enumerable) { + $needles = $needles->toArray(); + } + if ($ignoreCase) { $haystack = mb_strtolower($haystack); $needles = array_map('mb_strtolower', $needles); @@ -266,7 +274,7 @@ public static function containsAll($haystack, array $needles, $ignoreCase = fals * Determine if a given string ends with a given substring. * * @param string $haystack - * @param string|string[] $needles + * @param string|string[]|Enumerable $needles * @return bool */ public static function endsWith($haystack, $needles) @@ -781,12 +789,16 @@ public static function repeat(string $string, int $times) * Replace a given value in the string sequentially with an array. * * @param string $search - * @param array $replace + * @param string[]|Enumerable $replace * @param string $subject * @return string */ - public static function replaceArray($search, array $replace, $subject) + public static function replaceArray($search, $replace, $subject) { + if ($replace instanceof Enumerable) { + $replace = $replace->toArray(); + } + $segments = explode($search, $subject); $result = array_shift($segments); @@ -801,13 +813,25 @@ public static function replaceArray($search, array $replace, $subject) /** * Replace the given value in the given string. * - * @param string|string[] $search - * @param string|string[] $replace - * @param string|string[] $subject + * @param string|string[]|Enumerable $search + * @param string|string[]|Enumerable $replace + * @param string|string[]|Enumerable $subject * @return string */ public static function replace($search, $replace, $subject) { + if ($search instanceof Enumerable) { + $search = $search->toArray(); + } + + if ($replace instanceof Enumerable) { + $replace = $replace->toArray(); + } + + if ($subject instanceof Enumerable) { + $subject = $subject->toArray(); + } + return str_replace($search, $replace, $subject); } @@ -862,13 +886,17 @@ public static function replaceLast($search, $replace, $subject) /** * Remove any occurrence of the given string in the subject. * - * @param string|array $search + * @param string|string[]|Enumerable $search * @param string $subject * @param bool $caseSensitive * @return string */ public static function remove($search, $subject, $caseSensitive = true) { + if ($search instanceof Enumerable) { + $search = $search->toArray(); + } + $subject = $caseSensitive ? str_replace($search, '', $subject) : str_ireplace($search, '', $subject); @@ -1021,7 +1049,7 @@ public static function squish($value) * Determine if a given string starts with a given substring. * * @param string $haystack - * @param string|string[] $needles + * @param string|string[]|Enumerable $needles * @return bool */ public static function startsWith($haystack, $needles) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index ae35b57172f9..907960bc3876 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -576,12 +576,20 @@ public function repeat(int $times) /** * Replace the given value in the given string. * - * @param string|string[] $search - * @param string|string[] $replace + * @param string|string[]|Enumerable $search + * @param string|string[]|Enumerable $replace * @return static */ public function replace($search, $replace) { + if ($search instanceof Enumerable) { + $search = $search->toArray(); + } + + if ($replace instanceof Enumerable) { + $replace = $replace->toArray(); + } + return new static(str_replace($search, $replace, $this->value)); } diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 39e551d995ee..7136a130664d 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -524,6 +524,7 @@ public function testReplace() $this->assertSame('foo bar baz 8.x', Str::replace('?', '8.x', 'foo bar baz ?')); $this->assertSame('foo/bar/baz', Str::replace(' ', '/', 'foo bar baz')); $this->assertSame('foo bar baz', Str::replace(['?1', '?2', '?3'], ['foo', 'bar', 'baz'], '?1 ?2 ?3')); + $this->assertSame(['foo', 'bar', 'baz'], Str::replace(collect(['?1', '?2', '?3']), collect(['foo', 'bar', 'baz']), collect(['?1', '?2', '?3']))); } public function testReplaceArray() diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index ee1b9d149395..7686163f857d 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -766,6 +766,7 @@ public function testReplace() $this->assertSame('bar/bar', (string) $this->stringable('?/?')->replace('?', 'bar')); $this->assertSame('?/?/?', (string) $this->stringable('? ? ?')->replace(' ', '/')); $this->assertSame('foo/bar/baz/bam', (string) $this->stringable('?1/?2/?3/?4')->replace(['?1', '?2', '?3', '?4'], ['foo', 'bar', 'baz', 'bam'])); + $this->assertSame('foo/bar/baz/bam', (string) $this->stringable('?1/?2/?3/?4')->replace(collect(['?1', '?2', '?3', '?4']), collect(['foo', 'bar', 'baz', 'bam']))); } public function testReplaceArray()