1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the DateTime library. |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fresh\DateTime; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* DateTimeHelper. |
17
|
|
|
* |
18
|
|
|
* @author Artem Henvald <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class DateTimeHelper implements DateTimeHelperInterface |
21
|
|
|
{ |
22
|
|
|
private const INTERNAL_DATE_FORMAT = 'Y-m-d'; |
23
|
|
|
|
24
|
|
|
/** @var mixed[] */ |
25
|
|
|
private $datesCache = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function createDateTimeZone(string $timeZoneName = 'UTC'): \DateTimeZone |
31
|
|
|
{ |
32
|
|
|
return new \DateTimeZone($timeZoneName); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getCurrentTimestamp(): int |
39
|
|
|
{ |
40
|
|
|
return \time(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function getCurrentDatetime(?\DateTimeZone $timeZone = null): \DateTime |
47
|
|
|
{ |
48
|
|
|
return new \DateTime('now', $timeZone); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getCurrentDatetimeImmutable(?\DateTimeZone $timeZone = null): \DateTimeImmutable |
55
|
|
|
{ |
56
|
|
|
return new \DateTimeImmutable('now', $timeZone); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getDatesFromDateRangeAsArrayOfObjects(DateRangeInterface $dateRange): array |
63
|
|
|
{ |
64
|
|
|
$cacheKeyForDateRange = $this->getCacheKeyForDateRange($dateRange); |
65
|
|
|
|
66
|
|
|
if (!isset($this->datesCache[$cacheKeyForDateRange])) { |
67
|
|
|
$since = DateTimeCloner::cloneIntoDateTime($dateRange->getSince()); |
68
|
|
|
$since->setTime(0, 0); |
69
|
|
|
|
70
|
|
|
$till = DateTimeCloner::cloneIntoDateTime($dateRange->getTill()); |
71
|
|
|
$till->setTime(23, 59, 59); |
72
|
|
|
|
73
|
|
|
$datesAsObjects = []; |
74
|
|
|
$period = new \DatePeriod($since, new \DateInterval('P1D'), $till); |
75
|
|
|
foreach ($period as $date) { |
76
|
|
|
$datesAsObjects[] = $date; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->datesCache[$cacheKeyForDateRange] = $datesAsObjects; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->datesCache[$cacheKeyForDateRange]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getDatesFromDateRangeAsArrayOfStrings(DateRangeInterface $dateRange): array |
89
|
|
|
{ |
90
|
|
|
$datesAsObjects = $this->getDatesFromDateRangeAsArrayOfObjects($dateRange); |
91
|
|
|
|
92
|
|
|
$datesAsStrings = []; |
93
|
|
|
foreach ($datesAsObjects as $datesAsObject) { |
94
|
|
|
$datesAsStrings[] = $datesAsObject->format(self::INTERNAL_DATE_FORMAT); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $datesAsStrings; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param DateRangeInterface $dateRange |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
private function getCacheKeyForDateRange(DateRangeInterface $dateRange): string |
106
|
|
|
{ |
107
|
|
|
$since = $dateRange->getSince(); |
108
|
|
|
$till = $dateRange->getTill(); |
109
|
|
|
|
110
|
|
|
return \sprintf( |
111
|
|
|
'%s_%s_%s_%s', |
112
|
|
|
$since->format(self::INTERNAL_DATE_FORMAT), |
113
|
|
|
$since->getTimezone()->getName(), |
114
|
|
|
$till->format(self::INTERNAL_DATE_FORMAT), |
115
|
|
|
$till->getTimezone()->getName() |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|