Skip to content

Commit 8a0335e

Browse files
authored
Refactor RangeState interface (#7)
Replace comparison methods with compareStartTime and compareEndTime.
1 parent af3bdcf commit 8a0335e

File tree

7 files changed

+70
-129
lines changed

7 files changed

+70
-129
lines changed

src/DateRange.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Zee\DateRange;
1212

13+
use DateTimeImmutable;
1314
use DateTimeInterface;
1415
use JsonSerializable;
1516
use Zee\DateRange\States\RangeState;
@@ -140,30 +141,34 @@ public function setEndTime(DateTimeInterface $time): DateRangeInterface
140141
*/
141142
public function isStartAt(DateTimeInterface $time): bool
142143
{
143-
return $this->state->isStartAt($time);
144+
return $this->hasStartTime() && $this->state->compareStartTime($time) === 0;
144145
}
145146

146147
/**
147148
* {@inheritdoc}
148149
*/
149150
public function isEndAt(DateTimeInterface $time): bool
150151
{
151-
return $this->state->isEndAt($time);
152+
return $this->hasEndTime() && $this->state->compareEndTime($time) === 0;
152153
}
153154

154155
/**
155156
* {@inheritdoc}
156157
*/
157158
public function isStarted(): bool
158159
{
159-
return $this->state->isStarted();
160+
if ($this->hasStartTime()) {
161+
return $this->state->compareStartTime(new DateTimeImmutable()) <= 0;
162+
} else {
163+
return $this->hasEndTime();
164+
}
160165
}
161166

162167
/**
163168
* {@inheritdoc}
164169
*/
165170
public function isEnded(): bool
166171
{
167-
return $this->state->isEnded();
172+
return $this->hasEndTime() && $this->state->compareEndTime(new DateTimeImmutable()) < 0;
168173
}
169174
}

src/States/FiniteRange.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Zee\DateRange\States;
44

5-
use DateTimeImmutable;
65
use DateTimeInterface;
76
use Zee\DateRange\DateRangeException;
87

@@ -102,32 +101,16 @@ public function setEndTime(DateTimeInterface $time): RangeState
102101
/**
103102
* {@inheritdoc}
104103
*/
105-
public function isStartAt(DateTimeInterface $time): bool
104+
public function compareStartTime(DateTimeInterface $time): int
106105
{
107-
return $this->startTime->getTimestamp() === $time->getTimestamp();
106+
return $this->startTime->getTimestamp() <=> $time->getTimestamp();
108107
}
109108

110109
/**
111110
* {@inheritdoc}
112111
*/
113-
public function isEndAt(DateTimeInterface $time): bool
112+
public function compareEndTime(DateTimeInterface $time): int
114113
{
115-
return $this->endTime->getTimestamp() === $time->getTimestamp();
116-
}
117-
118-
/**
119-
* {@inheritdoc}
120-
*/
121-
public function isStarted(): bool
122-
{
123-
return $this->startTime <= new DateTimeImmutable();
124-
}
125-
126-
/**
127-
* {@inheritdoc}
128-
*/
129-
public function isEnded(): bool
130-
{
131-
return $this->endTime <= new DateTimeImmutable();
114+
return $this->endTime->getTimestamp() <=> $time->getTimestamp();
132115
}
133116
}

src/States/InfiniteEndRange.php

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Zee\DateRange\States;
44

5-
use DateTimeImmutable;
65
use DateTimeInterface;
7-
use Zee\DateRange\DateRangeException;
86

97
/**
108
* Class InfiniteEndRange.
@@ -48,14 +46,6 @@ public function hasStartTime(): bool
4846
return true;
4947
}
5048

51-
/**
52-
* {@inheritdoc}
53-
*/
54-
public function hasEndTime(): bool
55-
{
56-
return false;
57-
}
58-
5949
/**
6050
* {@inheritdoc}
6151
*/
@@ -64,14 +54,6 @@ public function getStartTime(): DateTimeInterface
6454
return $this->startTime;
6555
}
6656

67-
/**
68-
* {@inheritdoc}
69-
*/
70-
public function getEndTime(): DateTimeInterface
71-
{
72-
throw new DateRangeException('Date range is undefined');
73-
}
74-
7557
/**
7658
* {@inheritdoc}
7759
*/
@@ -91,16 +73,8 @@ public function setEndTime(DateTimeInterface $time): RangeState
9173
/**
9274
* {@inheritdoc}
9375
*/
94-
public function isStartAt(DateTimeInterface $time): bool
95-
{
96-
return $this->startTime->getTimestamp() === $time->getTimestamp();
97-
}
98-
99-
/**
100-
* {@inheritdoc}
101-
*/
102-
public function isStarted(): bool
76+
public function compareStartTime(DateTimeInterface $time): int
10377
{
104-
return $this->startTime <= new DateTimeImmutable();
78+
return $this->startTime->getTimestamp() <=> $time->getTimestamp();
10579
}
10680
}

