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
|
|
|
protected $formatter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Converter |
25
|
|
|
*/ |
26
|
|
|
protected $converter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Comparator |
30
|
|
|
*/ |
31
|
|
|
protected $comparator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Formatter $formatter |
35
|
|
|
* @param Converter $converter |
36
|
|
|
* @param Comparator $comparator |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Formatter $formatter, Converter $converter, Comparator $comparator) |
39
|
|
|
{ |
40
|
|
|
$this->formatter = $formatter; |
41
|
|
|
$this->converter = $converter; |
42
|
|
|
$this->comparator = $comparator; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getFilters() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
new \Twig_SimpleFilter('date', [$this, 'getDateFormat'], ['needs_environment' => true]), |
52
|
|
|
new \Twig_SimpleFilter('date_passed', [$this, 'getDatePassed']), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getFunctions() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
new \Twig_SimpleFunction('compare_date_time', [$this, 'getCompareDateTime']), |
63
|
|
|
new \Twig_SimpleFunction('compare_date', [$this, 'getCompareDate']), |
64
|
|
|
new \Twig_SimpleFunction('compare_time', [$this, 'getCompareTime']), |
65
|
|
|
new \Twig_SimpleFunction('compare_week', [$this, 'getCompareWeek']), |
66
|
|
|
new \Twig_SimpleFunction('compare_month', [$this, 'getCompareMonth']), |
67
|
|
|
new \Twig_SimpleFunction('compare_year', [$this, 'getCompareYear']), |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param \Twig_Environment $env |
73
|
|
|
* @param mixed $date |
74
|
|
|
* @param string|int|null $format |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getDateFormat(\Twig_Environment $env, $date, $format = null) |
79
|
|
|
{ |
80
|
|
|
if ($format === null) { |
81
|
|
|
/* @var $core \Twig_Extension_Core */ |
82
|
|
|
$core = $env->getExtension('core'); |
83
|
|
|
$formats = $core->getDateFormat(); |
84
|
|
|
$format = $date instanceof \DateInterval ? $formats[1] : $formats[0]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// date time formatter not support date interval |
88
|
|
|
if ($date instanceof \DateInterval) { |
89
|
|
|
return $date->format($format); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
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
|
|
|
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
|
|
|
|
110
|
|
|
return $this->formatter->passed($this->convert($date), $time_format, $month_format, $year_format); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param mixed $x |
115
|
|
|
* @param string $operator |
116
|
|
|
* @param mixed $y |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function getCompareDateTime($x, $operator, $y) |
121
|
|
|
{ |
122
|
|
|
return $this->comparator->compareDateTime($this->convert($x), $operator, $this->convert($y)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param mixed $x |
127
|
|
|
* @param string $operator |
128
|
|
|
* @param mixed $y |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function getCompareDate($x, $operator, $y) |
133
|
|
|
{ |
134
|
|
|
return $this->comparator->compareDate($this->convert($x), $operator, $this->convert($y)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param mixed $x |
139
|
|
|
* @param string $operator |
140
|
|
|
* @param mixed $y |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function getCompareTime($x, $operator, $y) |
145
|
|
|
{ |
146
|
|
|
return $this->comparator->compareTime($this->convert($x), $operator, $this->convert($y)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param mixed $x |
151
|
|
|
* @param string $operator |
152
|
|
|
* @param mixed $y |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function getCompareWeek($x, $operator, $y) |
157
|
|
|
{ |
158
|
|
|
return $this->comparator->compareWeek($this->convert($x), $operator, $this->convert($y)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param mixed $x |
163
|
|
|
* @param string $operator |
164
|
|
|
* @param mixed $y |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function getCompareMonth($x, $operator, $y) |
169
|
|
|
{ |
170
|
|
|
return $this->comparator->compareMonth($this->convert($x), $operator, $this->convert($y)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param mixed $x |
175
|
|
|
* @param string $operator |
176
|
|
|
* @param mixed $y |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function getCompareYear($x, $operator, $y) |
181
|
|
|
{ |
182
|
|
|
return $this->comparator->compareYear($this->convert($x), $operator, $this->convert($y)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param mixed $date |
187
|
|
|
* |
188
|
|
|
* @return \DateTime |
189
|
|
|
*/ |
190
|
|
|
private function convert($date) |
191
|
|
|
{ |
192
|
|
|
return $this->converter->getDateTime($date); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getName() |
199
|
|
|
{ |
200
|
|
|
return 'gpslab_date_extension'; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|