Skip to content

Commit

Permalink
Merge branch 'master' into 3.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/redis/tests/RedisTest.php
  • Loading branch information
limingxinleo committed Oct 20, 2023
2 parents 10f2b66 + 103f3a9 commit 7df3591
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace Hyperf\Stringable;

use Closure;
use DateTimeInterface;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
Expand Down Expand Up @@ -576,6 +577,10 @@ public static function replaceFirst(string $search, string $replace, string $sub
*/
public static function replaceLast(string $search, string $replace, string $subject): string
{
if ($search == '') {
return $subject;
}

$position = strrpos($subject, $search);

if ($position !== false) {
Expand Down Expand Up @@ -1006,6 +1011,24 @@ public static function replaceEnd($search, $replace, $subject)
return $subject;
}

/**
* Replace the patterns matching the given regular expression.
*
* @param string $pattern
* @param Closure|string $replace
* @param array|string $subject
* @param int $limit
* @return null|string|string[]
*/
public static function replaceMatches($pattern, $replace, $subject, $limit = -1)
{
if ($replace instanceof Closure) {
return preg_replace_callback($pattern, $replace, $subject, $limit);
}

return preg_replace($pattern, $replace, $subject, $limit);
}

public static function reverse($value): string
{
return implode(array_reverse(mb_str_split($value)));
Expand Down
6 changes: 1 addition & 5 deletions src/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,7 @@ public function replaceLast($search, $replace)
*/
public function replaceMatches($pattern, $replace, $limit = -1)
{
if ($replace instanceof Closure) {
return new static(preg_replace_callback($pattern, $replace, $this->value, $limit));
}

return new static(preg_replace($pattern, $replace, $this->value, $limit));
return new static(Str::replaceMatches($pattern, $replace, $this->value, $limit));
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,16 @@ public function testConvertCase()
$this->assertSame($item[0], Str::convertCase(...$item[1]));
}
}

public function testReplaceLast()
{
$this->assertSame('Hello earth', Str::replaceLast('world', 'earth', 'Hello world'));
$this->assertSame('Hello world', Str::replaceLast('', 'earth', 'Hello world'));
}

public function testReplaceMatches()
{
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', 'http://', 'https://hyperf.io'));
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', fn ($matches) => 'http://', 'https://hyperf.io'));
}
}
6 changes: 6 additions & 0 deletions tests/StringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ public function testValueAndToString()
$this->assertSame('foo', $this->stringable('foo')->toString());
}

public function testReplaceMatches()
{
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', 'http://'));
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', fn ($matches) => 'http://'));
}

/**
* @param string $string
* @return Stringable
Expand Down

0 comments on commit 7df3591

Please sign in to comment.