src/States/InfiniteStartRange.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Zee\DateRange\States;
44

5-
use DateTimeImmutable;
65
use DateTimeInterface;
76
use Zee\DateRange\DateRangeException;
87

@@ -40,14 +39,6 @@ public function jsonSerialize(): array
4039
return ['startTime' => null, 'endTime' => $this->endTime->format('c')];
4140
}
4241

43-
/**
44-
* {@inheritdoc}
45-
*/
46-
public function hasStartTime(): bool
47-
{
48-
return false;
49-
}
50-
5142
/**
5243
* {@inheritdoc}
5344
*/
@@ -56,14 +47,6 @@ public function hasEndTime(): bool
5647
return true;
5748
}
5849

59-
/**
60-
* {@inheritdoc}
61-
*/
62-
public function getStartTime(): DateTimeInterface
63-
{
64-
throw new DateRangeException('Date range is undefined');
65-
}
66-
6750
/**
6851
* {@inheritdoc}
6952
*/
@@ -91,24 +74,8 @@ public function setEndTime(DateTimeInterface $time): RangeState
9174
/**
9275
* {@inheritdoc}
9376
*/
94-
public function isEndAt(DateTimeInterface $time): bool
95-
{
96-
return $this->endTime->getTimestamp() === $time->getTimestamp();
97-
}
98-
99-
/**
100-
* {@inheritdoc}
101-
*/
102-
public function isStarted(): bool
103-
{
104-
return true;
105-
}
106-
107-
/**
108-
* {@inheritdoc}
109-
*/
110-
public function isEnded(): bool
77+
public function compareEndTime(DateTimeInterface $time): int
11178
{
112-
return $this->endTime <= new DateTimeImmutable();
79+
return $this->endTime->getTimestamp() <=> $time->getTimestamp();
11380
}
11481
}

src/States/RangeState.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,26 @@ public function setStartTime(DateTimeInterface $time): RangeState;
5757
public function setEndTime(DateTimeInterface $time): RangeState;
5858

5959
/**
60+
* Compares start time with specific time.
61+
*
62+
* Returns 0 if times are equals, negative if start time is less than given time,
63+
* and positive if start time is greater.
64+
*
6065
* @param DateTimeInterface $time
6166
*
62-
* @return bool
67+
* @return int
6368
*/
64-
public function isStartAt(DateTimeInterface $time): bool;
69+
public function compareStartTime(DateTimeInterface $time): int;
6570

6671
/**
72+
* Compares end time with specific time.
73+
*
74+
* Returns 0 if times are equals, negative if end time is less than given time
75+
* and positive if end time is greater.
76+
*
6777
* @param DateTimeInterface $time
6878
*
69-
* @return bool
70-
*/
71-
public function isEndAt(DateTimeInterface $time): bool;
72-
73-
/**
74-
* @return bool
75-
*/
76-
public function isStarted(): bool;
77-
78-
/**
79-
* @return bool
79+
* @return int
8080
*/
81-
public function isEnded(): bool;
81+
public function compareEndTime(DateTimeInterface $time): int;
8282
}

src/States/UndefinedRange.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public function hasEndTime(): bool
4747
*/
4848
public function getStartTime(): DateTimeInterface
4949
{
50-
throw new DateRangeException('Date range is undefined');
50+
throw new DateRangeException('Range start is undefined');
5151
}
5252

5353
/**
5454
* {@inheritdoc}
5555
*/
5656
public function getEndTime(): DateTimeInterface
5757
{
58-
throw new DateRangeException('Date range is undefined');
58+
throw new DateRangeException('Range end is undefined');
5959
}
6060

6161
/**
@@ -77,32 +77,16 @@ public function setEndTime(DateTimeInterface $time): RangeState
7777
/**
7878
* {@inheritdoc}
7979
*/
80-
public function isStartAt(DateTimeInterface $time): bool
80+
public function compareStartTime(DateTimeInterface $time): int
8181
{
82-
return false;
83-
}
84-
85-
/**
86-
* {@inheritdoc}
87-
*/
88-
public function isEndAt(DateTimeInterface $time): bool
89-
{
90-
return false;
82+
throw new DateRangeException('Range start is undefined');
9183
}
9284

9385
/**
9486
* {@inheritdoc}
9587
*/
96-
public function isStarted(): bool
88+
public function compareEndTime(DateTimeInterface $time): int
9789
{
98-
return false;
99-
}
100-
101-
/**
102-
* {@inheritdoc}
103-
*/
104-
public function isEnded(): bool
105-
{
106-
return false;
90+
throw new DateRangeException('Range end is undefined');
10791
}
10892
}

0 commit comments

Comments
 (0)