1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netdudes\DataSourceryBundle\Extension; |
4
|
|
|
|
5
|
|
|
use Netdudes\DataSourceryBundle\Extension\Type\TableBundleFunctionExtension; |
6
|
|
|
use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
8
|
|
|
use Symfony\Component\Security\Core\SecurityContextInterface; |
9
|
|
|
|
10
|
|
|
class BuiltInFunctionsExtension extends AbstractTableBundleExtension |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var TokenStorageInterface |
14
|
|
|
*/ |
15
|
|
|
private $tokenStorage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var CurrentDateTimeProvider |
19
|
|
|
*/ |
20
|
|
|
private $dateTimeProvider; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param TokenStorageInterface $tokenStorage |
24
|
|
|
* @param CurrentDateTimeProvider $dateTimeProvider |
25
|
|
|
*/ |
26
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider) |
27
|
|
|
{ |
28
|
|
|
$this->tokenStorage = $tokenStorage; |
29
|
|
|
$this->dateTimeProvider = $dateTimeProvider; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function getName() |
36
|
|
|
{ |
37
|
|
|
return 'table_bundle_extension_built_in'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getFunctions() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
new TableBundleFunctionExtension('now', $this, 'now'), |
47
|
|
|
new TableBundleFunctionExtension('startOfDay', $this, 'startOfDay'), |
48
|
|
|
new TableBundleFunctionExtension('endOfDay', $this, 'endOfDay'), |
49
|
|
|
new TableBundleFunctionExtension('currentUser', $this, 'currentUser'), |
50
|
|
|
new TableBundleFunctionExtension('random', $this, 'random') |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets the current timestamp, with an offset string |
56
|
|
|
* |
57
|
|
|
* @param string $offset |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
|
|
public function now($offset = null) |
63
|
|
|
{ |
64
|
|
|
$now = clone $this->dateTimeProvider->get(); |
65
|
|
|
|
66
|
|
|
if ($offset) { |
|
|
|
|
67
|
|
|
$interval = \DateInterval::createFromDateString($offset); |
68
|
|
|
$now->add($interval); |
69
|
|
|
|
70
|
|
|
if ($now == $this->dateTimeProvider->get()) { |
71
|
|
|
// The date didn't change therefore we assume the given offset is not valid |
72
|
|
|
throw new \Exception($offset . ' is not a valid date/time interval.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $now->format(\DateTime::ISO8601); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets a date with the hour 00:00:00 |
81
|
|
|
* |
82
|
|
|
* @param string $date |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function startOfDay($date = null) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$now = clone $this->dateTimeProvider->get(); |
90
|
|
|
|
91
|
|
|
if ($date) { |
|
|
|
|
92
|
|
|
$now = $this->modifyDate($now, $date); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$now->setTime(0, 0, 0); |
96
|
|
|
|
97
|
|
|
return $now->format(\DateTime::ISO8601); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets a date with the hour 23:59:59 |
102
|
|
|
* |
103
|
|
|
* @param string $date |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
* @throws \Exception |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function endOfDay($date = null) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$now = clone $this->dateTimeProvider->get(); |
111
|
|
|
|
112
|
|
|
if ($date) { |
|
|
|
|
113
|
|
|
$now = $this->modifyDate($now, $date); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$now->setTime(23, 59, 59); |
117
|
|
|
|
118
|
|
|
return $now->format(\DateTime::ISO8601); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Gets the current users' username |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function currentUser() |
127
|
|
|
{ |
128
|
|
|
return $this->tokenStorage->getToken()->getUsername(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Gets a random value between $min and $max |
133
|
|
|
* |
134
|
|
|
* @param int $min |
135
|
|
|
* @param int $max |
136
|
|
|
* |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
|
|
public function random($min = 0, $max = 10) |
140
|
|
|
{ |
141
|
|
|
return rand($min, $max); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param \DateTime $date |
146
|
|
|
* @param string $change |
147
|
|
|
* |
148
|
|
|
* @return \DateTime |
149
|
|
|
* @throws \Exception |
150
|
|
|
*/ |
151
|
|
|
private function modifyDate(\DateTime $date, $change) |
152
|
|
|
{ |
153
|
|
|
try { |
154
|
|
|
$date->modify($change); |
155
|
|
|
} catch (\Exception $e) { |
156
|
|
|
throw new \Exception($change . ' is not a valid date or date offset.'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return ($date); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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: