Skip to content

Commit

Permalink
Support Enumerable in Stringable
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Sep 5, 2022
1 parent 7fa4beb commit f6aa6c2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
50 changes: 39 additions & 11 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, string> $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);
Expand All @@ -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<array-key, string> $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);
Expand All @@ -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<array-key, string> $needles
* @return bool
*/
public static function endsWith($haystack, $needles)
Expand Down Expand Up @@ -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<int|string, string> $replace
* @param string[]|Enumerable<array-key, string> $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);
Expand All @@ -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<array-key, string> $search
* @param string|string[]|Enumerable<array-key, string> $replace
* @param string|string[]|Enumerable<array-key, string> $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);
}

Expand Down Expand Up @@ -862,13 +886,17 @@ public static function replaceLast($search, $replace, $subject)
/**
* Remove any occurrence of the given string in the subject.
*
* @param string|array<string> $search
* @param string|string[]|Enumerable<array-key, string> $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);
Expand Down Expand Up @@ -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<array-key, string> $needles
* @return bool
*/
public static function startsWith($haystack, $needles)
Expand Down
12 changes: 10 additions & 2 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, string> $search
* @param string|string[]|Enumerable<array-key, string> $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));
}

Expand Down
1 change: 1 addition & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f6aa6c2

Please sign in to comment.