TwigExtension   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 25
c 1
b 0
f 0
dl 0
loc 103
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A camelCaseToUnderscoreFilter() 0 3 1
A __construct() 0 3 1
A prependDayName() 0 3 1
A dateTimeFilter() 0 9 2
A dateFormatFilter() 0 9 2
A timeFormatFilter() 0 7 2
A booleanFilter() 0 7 2
A getFilters() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the thealternativezurich/feedback 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 DateTime;
16
use Symfony\Component\Translation\TranslatorInterface;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFilter;
19
20
class TwigExtension extends AbstractExtension
21
{
22
    private $translator;
23
24
    public function __construct(TranslatorInterface $translator)
25
    {
26
        $this->translator = $translator;
27
    }
28
29
    /**
30
     * makes the filters available to twig.
31
     *
32
     * @return array
33
     */
34
    public function getFilters()
35
    {
36
        return [
37
            new TwigFilter('dateFormat', [$this, 'dateFormatFilter']),
38
            new TwigFilter('timeFormat', [$this, 'timeFormatFilter']),
39
            new TwigFilter('dateTimeFormat', [$this, 'dateTimeFilter']),
40
            new TwigFilter('booleanFormat', [$this, 'booleanFilter']),
41
            new TwigFilter('camelCaseToUnderscore', [$this, 'camelCaseToUnderscoreFilter']),
42
        ];
43
    }
44
45
    /**
46
     * @param string $propertyName
47
     *
48
     * @return string
49
     */
50
    public function camelCaseToUnderscoreFilter($propertyName)
51
    {
52
        return mb_strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $propertyName));
53
    }
54
55
    /**
56
     * @param $date
57
     *
58
     * @return string
59
     */
60
    public function dateFormatFilter($date)
61
    {
62
        if ($date instanceof \DateTime) {
63
            $dateFormat = $this->translator->trans('time.format.date', [], 'framework');
64
65
            return $this->prependDayName($date).', '.$date->format($dateFormat);
66
        }
67
68
        return '-';
69
    }
70
71
    /**
72
     * @param $date
73
     *
74
     * @return string
75
     */
76
    public function dateTimeFilter($date)
77
    {
78
        if ($date instanceof \DateTime) {
79
            $dateTimeFormat = $this->translator->trans('time.format.date_time', [], 'framework');
80
81
            return $this->prependDayName($date).', '.$date->format($dateTimeFormat);
82
        }
83
84
        return '-';
85
    }
86
87
    /**
88
     * @param $date
89
     *
90
     * @return string
91
     */
92
    public function timeFormatFilter($date)
93
    {
94
        if (\is_string($date)) {
95
            return mb_substr($date, 0, 5);
96
        }
97
98
        return '-';
99
    }
100
101
    /**
102
     * translates the day of the week.
103
     *
104
     * @return string
105
     */
106
    private function prependDayName(DateTime $date)
107
    {
108
        return $this->translator->trans('time.weekdays.'.$date->format('D'), [], 'framework');
109
    }
110
111
    /**
112
     * @param $value
113
     *
114
     * @return string
115
     */
116
    public function booleanFilter($value)
117
    {
118
        if ($value) {
119
            return BooleanType::getTranslation(BooleanType::YES, $this->translator);
120
        }
121
122
        return BooleanType::getTranslation(BooleanType::NO, $this->translator);
123
    }
124
}
125