Skip to content

Commit

Permalink
[8.x] Add reverse method to Str (#39816)
Browse files Browse the repository at this point in the history
* Add Str::reverse

* Add Stringable::reverse
  • Loading branch information
netpok committed Nov 29, 2021
1 parent 606ac0a commit a570b71
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
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

0 comments on commit a570b71

Please sign in to comment.