|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the vseth-semesterly-reports 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\Form\Type\SemesterType; |
|
16
|
|
|
use DateTime; |
|
17
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
18
|
|
|
use Twig\Extension\AbstractExtension; |
|
19
|
|
|
use Twig\TwigFilter; |
|
20
|
|
|
|
|
21
|
|
|
class TwigExtension extends AbstractExtension |
|
22
|
|
|
{ |
|
23
|
|
|
private $translator; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(TranslatorInterface $translator) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->translator = $translator; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* makes the filters available to twig. |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getFilters() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
new TwigFilter('dateFormat', [$this, 'dateFormatFilter']), |
|
39
|
|
|
new TwigFilter('timeFormat', [$this, 'timeFormatFilter']), |
|
40
|
|
|
new TwigFilter('dateTimeFormat', [$this, 'dateTimeFilter']), |
|
41
|
|
|
new TwigFilter('booleanFormat', [$this, 'booleanFilter']), |
|
42
|
|
|
new TwigFilter('semesterFormat', [$this, 'semesterFilter']), |
|
43
|
|
|
new TwigFilter('camelCaseToUnderscore', [$this, 'camelCaseToUnderscoreFilter']), |
|
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 $date |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function dateFormatFilter($date) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($date instanceof \DateTime) { |
|
65
|
|
|
$dateFormat = $this->translator->trans('time.format.date', [], 'framework'); |
|
66
|
|
|
|
|
67
|
|
|
return $this->prependDayName($date) . ', ' . $date->format($dateFormat); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return '-'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $date |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function semesterFilter($semester) |
|
79
|
|
|
{ |
|
80
|
|
|
if (\is_int($semester)) { |
|
81
|
|
|
return SemesterType::semesterToString($semester); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return '-'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $date |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function dateTimeFilter($date) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($date instanceof \DateTime) { |
|
95
|
|
|
$dateTimeFormat = $this->translator->trans('time.format.date_time', [], 'framework'); |
|
96
|
|
|
|
|
97
|
|
|
return $this->prependDayName($date) . ', ' . $date->format($dateTimeFormat); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return '-'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $date |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function timeFormatFilter($date) |
|
109
|
|
|
{ |
|
110
|
|
|
if (\is_string($date)) { |
|
111
|
|
|
return mb_substr($date, 0, 5); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return '-'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* translates the day of the week. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private function prependDayName(DateTime $date) |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->translator->trans('time.weekdays.' . $date->format('D'), [], 'framework'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $value |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function booleanFilter($value) |
|
133
|
|
|
{ |
|
134
|
|
|
if ($value) { |
|
135
|
|
|
return BooleanType::getTranslation(BooleanType::YES, $this->translator); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return BooleanType::getTranslation(BooleanType::NO, $this->translator); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|