From aec63f994490a0fd96bfb059cbf2a2d9872a37be Mon Sep 17 00:00:00 2001 From: Romain BERTOLUCCI Date: Tue, 27 Aug 2024 15:11:49 +0200 Subject: [PATCH] Add function addCharAtInterval() to Str class --- src/Illuminate/Support/Str.php | 13 +++++++++++++ src/Illuminate/Support/Stringable.php | 12 ++++++++++++ tests/Support/SupportStrTest.php | 6 ++++++ tests/Support/SupportStringableTest.php | 6 ++++++ 4 files changed, 37 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 52867d89e473..8396ca6d6a9e 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -110,6 +110,19 @@ public static function afterLast($subject, $search) return substr($subject, $position + strlen($search)); } + /** + * Return the given string with a custom character every X character. + * + * @param string $subject + * @param string $char + * @param int $length + * @return string + */ + public static function addCharAtInterval($subject, $char, $length = 1) + { + return implode($char, str_split($subject, $length)); + } + /** * Transliterate a UTF-8 value to ASCII. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 6ae57e032066..01c5a5a546af 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -45,6 +45,18 @@ public function after($search) return new static(Str::after($this->value, $search)); } + /** + * Return the given string with a custom character every X character. + * + * @param $char + * @param $length + * @return static + */ + public function addCharAtInterval($char, $length = 1) + { + return new static(Str::addCharAtInterval($this->value, $char, $length)); + } + /** * Return the remainder of a string after the last occurrence of a given value. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 0901ce0b3f8e..bfda230001b6 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -12,6 +12,12 @@ class SupportStrTest extends TestCase { + public function testItAddsACustomCharacterEveryXTimes(): void + { + $this->assertSame('12 34 56 78 90', Str::addCharAtInterval('1234567890', ' ', 2)); + $this->assertSame('123-456-789', Str::addCharAtInterval('123456789', '-', 3)); + } + public function testStringCanBeLimitedByWords(): void { $this->assertSame('Taylor...', Str::words('Taylor Otwell', 1)); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index e28dbddc536d..7b48e5865f5e 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -29,6 +29,12 @@ class_basename(static::class), ); } + public function testItAddsACustomCharacterEveryXTimes() + { + $this->assertSame('12 34 56 78 90', (string) $this->stringable('1234567890')->addCharAtInterval(' ', 2)); + $this->assertSame('123-456-789', (string) $this->stringable('123456789')->addCharAtInterval('-', 3)); + } + public function testIsAscii() { $this->assertTrue($this->stringable('A')->isAscii());