TwigExtensions   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 1
dl 0
loc 120
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A formatTime() 0 15 4
A getLanguageName() 0 8 2
A formatFloat() 0 17 5
A formatDate() 0 5 2
A formatDateTime() 0 5 2
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[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 CRUDlex;
13
14
use Symfony\Component\Intl\Exception\MissingResourceException;
15
use Symfony\Component\Intl\Languages;
16
17
/**
18
 * Provides the Twig extensions like filters.
19
 */
20
class TwigExtensions
21
{
22
23
    /**
24
     * Formats the given time value to a timestring defined by the $pattern
25
     * parameter.
26
     *
27
     * If the value is false (like null), an empty string is
28
     * returned. Else, the value is tried to be parsed as datetime via the
29
     * given pattern. If that fails, it is tried to be parsed with the pattern
30
     * 'Y-m-d H:i:s'. If that fails, the value is returned unchanged. Else, it
31
     * is returned formatted with the given pattern. The effect is to shorten
32
     * 'Y-m-d H:i:s' to 'Y-m-d' for example.
33
     *
34
     * @param string $value
35
     * the value to be formatted
36
     * @param string $timezone
37
     * the timezone of the value
38
     * @param string $pattern
39
     * the pattern with which the value is parsed and formatted
40
     *
41
     * @return string
42
     * the formatted value
43
     */
44 2
    protected function formatTime($value, $timezone, $pattern)
45
    {
46 2
        if (!$value) {
47 2
            return '';
48
        }
49 2
        $result = \DateTime::createFromFormat($pattern, $value, new \DateTimeZone($timezone));
50 2
        if ($result === false) {
51 2
            $result = \DateTime::createFromFormat('Y-m-d H:i:s', $value, new \DateTimeZone($timezone));
52
        }
53 2
        if ($result === false) {
54 2
            return $value;
55
        }
56 2
        $result->setTimezone(new \DateTimeZone(date_default_timezone_get()));
57 2
        return $result->format($pattern);
58
    }
59
60
    /**
61
     * Gets a language name in the given language.
62
     *
63
     * @param string $language
64
     * the language code of the desired language name
65
     *
66
     * @return string|null
67
     * the language name in the given language or null if not available
68
     */
69 1
    public function getLanguageName($language)
70
    {
71
        try {
72 1
            return Languages::getName($language, $language);
73 1
        } catch (MissingResourceException $e) {
74 1
            return null;
75
        }
76
    }
77
78
    /**
79
     * Formats a float to not display in scientific notation.
80
     *
81
     * @param float $float
82
     * the float to format
83
     *
84
     * @return double|string
85
     * the formated float
86
     */
87 1
    public function formatFloat($float)
88
    {
89
90 1
        if (!$float) {
91 1
            return $float;
92
        }
93
94 1
        $zeroFraction = $float - floor($float) == 0 ? '0' : '';
95
96
        // We don't want values like 0.004 converted to 0.00400000000000000008
97 1
        if ($float > 0.0001) {
98 1
            return $float.($zeroFraction === '0' ? '.'.$zeroFraction : '');
99
        }
100
101
        // We don't want values like 0.00004 converted to its scientific notation 4.0E-5
102 1
        return rtrim(sprintf('%.20F', $float), '0').$zeroFraction;
103
    }
104
105
    /**
106
     * Formats the given value to a date of the format 'Y-m-d'.
107
     *
108
     * @param string $value
109
     * the value, might be of the format 'Y-m-d H:i' or 'Y-m-d'
110
     * @param boolean $isUTC
111
     * whether the given value is in UTC
112
     *
113
     * @return string
114
     * the formatted result or an empty string on null value
115
     */
116 1
    public function formatDate($value, $isUTC)
117
    {
118 1
        $timezone = $isUTC ? 'UTC' : date_default_timezone_get();
119 1
        return $this->formatTime($value, $timezone, 'Y-m-d');
120
    }
121
122
    /**
123
     * Formats the given value to a date of the format 'Y-m-d H:i'.
124
     *
125
     * @param string $value
126
     * the value, might be of the format 'Y-m-d H:i'
127
     * @param boolean $isUTC
128
     * whether the given value is in UTC
129
     *
130
     * @return string
131
     * the formatted result or an empty string on null value
132
     */
133 1
    public function formatDateTime($value, $isUTC)
134
    {
135 1
        $timezone = $isUTC ? 'UTC' : date_default_timezone_get();
136 1
        return $this->formatTime($value, $timezone, 'Y-m-d H:i');
137
    }
138
139
}
140