Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add reverse method to Str #39816

Merged
merged 2 commits into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Str.php
Expand Up @@ -675,6 +675,17 @@ public static function remove($search, $subject, $caseSensitive = true)
return $subject;
}

/**
* Reverse the given string.
*
* @param string $value
* @return string
*/
public static function reverse(string $value)
{
return implode(array_reverse(mb_str_split($value)));
}

/**
* Begin a string with a single instance of a given value.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Stringable.php
Expand Up @@ -492,6 +492,16 @@ public function remove($search, $caseSensitive = true)
return new static(Str::remove($search, $this->value, $caseSensitive));
}

/**
* Reverse the string.
*
* @return static
*/
public function reverse()
{
return new static(Str::reverse($this->value));
}

/**
* Repeat the string.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportStrTest.php
Expand Up @@ -418,6 +418,13 @@ public function testRemove()
$this->assertSame('Foobar', Str::remove(['f', '|'], 'Foo|bar'));
}

public function testReverse()
{
$this->assertSame('FooBar', Str::reverse('raBooF'));
$this->assertSame('Teniszütő', Str::reverse('őtüzsineT'));
$this->assertSame('❤MultiByte☆', Str::reverse('☆etyBitluM❤'));
}

public function testSnake()
{
$this->assertSame('laravel_p_h_p_framework', Str::snake('LaravelPHPFramework'));
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportStringableTest.php
Expand Up @@ -538,6 +538,13 @@ public function testRemove()
$this->assertSame('Foobar', (string) $this->stringable('Foo|bar')->remove(['f', '|']));
}

public function testReverse()
{
$this->assertSame('FooBar', (string) $this->stringable('raBooF')->reverse());
$this->assertSame('Teniszütő', (string) $this->stringable('őtüzsineT')->reverse());
$this->assertSame('❤MultiByte☆', (string) $this->stringable('☆etyBitluM❤')->reverse());
}

public function testSnake()
{
$this->assertSame('laravel_p_h_p_framework', (string) $this->stringable('LaravelPHPFramework')->snake());
Expand Down