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