1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zee\DateRange\States; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
use Zee\DateRange\DateRangeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class FiniteRange. |
11
|
|
|
*/ |
12
|
|
|
final class FiniteRange extends UndefinedRange |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DateTimeInterface |
16
|
|
|
*/ |
17
|
|
|
private $startTime; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var DateTimeInterface |
21
|
|
|
*/ |
22
|
|
|
private $endTime; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param DateTimeInterface $startTime |
26
|
|
|
* @param DateTimeInterface $endTime |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct(DateTimeInterface $startTime, DateTimeInterface $endTime) |
29
|
|
|
{ |
30
|
6 |
|
if ($endTime <= $startTime) { |
31
|
2 |
|
throw new DateRangeException('Invalid end time, must be after start'); |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
$this->startTime = $startTime; |
35
|
4 |
|
$this->endTime = $endTime; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function __toString(): string |
42
|
|
|
{ |
43
|
1 |
|
return sprintf('%s/%s', $this->startTime->format('c'), $this->endTime->format('c')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function jsonSerialize(): array |
50
|
|
|
{ |
51
|
1 |
|
return ['startTime' => $this->startTime->format('c'), 'endTime' => $this->endTime->format('c')]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
2 |
|
public function hasStartTime(): bool |
58
|
|
|
{ |
59
|
2 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
2 |
|
public function hasEndTime(): bool |
66
|
|
|
{ |
67
|
2 |
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
4 |
|
public function getStartTime(): DateTimeInterface |
74
|
|
|
{ |
75
|
4 |
|
return $this->startTime; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
4 |
|
public function getEndTime(): DateTimeInterface |
82
|
|
|
{ |
83
|
4 |
|
return $this->endTime; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function setStartTime(DateTimeInterface $time): RangeState |
90
|
|
|
{ |
91
|
1 |
|
return new FiniteRange($time, $this->endTime); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function setEndTime(DateTimeInterface $time): RangeState |
98
|
|
|
{ |
99
|
1 |
|
return new FiniteRange($this->startTime, $time); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function isStartAt(DateTimeInterface $time): bool |
106
|
|
|
{ |
107
|
1 |
|
return $this->startTime->getTimestamp() === $time->getTimestamp(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
1 |
|
public function isEndAt(DateTimeInterface $time): bool |
114
|
|
|
{ |
115
|
1 |
|
return $this->endTime->getTimestamp() === $time->getTimestamp(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function isStarted(): bool |
122
|
|
|
{ |
123
|
1 |
|
return $this->startTime <= new DateTimeImmutable(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
1 |
|
public function isEnded(): bool |
130
|
|
|
{ |
131
|
1 |
|
return $this->endTime <= new DateTimeImmutable(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|