|
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 Slick\Template\Extension\IpsumLorenGenerator; |
|
15
|
|
|
use Twig\Extension\AbstractExtension; |
|
16
|
|
|
use Twig\TwigFilter; |
|
17
|
|
|
use Twig\TwigFunction; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* TwigTextExtension |
|
21
|
|
|
* |
|
22
|
|
|
* @package Slick\Template\Extension\Twig |
|
23
|
|
|
*/ |
|
24
|
|
|
final class TwigTextExtension extends AbstractExtension |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Retrieves the filters of this extension. |
|
28
|
|
|
* |
|
29
|
|
|
* @return array<TwigFilter> The filters. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getFilters(): array |
|
32
|
|
|
{ |
|
33
|
|
|
return [ |
|
34
|
|
|
new TwigFilter('truncate', [$this, 'truncate']), |
|
35
|
|
|
new TwigFilter('wordwrap', [$this, 'wordwrap']) |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getFunctions(): array |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
new TwigFunction('generateWords', [$this, 'generateWords']), |
|
43
|
|
|
new TwigFunction('generateSentences', [$this, 'generateSentences']), |
|
44
|
|
|
new TwigFunction('generateParagraphs', [$this, 'generateParagraphs']), |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Generate random words using IpsumLorenGenerator. |
|
50
|
|
|
* |
|
51
|
|
|
* @param int $count The number of words to generate. Default is 1. |
|
52
|
|
|
* @return string The generated words. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function generateWords(int $count = 1): string |
|
55
|
|
|
{ |
|
56
|
|
|
$generator = new IpsumLorenGenerator(); |
|
57
|
|
|
return $generator->words($count); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Generates a specified number of sentences using IpsumLorenGenerator. |
|
62
|
|
|
* |
|
63
|
|
|
* @param int $count The number of sentences to generate (default is 1). |
|
64
|
|
|
* @return string The generated sentences. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function generateSentences(int $count = 1): string |
|
67
|
|
|
{ |
|
68
|
|
|
$generator = new IpsumLorenGenerator(); |
|
69
|
|
|
return $generator->sentences($count); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Generates a specified number of paragraphs using IpsumLorenGenerator. |
|
74
|
|
|
* |
|
75
|
|
|
* @param int $count The number of paragraphs to generate (default is 1). |
|
76
|
|
|
* @return string The generated paragraphs. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function generateParagraphs(int $count = 1): string |
|
79
|
|
|
{ |
|
80
|
|
|
$generator = new IpsumLorenGenerator(); |
|
81
|
|
|
return $generator->paragraphs($count); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Truncates a given string to a specified length and appends a terminator. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $value The string to truncate. |
|
88
|
|
|
* @param int $len The maximum length of the truncated string. Default is 75. |
|
89
|
|
|
* @param string $ter The terminator to append to the truncated string. Default is "...". |
|
90
|
|
|
* @return string The truncated string. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function truncate(string $value, int $len = 75, string $ter = "..."): string |
|
93
|
|
|
{ |
|
94
|
|
|
$truncated = $value; |
|
95
|
|
|
$length = $this->preserveBreakpoint($value, $len); |
|
96
|
|
|
if (mb_strlen($value) > $length) { |
|
97
|
|
|
$truncated = rtrim(mb_substr($value, 0, $length)). $ter; |
|
98
|
|
|
} |
|
99
|
|
|
return $truncated; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Wrap a string into lines of a specified length. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $value The input string to be wrapped. |
|
106
|
|
|
* @param int $len The number of characters at which the string will be wrapped. |
|
107
|
|
|
* @param string $break The character used to break the lines. |
|
108
|
|
|
* @return string The wrapped string. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function wordwrap(string $value, int $len = 75, string $break = "\n"): string |
|
111
|
|
|
{ |
|
112
|
|
|
return wordwrap($value, $len, $break); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns the length to preserve the breakpoint for truncating a given string. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $value The string to calculate the breakpoint for. |
|
119
|
|
|
* @param int $length The maximum length of the truncated string. |
|
120
|
|
|
* @return int The length to preserve the breakpoint. |
|
121
|
|
|
*/ |
|
122
|
|
|
private function preserveBreakpoint(string $value, int $length): int |
|
123
|
|
|
{ |
|
124
|
|
|
if (strlen($value) <= $length) { |
|
125
|
|
|
return $length; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$stringLength = mb_strlen($value, 'UTF-8'); |
|
129
|
|
|
$breakpoint = $stringLength > $length |
|
130
|
|
|
? mb_strpos($value, ' ', $length, 'UTF-8') |
|
131
|
|
|
: $stringLength; |
|
132
|
|
|
|
|
133
|
|
|
$length = false === $breakpoint ? mb_strlen($value) : $breakpoint; |
|
|
|
|
|
|
134
|
|
|
return (int) $length; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|