Completed
Push — master ( 3f2844...e02309 )
by Artem
04:26
created

DateTimeHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentDatetime() 0 4 1
A getCurrentDatetimeImmutable() 0 4 1
A getDatesFromDateRangeAsArrayOfObjects() 0 28 4
A getDatesFromDateRangeAsArrayOfStrings() 0 11 2
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
use Fresh\DateTime\Exception\UnexpectedValueException;
16
17
/**
18
 * DateTimeHelper.
19
 *
20
 * @author Artem Henvald <[email protected]>
21
 */
22
class DateTimeHelper
23
{
24
    private const INTERNAL_DATE_FORMAT = 'Y-m-d';
25
26
    /**
27
     * @param \DateTimeZone|null $timeZone
28
     *
29
     * @return \DateTime
30
     */
31
    public function getCurrentDatetime(?\DateTimeZone $timeZone = null): \DateTime
32
    {
33
        return new \DateTime('now', $timeZone);
34
    }
35
36
    /**
37
     * @param \DateTimeZone|null $timeZone
38
     *
39
     * @return \DateTimeImmutable
40
     */
41
    public function getCurrentDatetimeImmutable(?\DateTimeZone $timeZone = null): \DateTimeImmutable
42
    {
43
        return new \DateTimeImmutable('now', $timeZone);
44
    }
45
46
    /**
47
     * @param DateRange $dateRange
48
     *
49
     * @throws UnexpectedValueException
50
     *
51
     * @return \DateTimeImmutable[]
52
     */
53
    public function getDatesFromDateRangeAsArrayOfObjects(DateRange $dateRange): array
54
    {
55
        $datesAsObjects = [];
56
57
        if ($dateRange->getSince()->format(self::INTERNAL_DATE_FORMAT) !== $dateRange->getTill()->format(self::INTERNAL_DATE_FORMAT)) {
58
            $till = \DateTime::createFromFormat(
59
                'U',
60
                (string) $dateRange->getTill()->getTimestamp(),
61
                $dateRange->getTill()->getTimezone()
62
            );
63
64
            if (!$till instanceof \DateTime) {
65
                throw new UnexpectedValueException(\sprintf('Could not create %s object', \DateTime::class));
66
            }
67
68
            $period = new \DatePeriod($dateRange->getSince(), new \DateInterval('P1D'), $till);
69
70
            /** @var \DateTime $date */
71
            foreach ($period as $date) {
72
                $datesAsObjects[] = $date;
73
            }
74
        } else {
75
            // If since and till dates are equal, then only one date in range
76
            $datesAsObjects[] = $dateRange->getSince();
77
        }
78
79
        return \array_values($datesAsObjects);
80
    }
81
82
    /**
83
     * @param DateRange $dateRange
84
     *
85
     * @return string[]
86
     */
87
    public function getDatesFromDateRangeAsArrayOfStrings(DateRange $dateRange): array
88
    {
89
        $datesAsObjects = $this->getDatesFromDateRangeAsArrayOfObjects($dateRange);
90
91
        $datesAsStrings = [];
92
        foreach ($datesAsObjects as $datesAsObject) {
93
            $datesAsStrings[] = $datesAsObject->format(self::INTERNAL_DATE_FORMAT);
94
        }
95
96
        return $datesAsStrings;
97
    }
98
}
99