Skip to content

Commit 141e223

Browse files
authored
[8.x] Add a simple Str::repeat() helper function (#36887)
* Add Str::repeat() function * Fix styleci issues * Fix styleci issues (again)
1 parent bad9e1b commit 141e223

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,18 @@ public static function random($length = 16)
491491
return $string;
492492
}
493493

494+
/**
495+
* Repeat the given string.
496+
*
497+
* @param string $string
498+
* @param int $times
499+
* @return string
500+
*/
501+
public static function repeat(string $string, int $times)
502+
{
503+
return str_repeat($string, $times);
504+
}
505+
494506
/**
495507
* Replace a given value in the string sequentially with an array.
496508
*

src/Illuminate/Support/Stringable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,17 @@ public function remove($search, $caseSensitive = true)
479479
return new static(Str::remove($search, $this->value, $caseSensitive));
480480
}
481481

482+
/**
483+
* Repeat the string.
484+
*
485+
* @param int $times
486+
* @return static
487+
*/
488+
public function repeat(int $times)
489+
{
490+
return new static(Str::repeat($this->value, $times));
491+
}
492+
482493
/**
483494
* Replace the given value in the given string.
484495
*

tests/Support/SupportStrTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ public function testMarkdown()
531531
$this->assertSame("<p><em>hello world</em></p>\n", Str::markdown('*hello world*'));
532532
$this->assertSame("<h1>hello world</h1>\n", Str::markdown('# hello world'));
533533
}
534+
535+
public function testRepeat()
536+
{
537+
$this->assertSame('aaaaa', Str::repeat('a', 5));
538+
$this->assertSame('', Str::repeat('', 5));
539+
}
534540
}
535541

536542
class StringableObjectStub

tests/Support/SupportStringableTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,10 @@ public function testMarkdown()
602602
$this->assertEquals("<p><em>hello world</em></p>\n", $this->stringable('*hello world*')->markdown());
603603
$this->assertEquals("<h1>hello world</h1>\n", $this->stringable('# hello world')->markdown());
604604
}
605+
606+
public function testRepeat()
607+
{
608+
$this->assertSame('aaaaa', (string) $this->stringable('a')->repeat(5));
609+
$this->assertSame('', (string) $this->stringable('')->repeat(5));
610+
}
605611
}

0 commit comments

Comments
 (0)