Skip to content

Commit 7df3591

Browse files
committed
Merge branch 'master' into 3.1
# Conflicts: # src/redis/tests/RedisTest.php
2 parents 10f2b66 + 103f3a9 commit 7df3591

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

src/Str.php

+23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
namespace Hyperf\Stringable;
1313

14+
use Closure;
1415
use DateTimeInterface;
1516
use Hyperf\Collection\Arr;
1617
use Hyperf\Collection\Collection;
@@ -576,6 +577,10 @@ public static function replaceFirst(string $search, string $replace, string $sub
576577
*/
577578
public static function replaceLast(string $search, string $replace, string $subject): string
578579
{
580+
if ($search == '') {
581+
return $subject;
582+
}
583+
579584
$position = strrpos($subject, $search);
580585

581586
if ($position !== false) {
@@ -1006,6 +1011,24 @@ public static function replaceEnd($search, $replace, $subject)
10061011
return $subject;
10071012
}
10081013

1014+
/**
1015+
* Replace the patterns matching the given regular expression.
1016+
*
1017+
* @param string $pattern
1018+
* @param Closure|string $replace
1019+
* @param array|string $subject
1020+
* @param int $limit
1021+
* @return null|string|string[]
1022+
*/
1023+
public static function replaceMatches($pattern, $replace, $subject, $limit = -1)
1024+
{
1025+
if ($replace instanceof Closure) {
1026+
return preg_replace_callback($pattern, $replace, $subject, $limit);
1027+
}
1028+
1029+
return preg_replace($pattern, $replace, $subject, $limit);
1030+
}
1031+
10091032
public static function reverse($value): string
10101033
{
10111034
return implode(array_reverse(mb_str_split($value)));

src/Stringable.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,7 @@ public function replaceLast($search, $replace)
598598
*/
599599
public function replaceMatches($pattern, $replace, $limit = -1)
600600
{
601-
if ($replace instanceof Closure) {
602-
return new static(preg_replace_callback($pattern, $replace, $this->value, $limit));
603-
}
604-
605-
return new static(preg_replace($pattern, $replace, $this->value, $limit));
601+
return new static(Str::replaceMatches($pattern, $replace, $this->value, $limit));
606602
}
607603

608604
/**

tests/StrTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -912,4 +912,16 @@ public function testConvertCase()
912912
$this->assertSame($item[0], Str::convertCase(...$item[1]));
913913
}
914914
}
915+
916+
public function testReplaceLast()
917+
{
918+
$this->assertSame('Hello earth', Str::replaceLast('world', 'earth', 'Hello world'));
919+
$this->assertSame('Hello world', Str::replaceLast('', 'earth', 'Hello world'));
920+
}
921+
922+
public function testReplaceMatches()
923+
{
924+
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', 'http://', 'https://hyperf.io'));
925+
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', fn ($matches) => 'http://', 'https://hyperf.io'));
926+
}
915927
}

tests/StringableTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ public function testValueAndToString()
452452
$this->assertSame('foo', $this->stringable('foo')->toString());
453453
}
454454

455+
public function testReplaceMatches()
456+
{
457+
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', 'http://'));
458+
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', fn ($matches) => 'http://'));
459+
}
460+
455461
/**
456462
* @param string $string
457463
* @return Stringable

0 commit comments

Comments
 (0)