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

Add a new Psl\Str\reverse() function #238

Merged
merged 14 commits into from
Oct 18, 2021
1 change: 1 addition & 0 deletions docs/component/str.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
- [replace_ci](./../../src/Psl/Str/replace_ci.php#L20)
- [replace_every](./../../src/Psl/Str/replace_every.php#L19)
- [replace_every_ci](./../../src/Psl/Str/replace_every_ci.php#L19)
- [reverse](./../../src/Psl/Str/reverse.php#L14)
- [search](./../../src/Psl/Str/search.php#L25)
- [search_ci](./../../src/Psl/Str/search_ci.php#L25)
- [search_last](./../../src/Psl/Str/search_last.php#L25)
Expand Down
1 change: 1 addition & 0 deletions src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ final class Loader
'Psl\Str\replace_ci',
'Psl\Str\replace_every',
'Psl\Str\replace_every_ci',
'Psl\Str\reverse',
'Psl\Str\search',
'Psl\Str\search_ci',
'Psl\Str\search_last',
Expand Down
20 changes: 20 additions & 0 deletions src/Psl/Str/reverse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Psl\Str;

use Psl\Vec;

/**
* Returns the given string reversed.
yivi marked this conversation as resolved.
Show resolved Hide resolved
*
* @psalm-pure
*/
function reverse(string $string, ?string $encoding = null): string
{
$chunks = chunk($string, encoding: $encoding);

/** @psalm-suppress ImpureFunctionCall */
return join(Vec\reverse($chunks), '');
}
31 changes: 31 additions & 0 deletions tests/unit/Str/ReverseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Psl\Tests\Unit\Str;

use PHPUnit\Framework\TestCase;
use Psl\Str;

final class ReverseTest extends TestCase
{

public function provideData(): array
{
return [
['Hello World', 'dlroW olleH'],
['héllö wôrld', 'dlrôw ölléh'],
['Iñigo Montoya', 'ayotnoM ogiñI'],
['某物', '物某' ],
['что-то', 'от-отч']
];
yivi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @dataProvider provideData
*/
public function testReverse(string $string, string $expected): void
{
static::assertSame(Str\reverse($string), $expected);
}
}