1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/components-bundle package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2018 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Bundle\Components\Twig\Extension; |
14
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
use Twig\Extension\AbstractExtension; |
17
|
|
|
use Twig\TwigFilter; |
18
|
|
|
|
19
|
|
|
use function Sauls\Component\Helper\base64_url_decode; |
20
|
|
|
use function Sauls\Component\Helper\base64_url_encode; |
21
|
|
|
use function Sauls\Component\Helper\count_sentences; |
22
|
|
|
use function Sauls\Component\Helper\count_words; |
23
|
|
|
use function Sauls\Component\Helper\countdown; |
24
|
|
|
use function Sauls\Component\Helper\elapsed_time; |
25
|
|
|
use function Sauls\Component\Helper\explode_using_multi_delimiters; |
26
|
|
|
use function Sauls\Component\Helper\string_camelize; |
27
|
|
|
use function Sauls\Component\Helper\string_snakeify; |
28
|
|
|
use function Sauls\Component\Helper\truncate; |
29
|
|
|
use function Sauls\Component\Helper\truncate_html; |
30
|
|
|
use function Sauls\Component\Helper\truncate_html_sentences; |
31
|
|
|
use function Sauls\Component\Helper\truncate_html_worlds; |
32
|
|
|
use function Sauls\Component\Helper\truncate_sentences; |
33
|
|
|
use function Sauls\Component\Helper\truncate_words; |
34
|
|
|
|
35
|
|
|
class HelpersTwigExtension extends AbstractExtension |
36
|
|
|
{ |
37
|
15 |
|
public function getFilters() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
15 |
|
new TwigFilter('elapsed_time', [$this, 'elapsedTime']), |
41
|
15 |
|
new TwigFilter('countdown', [$this, 'countdown']), |
42
|
15 |
|
new TwigFilter('camelize', [$this, 'camelize']), |
43
|
15 |
|
new TwigFilter('snakeify', [$this, 'snakeify']), |
44
|
15 |
|
new TwigFilter('multi_split', [$this, 'multiSplit']), |
45
|
15 |
|
new TwigFilter('base64_url_encode', [$this, 'base64UrlEncode']), |
46
|
15 |
|
new TwigFilter('base64_url_decode', [$this, 'base64UrlDecode']), |
47
|
15 |
|
new TwigFilter('count_words', [$this, 'countWords']), |
48
|
15 |
|
new TwigFilter('count_sentences', [$this, 'countSentences']), |
49
|
15 |
|
new TwigFilter('truncate', [$this, 'truncate']), |
50
|
15 |
|
new TwigFilter('truncate_words', [$this, 'truncateWords']), |
51
|
15 |
|
new TwigFilter('truncate_sentences', [$this, 'truncateSentences']), |
52
|
15 |
|
new TwigFilter('truncate_html', [$this, 'truncateHtml']), |
53
|
15 |
|
new TwigFilter('truncate_html_words', [$this, 'truncateHtmlWords']), |
54
|
15 |
|
new TwigFilter('truncate_html_sentences', [$this, 'truncateHtmlSentences']), |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws Exception |
60
|
|
|
*/ |
61
|
1 |
|
public function elapsedTime($date, array $labels = [], $format = ELAPSED_TIME_FORMAT_FULL): string |
62
|
|
|
{ |
63
|
1 |
|
return elapsed_time($date, $labels, $format); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws Exception |
68
|
|
|
*/ |
69
|
1 |
|
public function countdown($dateFrom, $dateTo, string $format = '%s%02d:%02d:%02d'): string |
70
|
|
|
{ |
71
|
1 |
|
return countdown($dateFrom ?? 'now', $dateTo, $format); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws Exception |
76
|
|
|
*/ |
77
|
1 |
|
public function camelize(string $value): string |
78
|
|
|
{ |
79
|
1 |
|
return string_camelize($value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @throws Exception |
84
|
|
|
*/ |
85
|
1 |
|
public function snakeify(string $value): string |
86
|
|
|
{ |
87
|
1 |
|
return string_snakeify($value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws Exception |
92
|
|
|
*/ |
93
|
1 |
|
public function multiSplit(string $value, array $delimiters = ['.']): array |
94
|
|
|
{ |
95
|
1 |
|
return explode_using_multi_delimiters($delimiters, $value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws Exception |
100
|
|
|
*/ |
101
|
1 |
|
public function base64UrlEncode(string $value): string |
102
|
|
|
{ |
103
|
1 |
|
return base64_url_encode($value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @throws Exception |
108
|
|
|
*/ |
109
|
1 |
|
public function base64UrlDecode(string $value): string |
110
|
|
|
{ |
111
|
1 |
|
return base64_url_decode($value); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
public function countWords(string $value): string |
115
|
|
|
{ |
116
|
1 |
|
return count_words($value); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
public function countSentences(string $value): string |
120
|
|
|
{ |
121
|
1 |
|
return count_sentences($value); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
public function truncate(string $value, int $length, string $suffix = '...'): string |
125
|
|
|
{ |
126
|
1 |
|
return truncate($value, $length, $suffix); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function truncateWords(string $value, int $count, string $suffix = '...'): string |
130
|
|
|
{ |
131
|
1 |
|
return truncate_words($value, $count, $suffix); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function truncateSentences(string $value, int $length, string $suffix = '...'): string |
135
|
|
|
{ |
136
|
1 |
|
return truncate_sentences($value, $length, $suffix); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function truncateHtml(string $value, int $length, string $suffix = '...'): string |
140
|
|
|
{ |
141
|
1 |
|
return truncate_html($value, $length, $suffix); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
public function truncateHtmlWords(string $value, int $count, string $suffix = '...'): string |
145
|
|
|
{ |
146
|
1 |
|
return truncate_html_worlds($value, $count, $suffix); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
public function truncateHtmlSentences(string $value, int $count, string $suffix = '...'): string |
150
|
|
|
{ |
151
|
1 |
|
return truncate_html_sentences($value, $count, $suffix); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|