1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DrLenux\DataHelper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DateFill |
9
|
|
|
* @package DrLenux\DataHelper |
10
|
|
|
* |
11
|
|
|
* @method $this to(string $to) |
12
|
|
|
* @method $this from(string $from) |
13
|
|
|
* @method $this inclusiveStart(bool $status) |
14
|
|
|
* @method $this inclusiveEnd(bool $status) |
15
|
|
|
* @method $this interval(string $interval) |
16
|
|
|
* @method $this format(string $format) |
17
|
|
|
* @method $this timezone(\DateTimeZone $timezone = null) |
18
|
|
|
*/ |
19
|
|
|
class DateFill |
20
|
|
|
{ |
21
|
|
|
const DEFAULT_FORMAT = 'Y-m-d H:i:s'; |
22
|
|
|
|
23
|
|
|
const INTERVAL_SECOND = 'PT1S'; |
24
|
|
|
const INTERVAL_MINUTE = 'PT1M'; |
25
|
|
|
const INTERVAL_HOUR = 'PT1H'; |
26
|
|
|
const INTERVAL_DAY = 'P1D'; |
27
|
|
|
const INTERVAL_MONTH = 'P1M'; |
28
|
|
|
const INTERVAL_YEAR = 'P1Y'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DateTime |
32
|
|
|
*/ |
33
|
|
|
private $_from; |
34
|
|
|
/** |
35
|
|
|
* @var \DateTime |
36
|
|
|
*/ |
37
|
|
|
private $_to; |
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private $_inclusiveStart; |
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $_inclusiveEnd; |
46
|
|
|
/** |
47
|
|
|
* @var \DateInterval |
48
|
|
|
*/ |
49
|
|
|
private $_interval; |
50
|
|
|
/** |
51
|
|
|
* @var \DateTimeZone|null |
52
|
|
|
*/ |
53
|
|
|
private $_timezone; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $_format; |
59
|
|
|
/** |
60
|
|
|
* @var \Exception[] |
61
|
|
|
*/ |
62
|
|
|
private $_errors = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private $_params = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @param array $arguments |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
3 |
|
public function __call(string $name, array $arguments) |
75
|
|
|
{ |
76
|
3 |
|
if (count($arguments)) { |
77
|
3 |
|
$this->_params[$name] = $arguments[0]; |
78
|
|
|
} |
79
|
3 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string[] |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
3 |
|
public function fill():array |
87
|
|
|
{ |
88
|
3 |
|
$this->fillData(); |
89
|
3 |
|
$results = []; |
90
|
3 |
|
if ($this->_inclusiveStart) { |
91
|
1 |
|
$results[] = $this->_from->format($this->_format); |
92
|
|
|
} |
93
|
3 |
|
if ($this->_to > $this->_from) { |
94
|
2 |
|
$this->fillAscending($results); |
95
|
|
|
} else { |
96
|
1 |
|
$this->fillDescending($results); |
97
|
|
|
} |
98
|
3 |
|
if ($this->_inclusiveEnd) { |
99
|
1 |
|
$results[] = $this->_to->format($this->_format); |
100
|
|
|
} |
101
|
3 |
|
return $results; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $results |
106
|
|
|
*/ |
107
|
2 |
|
private function fillAscending(array &$results) |
108
|
|
|
{ |
109
|
2 |
|
$from = clone $this->_from; |
110
|
2 |
|
while ($from !== $this->_to) { |
111
|
2 |
|
$from->add($this->_interval); |
112
|
2 |
|
if ($from >= $this->_to) { |
113
|
2 |
|
$from = $this->_to; |
114
|
|
|
} else { |
115
|
2 |
|
$results[] = $from->format($this->_format); |
116
|
|
|
} |
117
|
|
|
} |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $results |
122
|
|
|
*/ |
123
|
1 |
|
private function fillDescending(array &$results) |
124
|
|
|
{ |
125
|
1 |
|
$from = clone $this->_from; |
126
|
1 |
|
while ($from !== $this->_to) { |
127
|
1 |
|
$from->sub($this->_interval); |
128
|
1 |
|
if ($from <= $this->_to) { |
129
|
1 |
|
$from = $this->_to; |
130
|
|
|
} else { |
131
|
1 |
|
$results[] = $from->format($this->_format); |
132
|
|
|
} |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @throws \Exception |
138
|
|
|
*/ |
139
|
3 |
|
private function fillData() |
140
|
|
|
{ |
141
|
3 |
|
$this->fillTimezone(); |
142
|
3 |
|
$this->fillFromAndTo(); |
143
|
3 |
|
$this->fillInterval(); |
144
|
3 |
|
$this->fillInclusive(); |
145
|
3 |
|
$this->fillFormat(); |
146
|
3 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* |
150
|
|
|
*/ |
151
|
3 |
|
private function fillFormat() |
152
|
|
|
{ |
153
|
3 |
|
$this->_format = $this->_params['format'] ?? DateFill::DEFAULT_FORMAT; |
154
|
3 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* |
158
|
|
|
*/ |
159
|
3 |
|
private function fillInclusive() |
160
|
|
|
{ |
161
|
3 |
|
$this->_inclusiveStart = (bool) ($this->_params['inclusiveStart'] ?? false); |
162
|
3 |
|
$this->_inclusiveEnd = (bool) ($this->_params['inclusiveEnd'] ?? false); |
163
|
3 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @throws \Exception |
167
|
|
|
*/ |
168
|
3 |
|
private function fillInterval() |
169
|
|
|
{ |
170
|
|
|
try { |
171
|
3 |
|
$this->_interval = new \DateInterval( |
172
|
3 |
|
$this->_params['interval'] ?? DateFill::INTERVAL_DAY |
173
|
|
|
); |
174
|
1 |
|
} catch (\Exception $e) { |
175
|
1 |
|
$this->_errors[] = $e; |
176
|
1 |
|
$this->_interval = new \DateInterval(DateFill::INTERVAL_DAY); |
177
|
|
|
} |
178
|
3 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* |
182
|
|
|
*/ |
183
|
3 |
|
private function fillFromAndTo() |
184
|
|
|
{ |
185
|
3 |
|
$this->_from = new \DateTime( |
186
|
3 |
|
$this->_params['from'] ?? 'now', |
187
|
3 |
|
$this->_timezone |
188
|
|
|
); |
189
|
3 |
|
$this->_to = new \DateTime( |
190
|
3 |
|
$this->_params['to'] ?? 'now', |
191
|
3 |
|
$this->_timezone |
192
|
|
|
); |
193
|
3 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* |
197
|
|
|
*/ |
198
|
3 |
|
private function fillTimezone() |
199
|
|
|
{ |
200
|
|
|
if ( |
201
|
3 |
|
isset($this->_params['timezone']) && |
202
|
3 |
|
$this->_params['timezone'] instanceof \DateTimeZone |
203
|
|
|
) { |
204
|
|
|
$this->_timezone = $this->_params['timezone']; |
205
|
|
|
} else { |
206
|
3 |
|
$this->_timezone = null; |
207
|
|
|
} |
208
|
3 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return \Exception[] |
212
|
|
|
*/ |
213
|
1 |
|
public function getErrors() |
214
|
|
|
{ |
215
|
1 |
|
return $this->_errors; |
216
|
|
|
} |
217
|
|
|
} |