diff --git a/src/Tempest/Support/src/StringHelper.php b/src/Tempest/Support/src/StringHelper.php index b94ad3c07..0193b6522 100644 --- a/src/Tempest/Support/src/StringHelper.php +++ b/src/Tempest/Support/src/StringHelper.php @@ -657,20 +657,20 @@ public function excerpt(int $from, int $to, bool $asArray = false): self|ArrayHe } /** - * Limits the number of characters of the instance. + * Truncates the instance to the specified amount of characters. * * ### Example * ```php - * str('Lorem ipsum')->limit(5, end: '...'); // Lorem... + * str('Lorem ipsum')->truncate(5, end: '...'); // Lorem... * ``` */ - public function limit(int $characters, string $end = ''): self + public function truncate(int $characters, string $end = ''): self { if (mb_strwidth($this->string, 'UTF-8') <= $characters) { return $this; } - return new self(rtrim(mb_strimwidth($this->string, 0, $characters, '', 'UTF-8')) . $end); + return new self(rtrim(mb_strimwidth($this->string, 0, $characters, encoding: 'UTF-8')) . $end); } /** diff --git a/src/Tempest/Support/tests/StringHelperTest.php b/src/Tempest/Support/tests/StringHelperTest.php index 05405b5b9..0d8fc114b 100644 --- a/src/Tempest/Support/tests/StringHelperTest.php +++ b/src/Tempest/Support/tests/StringHelperTest.php @@ -484,12 +484,12 @@ public function test_start(): void public function test_limit(): void { - $this->assertSame('Lorem', str('Lorem ipsum')->limit(5)->toString()); - $this->assertSame('Lorem...', str('Lorem ipsum')->limit(5, end: '...')->toString()); - $this->assertSame('...', str('Lorem ipsum')->limit(0, end: '...')->toString()); - $this->assertSame('L...', str('Lorem ipsum')->limit(1, end: '...')->toString()); - $this->assertSame('Lorem ipsum', str('Lorem ipsum')->limit(100)->toString()); - $this->assertSame('Lorem ipsum', str('Lorem ipsum')->limit(100, end: '...')->toString()); + $this->assertSame('Lorem', str('Lorem ipsum')->truncate(5)->toString()); + $this->assertSame('Lorem...', str('Lorem ipsum')->truncate(5, end: '...')->toString()); + $this->assertSame('...', str('Lorem ipsum')->truncate(0, end: '...')->toString()); + $this->assertSame('L...', str('Lorem ipsum')->truncate(1, end: '...')->toString()); + $this->assertSame('Lorem ipsum', str('Lorem ipsum')->truncate(100)->toString()); + $this->assertSame('Lorem ipsum', str('Lorem ipsum')->truncate(100, end: '...')->toString()); } public function test_substr(): void