1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netdudes\DataSourceryBundle\Extension; |
4
|
|
|
|
5
|
|
|
use Netdudes\DataSourceryBundle\Extension\Type\UqlFunction; |
6
|
|
|
use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
8
|
|
|
|
9
|
|
|
class BuiltInFunctionsExtension implements UqlExtensionInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var TokenStorageInterface |
13
|
|
|
*/ |
14
|
|
|
private $tokenStorage; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var CurrentDateTimeProvider |
18
|
|
|
*/ |
19
|
|
|
private $dateTimeProvider; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param TokenStorageInterface $tokenStorage |
23
|
|
|
* @param CurrentDateTimeProvider $dateTimeProvider |
24
|
|
|
*/ |
25
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider) |
26
|
|
|
{ |
27
|
|
|
$this->tokenStorage = $tokenStorage; |
28
|
|
|
$this->dateTimeProvider = $dateTimeProvider; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function getFunctions() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
new UqlFunction('now', $this, 'now'), |
38
|
|
|
new UqlFunction('startOfDay', $this, 'startOfDay'), |
39
|
|
|
new UqlFunction('startOfWeek', $this, 'startOfWeek'), |
40
|
|
|
new UqlFunction('startOfMonth', $this, 'startOfMonth'), |
41
|
|
|
new UqlFunction('startOfYear', $this, 'startOfYear'), |
42
|
|
|
new UqlFunction('currentUser', $this, 'currentUser'), |
43
|
|
|
new UqlFunction('random', $this, 'random') |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets the current timestamp, with an offset string |
49
|
|
|
* |
50
|
|
|
* @param string $offset |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
public function now($offset = null) |
56
|
|
|
{ |
57
|
|
|
$now = clone $this->dateTimeProvider->get(); |
58
|
|
|
|
59
|
|
|
if ($offset) { |
|
|
|
|
60
|
|
|
$interval = \DateInterval::createFromDateString($offset); |
61
|
|
|
$now->add($interval); |
62
|
|
|
|
63
|
|
|
if ($now == $this->dateTimeProvider->get()) { |
64
|
|
|
// The date didn't change therefore we assume the given offset is not valid |
65
|
|
|
throw new \Exception($offset . ' is not a valid date/time interval.'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $now->format(\DateTime::ISO8601); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets a date with the hour 00:00:00 |
74
|
|
|
* |
75
|
|
|
* @param string $date |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
|
|
public function startOfDay($date = null) |
81
|
|
|
{ |
82
|
|
|
$now = clone $this->dateTimeProvider->get(); |
83
|
|
|
|
84
|
|
|
if ($date) { |
|
|
|
|
85
|
|
|
$now = $this->modifyDate($now, $date); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$now->setTime(0, 0, 0); |
89
|
|
|
|
90
|
|
|
return $now->format(\DateTime::ISO8601); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the Monday of the week for the specified date with the hour 00:00:00 |
95
|
|
|
* |
96
|
|
|
* @param string $date |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function startOfWeek($date = null) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$now = clone $this->dateTimeProvider->get(); |
104
|
|
|
|
105
|
|
|
if ($date) { |
|
|
|
|
106
|
|
|
$now = $this->modifyDate($now, $date); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$year = $now->format('o'); // o = ISO-8601 year number |
110
|
|
|
$week = $now->format('W'); // W = ISO-8601 week number of year, weeks starting on Monday |
111
|
|
|
|
112
|
|
|
$startOfWeek = $now->setISODate($year, $week); |
113
|
|
|
$startOfWeek->setTime(0, 0, 0); |
114
|
|
|
|
115
|
|
|
return $startOfWeek->format(\DateTime::ISO8601); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets the first day of the month for the specified date with the hour 00:00:00 |
120
|
|
|
* |
121
|
|
|
* @param string $date |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
* @throws \Exception |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function startOfMonth($date = null) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$now = clone $this->dateTimeProvider->get(); |
129
|
|
|
|
130
|
|
|
if ($date) { |
|
|
|
|
131
|
|
|
$now = $this->modifyDate($now, $date); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$year = $now->format('Y'); |
135
|
|
|
$month = $now->format('m'); |
136
|
|
|
|
137
|
|
|
$startOfMonth = $now->setDate($year, $month, 1); |
138
|
|
|
$startOfMonth->setTime(0, 0, 0); |
139
|
|
|
|
140
|
|
|
return $startOfMonth->format(\DateTime::ISO8601); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Gets the first day of the year for the specified date with the hour 00:00:00 |
145
|
|
|
* |
146
|
|
|
* @param string $date |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
* @throws \Exception |
150
|
|
|
*/ |
151
|
|
|
public function startOfYear($date = null) |
152
|
|
|
{ |
153
|
|
|
$now = clone $this->dateTimeProvider->get(); |
154
|
|
|
|
155
|
|
|
if ($date) { |
|
|
|
|
156
|
|
|
$now = $this->modifyDate($now, $date); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$year = $now->format('Y'); |
160
|
|
|
|
161
|
|
|
$startOfYear = $now->setDate($year, 1, 1); |
162
|
|
|
$startOfYear->setTime(0, 0, 0); |
163
|
|
|
|
164
|
|
|
return $startOfYear->format(\DateTime::ISO8601); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Gets the current users' username |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function currentUser() |
173
|
|
|
{ |
174
|
|
|
return $this->tokenStorage->getToken()->getUsername(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Gets a random value between $min and $max |
179
|
|
|
* |
180
|
|
|
* @param int $min |
181
|
|
|
* @param int $max |
182
|
|
|
* |
183
|
|
|
* @return int |
184
|
|
|
*/ |
185
|
|
|
public function random($min = 0, $max = 10) |
186
|
|
|
{ |
187
|
|
|
return rand($min, $max); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param \DateTime $date |
192
|
|
|
* @param string $change |
193
|
|
|
* |
194
|
|
|
* @return \DateTime |
195
|
|
|
* @throws \Exception |
196
|
|
|
*/ |
197
|
|
|
private function modifyDate(\DateTime $date, $change) |
198
|
|
|
{ |
199
|
|
|
try { |
200
|
|
|
$date->modify($change); |
201
|
|
|
} catch (\Exception $e) { |
202
|
|
|
throw new \Exception($change . ' is not a valid date or date offset.'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return ($date); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: