Completed
Push — master ( 4a55b4...d96490 )
by Artem
01:40
created

getDatesFromDateRangeAsArrayOfObjects()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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