Skip to content

[String] Add TruncateMode mode to truncate methods #57243

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

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
8 changes: 8 additions & 0 deletions UPGRADE-7.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ Yaml
----

* Deprecate parsing duplicate mapping keys whose value is `null`

String
------

* `truncate` method now also accept `TruncateMode` enum instead of a boolean:
* `TruncateMode::Char` is equivalent to `true` value ;
* `TruncateMode::WordAfter` is equivalent to `true` value ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `TruncateMode::WordAfter` is equivalent to `true` value ;
* `TruncateMode::WordAfter` is equivalent to `false` value ;

* `TruncateMode::Word` is a new mode that will cut the sentence on the last word before the limit is reached.
13 changes: 11 additions & 2 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public function trimSuffix($suffix): static
return $str;
}

public function truncate(int $length, string $ellipsis = '', bool $cut = true): static
public function truncate(int $length, string $ellipsis = '', bool|TruncateMode $cut = TruncateMode::Char): static
{
$stringLength = $this->length();

Expand All @@ -619,7 +619,8 @@ public function truncate(int $length, string $ellipsis = '', bool $cut = true):
$ellipsisLength = 0;
}

if (!$cut) {
$desiredLength = $length;
if (TruncateMode::WordAfter === $cut || TruncateMode::WordBefore === $cut || !$cut) {
if (null === $length = $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) {
return clone $this;
}
Expand All @@ -629,6 +630,14 @@ public function truncate(int $length, string $ellipsis = '', bool $cut = true):

$str = $this->slice(0, $length - $ellipsisLength);

if (TruncateMode::WordBefore === $cut) {
if (0 === $ellipsisLength && $desiredLength === $this->indexOf([' ', "\r", "\n", "\t"], $length)) {
return $str;
}

$str = $str->beforeLast([' ', "\r", "\n", "\t"]);
}

return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add `TruncateMode` enum to handle more truncate methods

7.1
---

Expand Down
15 changes: 13 additions & 2 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\String\ByteString;
use Symfony\Component\String\CodePointString;
use Symfony\Component\String\Exception\InvalidArgumentException;
use Symfony\Component\String\TruncateMode;
use Symfony\Component\String\UnicodeString;

abstract class AbstractAsciiTestCase extends TestCase
Expand Down Expand Up @@ -1500,22 +1501,24 @@ public static function providePadStart()
/**
* @dataProvider provideTruncate
*/
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $cut = true)
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool|TruncateMode $cut = TruncateMode::Char)
{
$instance = static::createFromString($origin)->truncate($length, $ellipsis, $cut);

$this->assertEquals(static::createFromString($expected), $instance);
}

public static function provideTruncate()
public static function provideTruncate(): array
{
return [
['', '', 3, ''],
['', 'foo', 0, '...'],
['foo', 'foo', 0, '...', false],
['foo', 'foo', 0, '...', TruncateMode::WordAfter],
['fo', 'foobar', 2, ''],
['foobar', 'foobar', 10, ''],
['foobar', 'foobar', 10, '...', false],
['foobar', 'foobar', 10, '...', TruncateMode::WordAfter],
['foo', 'foo', 3, '...'],
['fo', 'foobar', 2, '...'],
['...', 'foobar', 3, '...'],
Expand All @@ -1524,6 +1527,14 @@ public static function provideTruncate()
['foobar...', 'foobar foo', 7, '...', false],
['foobar foo...', 'foobar foo a', 10, '...', false],
['foobar foo aar', 'foobar foo aar', 12, '...', false],
['foobar...', 'foobar foo', 6, '...', TruncateMode::WordAfter],
['foobar...', 'foobar foo', 7, '...', TruncateMode::WordAfter],
['foobar foo...', 'foobar foo a', 10, '...', TruncateMode::WordAfter],
['foobar foo aar', 'foobar foo aar', 12, '...', TruncateMode::WordAfter],
['foobar foo', 'foobar foo aar', 10, '', TruncateMode::WordBefore],
['foobar...', 'foobar foo aar', 10, '...', TruncateMode::WordBefore],
['Lorem ipsum', 'Lorem ipsum dolor sit amet', 14, '', TruncateMode::WordBefore],
['Lorem...', 'Lorem ipsum dolor sit amet', 10, '...', TruncateMode::WordBefore],
];
}

Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/String/TruncateMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\String;

enum TruncateMode
{
/**
* Will cut exactly at given length.
*
* Length: 14
* Source: Lorem ipsum dolor sit amet
* Output: Lorem ipsum do
*/
case Char;

/**
* Returns the string up to the last complete word containing the specified length.
*
* Length: 14
* Source: Lorem ipsum dolor sit amet
* Output: Lorem ipsum
*/
case WordBefore;

/**
* Returns the string up to the complete word after or at the given length.
*
* Length: 14
* Source: Lorem ipsum dolor sit amet
* Output: Lorem ipsum dolor
*/
case WordAfter;
}
Loading