1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpha\Model\Type; |
4
|
|
|
|
5
|
|
|
use Alpha\Util\Helper\Validator; |
6
|
|
|
use Alpha\Exception\IllegalArguementException; |
7
|
|
|
use Alpha\Util\Config\ConfigProvider; |
8
|
|
|
use \DateTime; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The Date complex data type. |
12
|
|
|
* |
13
|
|
|
* @since 1.0 |
14
|
|
|
* |
15
|
|
|
* @author John Collins <[email protected]> |
16
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
17
|
|
|
* @copyright Copyright (c) 2019, John Collins (founder of Alpha Framework). |
18
|
|
|
* All rights reserved. |
19
|
|
|
* |
20
|
|
|
* <pre> |
21
|
|
|
* Redistribution and use in source and binary forms, with or |
22
|
|
|
* without modification, are permitted provided that the |
23
|
|
|
* following conditions are met: |
24
|
|
|
* |
25
|
|
|
* * Redistributions of source code must retain the above |
26
|
|
|
* copyright notice, this list of conditions and the |
27
|
|
|
* following disclaimer. |
28
|
|
|
* * Redistributions in binary form must reproduce the above |
29
|
|
|
* copyright notice, this list of conditions and the |
30
|
|
|
* following disclaimer in the documentation and/or other |
31
|
|
|
* materials provided with the distribution. |
32
|
|
|
* * Neither the name of the Alpha Framework nor the names |
33
|
|
|
* of its contributors may be used to endorse or promote |
34
|
|
|
* products derived from this software without specific |
35
|
|
|
* prior written permission. |
36
|
|
|
* |
37
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
38
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
39
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
40
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
41
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
42
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
43
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
44
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
45
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
46
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
47
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
48
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
49
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
50
|
|
|
* </pre> |
51
|
|
|
*/ |
52
|
|
|
class Date extends Type implements TypeInterface |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* The year part. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
* |
59
|
|
|
* @since 1.0 |
60
|
|
|
*/ |
61
|
|
|
private $year; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The month part. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
* |
68
|
|
|
* @since 1.0 |
69
|
|
|
*/ |
70
|
|
|
private $month; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The textual version of the month, e.g. July. |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
* |
77
|
|
|
* @since 3.1 |
78
|
|
|
*/ |
79
|
|
|
private $monthName; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The day part. |
83
|
|
|
* |
84
|
|
|
* @var string |
85
|
|
|
* |
86
|
|
|
* @since 1.0 |
87
|
|
|
*/ |
88
|
|
|
private $day; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The textual version of the day, e.g. Monday. |
92
|
|
|
* |
93
|
|
|
* @var string |
94
|
|
|
* |
95
|
|
|
* @since 1.0 |
96
|
|
|
*/ |
97
|
|
|
private $weekday; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The validation rule (reg-ex) applied to Date values. |
101
|
|
|
* |
102
|
|
|
* @var string |
103
|
|
|
* |
104
|
|
|
* @since 1.0 |
105
|
|
|
*/ |
106
|
|
|
private $validationRule; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The error message returned for invalid values. |
110
|
|
|
* |
111
|
|
|
* @var string |
112
|
|
|
* |
113
|
|
|
* @since 1.0 |
114
|
|
|
*/ |
115
|
|
|
protected $helper = 'Not a valid date value! A date should be in the ISO format YYYY-MM-DD.'; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Constructor. Leave $date param empty to default to now. |
119
|
|
|
* |
120
|
|
|
* @param string $date Date string in the ISO format YYYY-MM-DD. |
121
|
|
|
* |
122
|
|
|
* @since 1.0 |
123
|
|
|
* |
124
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
125
|
|
|
*/ |
126
|
|
|
public function __construct($date = '') |
127
|
|
|
{ |
128
|
|
|
$config = ConfigProvider::getInstance(); |
129
|
|
|
|
130
|
|
|
$this->validationRule = Validator::ALLOW_ALL; |
131
|
|
|
|
132
|
|
|
if (empty($date)) { |
133
|
|
|
if ($config->get('app.default.datetime') == 'now') { |
134
|
|
|
$this->year = date('Y'); |
135
|
|
|
$this->month = date('m'); |
136
|
|
|
$this->day = date('d'); |
137
|
|
|
$this->weekday = date('l'); |
138
|
|
|
$this->monthName = date('F'); |
139
|
|
|
} else { |
140
|
|
|
$this->year = '0000'; |
141
|
|
|
$this->month = '00'; |
142
|
|
|
$this->day = '00'; |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
if (preg_match($this->validationRule, $date)) { |
146
|
|
|
$this->populateFromString($date); |
147
|
|
|
} else { |
148
|
|
|
throw new IllegalArguementException($this->helper); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Accepts a full date string in ISO YYYY-mm-dd format and populates relevent Date attributes. |
155
|
|
|
* |
156
|
|
|
* @param string $date |
157
|
|
|
* |
158
|
|
|
* @since 1.0 |
159
|
|
|
* |
160
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
161
|
|
|
*/ |
162
|
|
|
public function setValue($date) |
163
|
|
|
{ |
164
|
|
|
$this->populateFromString($date); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set the Date attributes to match the three values provided. |
169
|
|
|
* |
170
|
|
|
* @param int $year |
171
|
|
|
* @param int $month |
172
|
|
|
* @param int $day |
173
|
|
|
* |
174
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
175
|
|
|
* |
176
|
|
|
* @since 1.0 |
177
|
|
|
*/ |
178
|
|
|
public function setDateValue($year, $month, $day) |
179
|
|
|
{ |
180
|
|
|
$valid = null; |
181
|
|
|
|
182
|
|
|
if (!preg_match('/^[0-9]{4}$/', $year)) { |
183
|
|
|
$valid = 'Error: the year value '.$year.' provided is invalid!'; |
184
|
|
|
} |
185
|
|
|
if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month)) { |
186
|
|
|
$valid = 'Error: the month value '.$month.' provided is invalid!'; |
187
|
|
|
} |
188
|
|
|
if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day)) { |
189
|
|
|
$valid = 'Error: the day value '.$day.' provided is invalid!'; |
190
|
|
|
} |
191
|
|
|
if (!isset($valid) && !checkdate($month, $day, $year)) { |
192
|
|
|
$valid = 'Error: the day value '.$year.'-'.$month.'-'.$day.' provided is invalid!'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (isset($valid)) { |
196
|
|
|
throw new IllegalArguementException($valid); |
197
|
|
|
} else { |
198
|
|
|
$this->year = $year; |
|
|
|
|
199
|
|
|
$this->month = str_pad($month, 2, '0', STR_PAD_LEFT); |
200
|
|
|
$this->day = str_pad($day, 2, '0', STR_PAD_LEFT); |
201
|
|
|
$unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year); |
202
|
|
|
$this->weekday = date('l', $unixTime); |
203
|
|
|
$this->monthName = date('F', $unixTime); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the date value as a string in the format "YYYY-MM-DD". |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
* |
212
|
|
|
* @since 1.0 |
213
|
|
|
*/ |
214
|
|
|
public function getValue() |
215
|
|
|
{ |
216
|
|
|
return $this->year.'-'.$this->month.'-'.$this->day; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Return the value in UNIX timestamp format. |
221
|
|
|
* |
222
|
|
|
* @return int |
223
|
|
|
* |
224
|
|
|
* @since 1.0 |
225
|
|
|
*/ |
226
|
|
|
public function getUnixValue() |
227
|
|
|
{ |
228
|
|
|
return mktime(0, 0, 0, $this->month, $this->day, $this->year); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get the date value as a string in the format "DD/MM/YYYY". |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
* |
236
|
|
|
* @since 1.0 |
237
|
|
|
*/ |
238
|
|
|
public function getEuroValue() |
239
|
|
|
{ |
240
|
|
|
return $this->day.'/'.$this->month.'/'.mb_substr($this->year, 2, 2); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Get the date value as a string in the format "MM/DD/YYYY". |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
* |
248
|
|
|
* @since 1.0 |
249
|
|
|
*/ |
250
|
|
|
public function getUSValue() |
251
|
|
|
{ |
252
|
|
|
return $this->month.'/'.$this->day.'/'.mb_substr($this->year, 2, 2); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the year part. |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
* |
260
|
|
|
* @since 1.0 |
261
|
|
|
*/ |
262
|
|
|
public function getYear() |
263
|
|
|
{ |
264
|
|
|
return $this->year; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get the month part. |
269
|
|
|
* |
270
|
|
|
* @return string |
271
|
|
|
* |
272
|
|
|
* @since 1.0 |
273
|
|
|
*/ |
274
|
|
|
public function getMonth() |
275
|
|
|
{ |
276
|
|
|
return $this->month; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get the month part. |
281
|
|
|
* |
282
|
|
|
* @return string |
283
|
|
|
* |
284
|
|
|
* @since 3.1 |
285
|
|
|
*/ |
286
|
|
|
public function getMonthName() |
287
|
|
|
{ |
288
|
|
|
return $this->monthName; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get the day part. |
293
|
|
|
* |
294
|
|
|
* @return string |
295
|
|
|
* |
296
|
|
|
* @since 1.0 |
297
|
|
|
*/ |
298
|
|
|
public function getDay() |
299
|
|
|
{ |
300
|
|
|
return $this->day; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get the textual weekday part, e.g. Monday. |
305
|
|
|
* |
306
|
|
|
* @return string |
307
|
|
|
* |
308
|
|
|
* @since 1.0 |
309
|
|
|
*/ |
310
|
|
|
public function getWeekday() |
311
|
|
|
{ |
312
|
|
|
return $this->weekday; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Accepts a full date string in YYYY-MM-DD format and populates relevent Date attributes. |
317
|
|
|
* |
318
|
|
|
* @param string $date |
319
|
|
|
* |
320
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
321
|
|
|
* |
322
|
|
|
* @since 1.0 |
323
|
|
|
*/ |
324
|
|
|
public function populateFromString($date) |
325
|
|
|
{ |
326
|
|
|
$valid = null; |
327
|
|
|
|
328
|
|
|
if ($date == '' || $date == '0000-00-00') { |
329
|
|
|
$this->year = '0000'; |
330
|
|
|
$this->month = '00'; |
331
|
|
|
$this->day = '00'; |
332
|
|
|
} else { |
333
|
|
|
// This is just here for legacy to ensure that any old time value from a Date object is ignored |
334
|
|
|
$spilt_by_space = explode(' ', $date); |
335
|
|
|
|
336
|
|
|
if (isset($spilt_by_space[0])) { |
337
|
|
|
$date = $spilt_by_space[0]; |
338
|
|
|
} else { |
339
|
|
|
throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$split_by_dash = explode('-', $date); |
343
|
|
|
|
344
|
|
|
// Parse for the date parts, seperated by "-" |
345
|
|
|
if (isset($split_by_dash[0]) && isset($split_by_dash[1]) && isset($split_by_dash[2])) { |
346
|
|
|
$year = $split_by_dash[0]; |
347
|
|
|
$month = $split_by_dash[1]; |
348
|
|
|
$day = $split_by_dash[2]; |
349
|
|
|
} else { |
350
|
|
|
throw new IllegalArguementException('Invalid Date value ['.$date.'] provided!'); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
if (!preg_match('/^[0-9]{4}$/', $year)) { |
354
|
|
|
$valid = 'Error: the year value '.$year.' provided is invalid!'; |
355
|
|
|
} |
356
|
|
|
if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month)) { |
357
|
|
|
$valid = 'Error: the month value '.$month.' provided is invalid!'; |
358
|
|
|
} |
359
|
|
|
if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day)) { |
360
|
|
|
$valid = 'Error: the day value '.$day.' provided is invalid!'; |
361
|
|
|
} |
362
|
|
|
if (!isset($valid) && !checkdate($month, $day, $year)) { |
363
|
|
|
$valid = 'Error: the date value '.$year.'-'.$month.'-'.$day.' provided is invalid!'; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
if (isset($valid)) { |
367
|
|
|
throw new IllegalArguementException($valid); |
368
|
|
|
} else { |
369
|
|
|
$this->year = $year; |
370
|
|
|
$this->month = str_pad($month, 2, '0', STR_PAD_LEFT); |
371
|
|
|
$this->day = str_pad($day, 2, '0', STR_PAD_LEFT); |
372
|
|
|
$unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year); |
373
|
|
|
$this->weekday = date('l', $unixTime); |
374
|
|
|
$this->monthName = date('F', $unixTime); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Get the validation rule. |
381
|
|
|
* |
382
|
|
|
* @return string |
383
|
|
|
* |
384
|
|
|
* @since 1.0 |
385
|
|
|
*/ |
386
|
|
|
public function getRule() |
387
|
|
|
{ |
388
|
|
|
return $this->validationRule; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Set the validation rule. |
393
|
|
|
* |
394
|
|
|
* @param string $rule |
395
|
|
|
* |
396
|
|
|
* @since 1.0 |
397
|
|
|
*/ |
398
|
|
|
public function setRule($rule) |
399
|
|
|
{ |
400
|
|
|
$this->validationRule = $rule; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* |
405
|
|
|
* Increment the cunrrent date by the amount provided |
406
|
|
|
* |
407
|
|
|
* @param string $amount The amount to increment the date by, e.g. "1 day" |
408
|
|
|
* |
409
|
|
|
* @since 3.1.0 |
410
|
|
|
*/ |
411
|
|
|
public function increment($amount) |
412
|
|
|
{ |
413
|
|
|
$date = strtotime($amount, strtotime($this->getValue())); |
414
|
|
|
$this->setValue(date('Y-m-d', $date)); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* |
419
|
|
|
* Get the start date and the end date of the week of the year provided |
420
|
|
|
* |
421
|
|
|
* @param int The number of the week (1-52) |
422
|
|
|
* @param int The year (YYYY) |
423
|
|
|
* |
424
|
|
|
* @return array An array containing the "start" date and "end" date. |
425
|
|
|
* |
426
|
|
|
* @since 3.1.0 |
427
|
|
|
*/ |
428
|
|
|
public static function getStartAndEndDate($week, $year) |
429
|
|
|
{ |
430
|
|
|
$dateTime = new DateTime(); |
431
|
|
|
$dateTime->setISODate($year, $week); |
432
|
|
|
|
433
|
|
|
$value = array(); |
434
|
|
|
|
435
|
|
|
$value['start'] = $dateTime->format('Y-m-d'); |
436
|
|
|
$dateTime->modify('+6 days'); |
437
|
|
|
$value['end'] = $dateTime->format('Y-m-d'); |
438
|
|
|
|
439
|
|
|
return $value; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.