feature #35649 [String] Allow to keep the last word when truncating a text (franmomu)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[String] Allow to keep the last word when truncating a text

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #35567
| License       | MIT
| Doc PR        | -

The [truncate filter from twig/extensions](https://github.com/twigphp/Twig-extensions/blob/master/src/TextExtension.php#L36) has a `preserve` parameter to preserve whole words.

Since `twig/extensions` is deprecated and its alternative for `truncate` filter is the use of `u` filter, this PR adds preverse word functionality.

Commits
-------

1cfaeec378 [String] Allow to keep the last word when truncating a text
This commit is contained in:
Fabien Potencier 2020-02-21 08:43:56 +01:00
commit d33a483575
3 changed files with 16 additions and 6 deletions

View File

@ -620,7 +620,7 @@ abstract class AbstractString implements \JsonSerializable
/** /**
* @return static * @return static
*/ */
public function truncate(int $length, string $ellipsis = ''): self public function truncate(int $length, string $ellipsis = '', bool $cut = true): self
{ {
$stringLength = $this->length(); $stringLength = $this->length();
@ -634,6 +634,10 @@ abstract class AbstractString implements \JsonSerializable
$ellipsisLength = 0; $ellipsisLength = 0;
} }
if (!$cut) {
$length = $ellipsisLength + ($this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1) ?? $stringLength);
}
$str = $this->slice(0, $length - $ellipsisLength); $str = $this->slice(0, $length - $ellipsisLength);
return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str; return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;

View File

@ -7,9 +7,10 @@ CHANGELOG
* added the `AbstractString::reverse()` method * added the `AbstractString::reverse()` method
* made `AbstractString::width()` follow POSIX.1-2001 * made `AbstractString::width()` follow POSIX.1-2001
* added `LazyString` which provides memoizing stringable objects * added `LazyString` which provides memoizing stringable objects
* The component is not marked as `@experimental` anymore. * The component is not marked as `@experimental` anymore
* Added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance, * added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
depending of the input string UTF-8 compliancy. depending of the input string UTF-8 compliancy
* added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()`
5.0.0 5.0.0
----- -----

View File

@ -1405,9 +1405,9 @@ abstract class AbstractAsciiTestCase extends TestCase
/** /**
* @dataProvider provideTruncate * @dataProvider provideTruncate
*/ */
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis) public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $cut = true)
{ {
$instance = static::createFromString($origin)->truncate($length, $ellipsis); $instance = static::createFromString($origin)->truncate($length, $ellipsis, $cut);
$this->assertEquals(static::createFromString($expected), $instance); $this->assertEquals(static::createFromString($expected), $instance);
} }
@ -1417,12 +1417,17 @@ abstract class AbstractAsciiTestCase extends TestCase
return [ return [
['', '', 3, ''], ['', '', 3, ''],
['', 'foo', 0, '...'], ['', 'foo', 0, '...'],
['foo', 'foo', 0, '...', false],
['fo', 'foobar', 2, ''], ['fo', 'foobar', 2, ''],
['foobar', 'foobar', 10, ''], ['foobar', 'foobar', 10, ''],
['foobar', 'foobar', 10, '...', false],
['foo', 'foo', 3, '...'], ['foo', 'foo', 3, '...'],
['fo', 'foobar', 2, '...'], ['fo', 'foobar', 2, '...'],
['...', 'foobar', 3, '...'], ['...', 'foobar', 3, '...'],
['fo...', 'foobar', 5, '...'], ['fo...', 'foobar', 5, '...'],
['foobar...', 'foobar foo', 6, '...', false],
['foobar...', 'foobar foo', 7, '...', false],
['foobar foo...', 'foobar foo a', 10, '...', false],
]; ];
} }