1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Twig\Extension; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\String\UnicodeString as SymfonyUnicodeString; |
17
|
|
|
use Twig\Environment; |
18
|
|
|
use Twig\Extension\AbstractExtension; |
19
|
|
|
use Twig\Extensions\TextExtension; |
20
|
|
|
use Twig\TwigFilter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* NEXT_MAJOR: Remove this class. |
24
|
|
|
* |
25
|
|
|
* Decorates `Twig\Extra\String\StringExtension` in order to provide the `$cut` |
26
|
|
|
* argument for `Symfony\Component\String\UnicodeString::truncate()`. |
27
|
|
|
* This class must be removed when the component ships this feature. |
28
|
|
|
* |
29
|
|
|
* @internal |
30
|
|
|
* |
31
|
|
|
* @see https://github.com/symfony/symfony/pull/35649 |
32
|
|
|
* |
33
|
|
|
* @author Javier Spagnoletti <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
final class StringExtension extends AbstractExtension |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var TextExtension |
39
|
|
|
*/ |
40
|
|
|
private $legacyExtension; |
41
|
|
|
|
42
|
|
|
public function __construct(?TextExtension $legacyExtension = null) |
43
|
|
|
{ |
44
|
|
|
$this->legacyExtension = $legacyExtension; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return TwigFilter[] |
49
|
|
|
*/ |
50
|
|
|
public function getFilters(): array |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
new TwigFilter('sonata_truncate', [$this, 'deprecatedTruncate'], ['needs_environment' => true]), |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return SymfonyUnicodeString|string |
59
|
|
|
*/ |
60
|
|
|
public function deprecatedTruncate(Environment $env, ?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...') |
61
|
|
|
{ |
62
|
|
|
@trigger_error( |
|
|
|
|
63
|
|
|
'The "sonata_truncate" twig filter is deprecated' |
64
|
|
|
.' since sonata-project/admin-bundle 3.x and will be removed in 4.0. Use "u.truncate" instead.', |
65
|
|
|
E_USER_DEPRECATED |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if (null !== $this->legacyExtension) { |
69
|
|
|
return twig_truncate_filter($env, $text, $length, $preserve, $ellipsis); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->legacyTruncteWithUnicodeString($text, $length, $preserve, $ellipsis); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* NEXT_MAJOR: Fix the arguments in order to respect the signature at `UnicodeString::truncate()`. |
77
|
|
|
*/ |
78
|
|
|
public function legacyTruncteWithUnicodeString(?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...'): SymfonyUnicodeString |
79
|
|
|
{ |
80
|
|
|
return (new SymfonyUnicodeString($text ?? ''))->truncate($length, $ellipsis, $preserve); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: