1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of template |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Template\Extension\Twig; |
13
|
|
|
|
14
|
|
|
use Twig\Extension\AbstractExtension; |
15
|
|
|
use Twig\TwigFilter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* TwigTextExtension |
19
|
|
|
* |
20
|
|
|
* @package Slick\Template\Extension\Twig |
21
|
|
|
*/ |
22
|
|
|
final class TwigTextExtension extends AbstractExtension |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Retrieves the filters of this extension. |
26
|
|
|
* |
27
|
|
|
* @return array<TwigFilter> The filters. |
28
|
|
|
*/ |
29
|
|
|
public function getFilters(): array |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
new TwigFilter('truncate', [$this, 'truncate']), |
33
|
|
|
new TwigFilter('wordwrap', [$this, 'wordwrap']), |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Truncates a given string to a specified length and appends a terminator. |
39
|
|
|
* |
40
|
|
|
* @param string $value The string to truncate. |
41
|
|
|
* @param int $len The maximum length of the truncated string. Default is 75. |
42
|
|
|
* @param string $ter The terminator to append to the truncated string. Default is "...". |
43
|
|
|
* @return string The truncated string. |
44
|
|
|
*/ |
45
|
|
|
public function truncate(string $value, int $len = 75, string $ter = "..."): string |
46
|
|
|
{ |
47
|
|
|
$truncated = $value; |
48
|
|
|
$length = $this->preserveBreakpoint($value, $len); |
49
|
|
|
if (mb_strlen($value) > $length) { |
50
|
|
|
$truncated = rtrim(mb_substr($value, 0, $length)). $ter; |
51
|
|
|
} |
52
|
|
|
return $truncated; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Wrap a string into lines of a specified length. |
57
|
|
|
* |
58
|
|
|
* @param string $value The input string to be wrapped. |
59
|
|
|
* @param int $len The number of characters at which the string will be wrapped. |
60
|
|
|
* @param string $break The character used to break the lines. |
61
|
|
|
* @return string The wrapped string. |
62
|
|
|
*/ |
63
|
|
|
public function wordwrap(string $value, int $len = 75, string $break = "\n"): string |
64
|
|
|
{ |
65
|
|
|
return wordwrap($value, $len, $break, false); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the length to preserve the breakpoint for truncating a given string. |
70
|
|
|
* |
71
|
|
|
* @param string $value The string to calculate the breakpoint for. |
72
|
|
|
* @param int $length The maximum length of the truncated string. |
73
|
|
|
* @return int The length to preserve the breakpoint. |
74
|
|
|
*/ |
75
|
|
|
private function preserveBreakpoint(string $value, int $length): int |
76
|
|
|
{ |
77
|
|
|
if (strlen($value) <= $length) { |
78
|
|
|
return $length; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$breakpoint = mb_strpos($value, ' ', $length); |
82
|
|
|
$length = false === $breakpoint ? mb_strlen($value) : $breakpoint; |
83
|
|
|
return (int) $length; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|