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 function Sauls\Component\Helper\base64_url_decode; |
16
|
|
|
use function Sauls\Component\Helper\base64_url_encode; |
17
|
|
|
use function Sauls\Component\Helper\count_sentences; |
18
|
|
|
use function Sauls\Component\Helper\count_words; |
19
|
|
|
use function Sauls\Component\Helper\countdown; |
20
|
|
|
use function Sauls\Component\Helper\elapsed_time; |
21
|
|
|
use function Sauls\Component\Helper\explode_using_multi_delimiters; |
22
|
|
|
use function Sauls\Component\Helper\string_camelize; |
23
|
|
|
use function Sauls\Component\Helper\string_snakeify; |
24
|
|
|
use function Sauls\Component\Helper\truncate; |
25
|
|
|
use function Sauls\Component\Helper\truncate_html; |
26
|
|
|
use function Sauls\Component\Helper\truncate_html_sentences; |
27
|
|
|
use function Sauls\Component\Helper\truncate_html_worlds; |
28
|
|
|
use function Sauls\Component\Helper\truncate_sentences; |
29
|
|
|
use function Sauls\Component\Helper\truncate_words; |
30
|
|
|
use Twig\Extension\AbstractExtension; |
31
|
|
|
|
32
|
|
|
class HelpersTwigExtension extends AbstractExtension |
33
|
|
|
{ |
34
|
15 |
|
public function getFilters() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
15 |
|
new \Twig_SimpleFilter('elapsed_time', [$this, 'elapsedTime']), |
38
|
15 |
|
new \Twig_SimpleFilter('countdown', [$this, 'countdown']), |
39
|
15 |
|
new \Twig_SimpleFilter('camelize', [$this, 'camelize']), |
40
|
15 |
|
new \Twig_SimpleFilter('snakeify', [$this, 'snakeify']), |
41
|
15 |
|
new \Twig_SimpleFilter('multi_split', [$this, 'multiSplit']), |
42
|
15 |
|
new \Twig_SimpleFilter('base64_url_encode', [$this, 'base64UrlEncode']), |
43
|
15 |
|
new \Twig_SimpleFilter('base64_url_decode', [$this, 'base64UrlDecode']), |
44
|
15 |
|
new \Twig_SimpleFilter('count_words', [$this, 'countWords']), |
45
|
15 |
|
new \Twig_SimpleFilter('count_sentences', [$this, 'countSentences']), |
46
|
15 |
|
new \Twig_SimpleFilter('truncate', [$this, 'truncate']), |
47
|
15 |
|
new \Twig_SimpleFilter('truncate_words', [$this, 'truncateWords']), |
48
|
15 |
|
new \Twig_SimpleFilter('truncate_sentences', [$this, 'truncateSentences']), |
49
|
15 |
|
new \Twig_SimpleFilter('truncate_html', [$this, 'truncateHtml']), |
50
|
15 |
|
new \Twig_SimpleFilter('truncate_html_words', [$this, 'truncateHtmlWords']), |
51
|
15 |
|
new \Twig_SimpleFilter('truncate_html_sentences', [$this, 'truncateHtmlSentences']), |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
1 |
|
public function elapsedTime($date, array $labels = [], $format = ELAPSED_TIME_FORMAT_FULL): string |
59
|
|
|
{ |
60
|
1 |
|
return elapsed_time($date, $labels, $format); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
1 |
|
public function countdown($dateFrom = 'now', $dateTo, string $format = '%s%02d:%02d:%02d'): string |
67
|
|
|
{ |
68
|
1 |
|
return countdown($dateFrom, $dateTo, $format); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
1 |
|
public function camelize(string $value): string |
75
|
|
|
{ |
76
|
1 |
|
return string_camelize($value); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
1 |
|
public function snakeify(string $value): string |
83
|
|
|
{ |
84
|
1 |
|
return string_snakeify($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
1 |
|
public function multiSplit(string $value, array $delimiters = ['.']): array |
91
|
|
|
{ |
92
|
1 |
|
return explode_using_multi_delimiters($delimiters, $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws \Exception |
97
|
|
|
*/ |
98
|
1 |
|
public function base64UrlEncode(string $value): string |
99
|
|
|
{ |
100
|
1 |
|
return base64_url_encode($value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @throws \Exception |
105
|
|
|
*/ |
106
|
1 |
|
public function base64UrlDecode(string $value): string |
107
|
|
|
{ |
108
|
1 |
|
return base64_url_decode($value); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function countWords(string $value): string |
112
|
|
|
{ |
113
|
1 |
|
return count_words($value); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function countSentences(string $value): string |
117
|
|
|
{ |
118
|
1 |
|
return count_sentences($value); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
public function truncate(string $value, int $length, string $suffix = '...'): string |
122
|
|
|
{ |
123
|
1 |
|
return truncate($value, $length, $suffix); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function truncateWords(string $value, int $count, string $suffix = '...'): string |
127
|
|
|
{ |
128
|
1 |
|
return truncate_words($value, $count, $suffix); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function truncateSentences(string $value, int $length, string $suffix = '...'): string |
132
|
|
|
{ |
133
|
1 |
|
return truncate_sentences($value, $length, $suffix); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function truncateHtml(string $value, int $length, string $suffix = '...'): string |
137
|
|
|
{ |
138
|
1 |
|
return truncate_html($value, $length, $suffix); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
public function truncateHtmlWords(string $value, int $count, string $suffix = '...'): string |
142
|
|
|
{ |
143
|
1 |
|
return truncate_html_worlds($value, $count, $suffix); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function truncateHtmlSentences(string $value, int $count, string $suffix = '...'): string |
147
|
|
|
{ |
148
|
1 |
|
return truncate_html_sentences($value, $count, $suffix); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|