1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the TheAlternativeZurich/events project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Extension; |
13
|
|
|
|
14
|
|
|
use App\Enum\BooleanType; |
15
|
|
|
use App\Helper\DateTimeFormatter; |
16
|
|
|
use DateTime; |
17
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
18
|
|
|
use Twig\Environment; |
19
|
|
|
use Twig\Extension\AbstractExtension; |
20
|
|
|
use Twig\TwigFilter; |
21
|
|
|
|
22
|
|
|
class MyTwigExtension extends AbstractExtension |
23
|
|
|
{ |
24
|
|
|
private $translator; |
25
|
|
|
|
26
|
|
|
public function __construct(TranslatorInterface $translator) |
27
|
|
|
{ |
28
|
|
|
$this->translator = $translator; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* makes the filters available to twig. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function getFilters() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
new TwigFilter('dateFormat', [$this, 'dateFormatFilter']), |
40
|
|
|
new TwigFilter('dateTimeFormat', [$this, 'dateTimeFormatFilter']), |
41
|
|
|
new TwigFilter('booleanFormat', [$this, 'booleanFilter']), |
42
|
|
|
new TwigFilter('camelCaseToUnderscore', [$this, 'camelCaseToUnderscoreFilter']), |
43
|
|
|
new TwigFilter('truncate', [$this, 'truncateFilter'], ['needs_environment' => true]), |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $propertyName |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function camelCaseToUnderscoreFilter($propertyName) |
53
|
|
|
{ |
54
|
|
|
return mb_strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $propertyName)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param DateTime|null $date |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function dateFormatFilter($date) |
63
|
|
|
{ |
64
|
|
|
if ($date instanceof DateTime) { |
65
|
|
|
return $date->format(DateTimeFormatter::DATE_FORMAT); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return '-'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param DateTime|null $date |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function dateTimeFormatFilter($date) |
77
|
|
|
{ |
78
|
|
|
if ($date instanceof DateTime) { |
79
|
|
|
return $date->format(DateTimeFormatter::DATE_TIME_FORMAT); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return '-'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $value |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function booleanFilter($value) |
91
|
|
|
{ |
92
|
|
|
if ($value) { |
93
|
|
|
return BooleanType::getTranslationForValue(BooleanType::YES, $this->translator); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return BooleanType::getTranslationForValue(BooleanType::NO, $this->translator); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @source https://github.com/twigphp/Twig-extensions/blob/master/src/TextExtension.php |
101
|
|
|
*/ |
102
|
|
|
public function truncateFilter(Environment $env, $value, $length = 30, $preserve = false, $separator = '...') |
103
|
|
|
{ |
104
|
|
|
if (mb_strlen($value, $env->getCharset()) > $length) { |
105
|
|
|
if ($preserve) { |
106
|
|
|
// If breakpoint is on the last word, return the value without separator. |
107
|
|
|
if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) { |
108
|
|
|
return $value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$length = $breakpoint; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $value; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|