Extension::getCompareDateTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Bundle\DateBundle\Twig;
11
12
use GpsLab\Bundle\DateBundle\Converter;
13
use GpsLab\Bundle\DateBundle\Formatter;
14
use GpsLab\Bundle\DateBundle\Comparator;
15
16
class Extension extends \Twig_Extension
17
{
18
    /**
19
     * @var Formatter
20
     */
21
    private $formatter;
22
23
    /**
24
     * @var Converter
25
     */
26
    private $converter;
27
28
    /**
29
     * @var Comparator
30
     */
31
    private $comparator;
32
33
    /**
34
     * @param Formatter $formatter
35
     * @param Converter $converter
36
     * @param Comparator $comparator
37
     */
38 14
    public function __construct(Formatter $formatter, Converter $converter, Comparator $comparator)
39
    {
40 14
        $this->formatter = $formatter;
41 14
        $this->converter = $converter;
42 14
        $this->comparator = $comparator;
43 14
    }
44
45
    /**
46
     * @return array
47
     */
48 1
    public function getFilters()
49
    {
50
        return [
51 1
            new \Twig_SimpleFilter('date', [$this, 'getDateFormat'], ['needs_environment' => true]),
52 1
            new \Twig_SimpleFilter('date_passed', [$this, 'getDatePassed']),
53 1
        ];
54
    }
55
56
    /**
57
     * @return array
58
     */
59 1
    public function getFunctions()
60
    {
61
        return [
62 1
            new \Twig_SimpleFunction('compare_date_time', [$this, 'getCompareDateTime']),
63 1
            new \Twig_SimpleFunction('compare_date', [$this, 'getCompareDate']),
64 1
            new \Twig_SimpleFunction('compare_time', [$this, 'getCompareTime']),
65 1
            new \Twig_SimpleFunction('compare_week', [$this, 'getCompareWeek']),
66 1
            new \Twig_SimpleFunction('compare_month', [$this, 'getCompareMonth']),
67 1
            new \Twig_SimpleFunction('compare_year', [$this, 'getCompareYear']),
68 1
        ];
69
    }
70
71
    /**
72
     * @param \Twig_Environment $env
73
     * @param mixed $date
74
     * @param string|int|null $format
75
     *
76
     * @return string
77
     */
78 4
    public function getDateFormat(\Twig_Environment $env, $date, $format = null)
79
    {
80 4
        if ($format === null) {
81
            /* @var $core \Twig_Extension_Core */
82 2
            $core = $env->getExtension('core');
83 2
            $formats = $core->getDateFormat();
84 2
            $format = $date instanceof \DateInterval ? $formats[1] : $formats[0];
85 2
        }
86
87
        // date time formatter not support date interval
88 4
        if ($date instanceof \DateInterval) {
89 2
            return $date->format($format);
90
        }
91
92 2
        return $this->formatter->format($this->convert($date), $format);
93
    }
94
95
    /**
96
     * @param mixed $date
97
     * @param string $time_format
98
     * @param string $month_format
99
     * @param string $year_format
100
     *
101
     * @return string
102
     */
103 1
    public function getDatePassed(
104
        $date,
105
        $time_format = Formatter::DEFAULT_PASSED_TIME_FORMAT,
106
        $month_format = Formatter::DEFAULT_PASSED_MONTH_FORMAT,
107
        $year_format = Formatter::DEFAULT_PASSED_YEAR_FORMAT
108
    ) {
109 1
        return $this->formatter->passed($this->convert($date), $time_format, $month_format, $year_format);
110
    }
111
112
    /**
113
     * @param mixed $x
114
     * @param string $operator
115
     * @param mixed $y
116
     *
117
     * @return bool
118
     */
119 1
    public function getCompareDateTime($x, $operator, $y)
120
    {
121 1
        return $this->comparator->compareDateTime($this->convert($x), $operator, $this->convert($y));
122
    }
123
124
    /**
125
     * @param mixed $x
126
     * @param string $operator
127
     * @param mixed $y
128
     *
129
     * @return bool
130
     */
131 1
    public function getCompareDate($x, $operator, $y)
132
    {
133 1
        return $this->comparator->compareDate($this->convert($x), $operator, $this->convert($y));
134
    }
135
136
    /**
137
     * @param mixed $x
138
     * @param string $operator
139
     * @param mixed $y
140
     *
141
     * @return bool
142
     */
143 1
    public function getCompareTime($x, $operator, $y)
144
    {
145 1
        return $this->comparator->compareTime($this->convert($x), $operator, $this->convert($y));
146
    }
147
148
    /**
149
     * @param mixed $x
150
     * @param string $operator
151
     * @param mixed $y
152
     *
153
     * @return bool
154
     */
155 1
    public function getCompareWeek($x, $operator, $y)
156
    {
157 1
        return $this->comparator->compareWeek($this->convert($x), $operator, $this->convert($y));
158
    }
159
160
    /**
161
     * @param mixed $x
162
     * @param string $operator
163
     * @param mixed $y
164
     *
165
     * @return bool
166
     */
167 1
    public function getCompareMonth($x, $operator, $y)
168
    {
169 1
        return $this->comparator->compareMonth($this->convert($x), $operator, $this->convert($y));
170
    }
171
172
    /**
173
     * @param mixed $x
174
     * @param string $operator
175
     * @param mixed $y
176
     *
177
     * @return bool
178
     */
179 1
    public function getCompareYear($x, $operator, $y)
180
    {
181 1
        return $this->comparator->compareYear($this->convert($x), $operator, $this->convert($y));
182
    }
183
184
    /**
185
     * @param mixed $date
186
     *
187
     * @return \DateTime
188
     */
189 9
    private function convert($date)
190
    {
191 9
        return $this->converter->getDateTime($date);
192
    }
193
194
    /**
195
     * @return string
196
     */
197 1
    public function getName()
198
    {
199 1
        return 'gpslab_date_extension';
200
    }
201
}
202