Completed
Push — master ( 3b838d...895dde )
by Artem
01:03
created

DateTimeHelper::createDateTimeZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    private $datesCache = [];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function createDateTimeZone(string $timeZoneName = 'UTC'): \DateTimeZone
30
    {
31
        return new \DateTimeZone($timeZoneName);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getCurrentDatetime(?\DateTimeZone $timeZone = null): \DateTime
38
    {
39
        return new \DateTime('now', $timeZone);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getCurrentDatetimeImmutable(?\DateTimeZone $timeZone = null): \DateTimeImmutable
46
    {
47
        return new \DateTimeImmutable('now', $timeZone);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getDatesFromDateRangeAsArrayOfObjects(DateRangeInterface $dateRange): array
54
    {
55
        $cacheKeyForDateRange = $this->getCacheKeyForDateRange($dateRange);
56
57
        if (!isset($this->datesCache[$cacheKeyForDateRange])) {
58
            $since = DateTimeCloner::cloneIntoDateTime($dateRange->getSince());
59
            $since->setTime(0, 0);
60
61
            $till = DateTimeCloner::cloneIntoDateTime($dateRange->getTill());
62
            $till->setTime(23, 59, 59);
63
64
            $datesAsObjects = [];
65
            $period = new \DatePeriod($since, new \DateInterval('P1D'), $till);
66
            foreach ($period as $date) {
67
                $datesAsObjects[] = $date;
68
            }
69
70
            $this->datesCache[$cacheKeyForDateRange] = $datesAsObjects;
71
        }
72
73
        return $this->datesCache[$cacheKeyForDateRange];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getDatesFromDateRangeAsArrayOfStrings(DateRangeInterface $dateRange): array
80
    {
81
        $datesAsObjects = $this->getDatesFromDateRangeAsArrayOfObjects($dateRange);
82
83
        $datesAsStrings = [];
84
        foreach ($datesAsObjects as $datesAsObject) {
85
            $datesAsStrings[] = $datesAsObject->format(self::INTERNAL_DATE_FORMAT);
86
        }
87
88
        return $datesAsStrings;
89
    }
90
91
    /**
92
     * @param DateRangeInterface $dateRange
93
     *
94
     * @return string
95
     */
96
    private function getCacheKeyForDateRange(DateRangeInterface $dateRange): string
97
    {
98
        $since = $dateRange->getSince();
99
        $till = $dateRange->getTill();
100
101
        return \sprintf(
102
            '%s_%s_%s_%s',
103
            $since->format(self::INTERNAL_DATE_FORMAT),
104
            $since->getTimezone()->getName(),
105
            $till->format(self::INTERNAL_DATE_FORMAT),
106
            $till->getTimezone()->getName()
107
        );
108
    }
109
}
110