Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add Str::limit method #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,27 @@

return array_map('trim', $exploded);
}

/**
* Str::limit method truncates the given string to the specified length.
*
* This method will replace the truncated part with an ellipsis.
*
* @param string $subject
* @param ?int $limit Length of the string that will be returned
* @param ?string $end Ellipsis that will be added to the truncated string
*
* @return string
*/
public static function limit(string $subject, ?int $limit = 50, ?string $end = '...'): string
{
// If the string is smaller or equal to the limit we simply return it
if (mb_strwidth($subject, 'UTF-8') <= $limit) {
return $subject;
}

// If the string is longer than the limit we truncate it
// and add the given end to it.
return mb_strimwidth($subject, 0, $limit, '', 'UTF-8') . $end;

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.
}
}
22 changes: 22 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,26 @@ public function testTrimSplitRespectsLimit()
{
$this->assertSame(['foo', 'bar , baz'], Str::trimSplit(' foo ,bar , baz ', ',', 2));
}

public function testLimitWithSmallerString()
{
$this->assertSame('', Str::limit(''));
$this->assertSame('noop', Str::limit('noop'));
}

public function testLimitWithLongerStringAndSpecificLimit()
{
$this->assertSame(
'Lorem ipsu...',
Str::limit('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 10)
);
}

public function testLimitWithLongerStringAndSpecificEnd()
{
$this->assertSame(
'L (...)',
Str::limit('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 1, ' (...)')
);
}
}
Loading