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\LogicException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* DateRange. |
19
|
|
|
* |
20
|
|
|
* @author Artem Henvald <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class DateRange implements DateRangeInterface |
23
|
|
|
{ |
24
|
|
|
private const INTERNAL_DATE_FORMAT = 'Y-m-d'; |
25
|
|
|
|
26
|
|
|
/** @var \DateTimeImmutable */ |
27
|
|
|
private $since; |
28
|
|
|
|
29
|
|
|
/** @var \DateTimeImmutable */ |
30
|
|
|
private $till; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \DateTimeInterface $since |
34
|
|
|
* @param \DateTimeInterface $till |
35
|
|
|
*/ |
36
|
|
|
public function __construct(\DateTimeInterface $since, \DateTimeInterface $till) |
37
|
|
|
{ |
38
|
|
|
$this->assertSameTimezones($since, $till); |
39
|
|
|
|
40
|
|
|
$this->since = \DateTimeImmutable::createFromFormat(\DateTime::RFC3339, $since->format(\DateTime::RFC3339), $since->getTimezone()); |
41
|
|
|
$this->till = \DateTimeImmutable::createFromFormat(\DateTime::RFC3339, $till->format(\DateTime::RFC3339), $till->getTimezone()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getSince(): \DateTimeImmutable |
48
|
|
|
{ |
49
|
|
|
return $this->since; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function getTill(): \DateTimeImmutable |
56
|
|
|
{ |
57
|
|
|
return $this->till; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function isEqual(DateRangeInterface $dateRange): bool |
64
|
|
|
{ |
65
|
|
|
return $this->since->format(self::INTERNAL_DATE_FORMAT) === $dateRange->getSince()->format(self::INTERNAL_DATE_FORMAT) |
66
|
|
|
&& $this->till->format(self::INTERNAL_DATE_FORMAT) === $dateRange->getTill()->format(self::INTERNAL_DATE_FORMAT) |
67
|
|
|
&& $this->since->getTimezone()->getName() === $dateRange->getTill()->getTimezone()->getName() |
68
|
|
|
&& $this->till->getTimezone()->getName() === $dateRange->getTill()->getTimezone()->getName() |
69
|
|
|
; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param \DateTimeInterface $since |
74
|
|
|
* @param \DateTimeInterface $till |
75
|
|
|
* |
76
|
|
|
* @throws LogicException |
77
|
|
|
*/ |
78
|
|
|
private function assertSameTimezones(\DateTimeInterface $since, \DateTimeInterface $till): void |
79
|
|
|
{ |
80
|
|
|
if ($since->getTimezone()->getName() !== $till->getTimezone()->getName()) { |
81
|
|
|
throw new LogicException('Dates have different timezones'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|