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 Twig\Environment; |
17
|
|
|
use Twig\Extension\AbstractExtension; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* NEXT_MAJOR: Remove this class. |
21
|
|
|
* |
22
|
|
|
* @internal |
23
|
|
|
* |
24
|
|
|
* @deprecated since sonata-project/admin-bundle 3.x, to be removed in 4.0. |
25
|
|
|
* |
26
|
|
|
* This class is used to support `Sonata\AdminBundle\Twig\Extension\StringExtensions` when `sonata_admin.options.legacy_twig_text_extension` |
27
|
|
|
* is set to true and deprecated `twig/extensions` is not installed. It is copy of required function, which keep BC for `sonata_truncate` |
28
|
|
|
* twig filter until sonata-project/admin-bundle 4.0 where this filter will be dropped. |
29
|
|
|
*/ |
30
|
|
|
final class DeprecatedTextExtension extends AbstractExtension |
31
|
|
|
{ |
32
|
|
|
public function twigTruncateFilter(Environment $env, ?string $value, int $length = 30, bool $preserve = false, $separator = '...') |
33
|
|
|
{ |
34
|
|
|
if (\function_exists('mb_get_info')) { |
35
|
|
|
if (mb_strlen($value, $env->getCharset()) > $length) { |
36
|
|
|
if ($preserve) { |
37
|
|
|
// If breakpoint is on the last word, return the value without separator. |
38
|
|
|
if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) { |
39
|
|
|
return $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$length = $breakpoint; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator; |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
if (\strlen($value) > $length) { |
49
|
|
|
if ($preserve) { |
50
|
|
|
if (false !== ($breakpoint = strpos($value, ' ', $length))) { |
51
|
|
|
$length = $breakpoint; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return rtrim(substr($value, 0, $length)).$separator; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $value; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|