Skip to content

Commit ab9fdbc

Browse files
authored
Refactor states (#10)
1 parent 17e075a commit ab9fdbc

File tree

6 files changed

+75
-120
lines changed

6 files changed

+75
-120
lines changed

src/DateRange.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public function __construct(DateTimeInterface $startTime = null, DateTimeInterfa
5555
*/
5656
public function __toString(): string
5757
{
58-
return (string) $this->state;
58+
return sprintf(
59+
'%s/%s',
60+
$this->state->formatStartTime('c') ?: '-',
61+
$this->state->formatEndTime('c') ?: '-'
62+
);
5963
}
6064

6165
/**
@@ -82,7 +86,10 @@ public function __debugInfo()
8286
*/
8387
public function jsonSerialize(): array
8488
{
85-
return $this->state->jsonSerialize();
89+
return [
90+
'startTime' => $this->state->formatStartTime(),
91+
'endTime' => $this->state->formatEndTime(),
92+
];
8693
}
8794

8895
/**

src/States/FiniteRange.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Class FiniteRange.
1010
*/
11-
final class FiniteRange extends UndefinedRange
11+
final class FiniteRange extends RangeState
1212
{
1313
/**
1414
* @var DateTimeInterface
@@ -34,22 +34,6 @@ public function __construct(DateTimeInterface $startTime, DateTimeInterface $end
3434
$this->endTime = $endTime;
3535
}
3636

37-
/**
38-
* {@inheritdoc}
39-
*/
40-
public function __toString(): string
41-
{
42-
return sprintf('%s/%s', $this->startTime->format('c'), $this->endTime->format('c'));
43-
}
44-
45-
/**
46-
* {@inheritdoc}
47-
*/
48-
public function jsonSerialize(): array
49-
{
50-
return ['startTime' => $this->startTime->format('c'), 'endTime' => $this->endTime->format('c')];
51-
}
52-
5337
/**
5438
* {@inheritdoc}
5539
*/
@@ -97,20 +81,4 @@ public function setEndTime(DateTimeInterface $time): RangeState
9781
{
9882
return new FiniteRange($this->startTime, $time);
9983
}
100-
101-
/**
102-
* {@inheritdoc}
103-
*/
104-
public function compareStartTime(DateTimeInterface $time): int
105-
{
106-
return $this->startTime->getTimestamp() <=> $time->getTimestamp();
107-
}
108-
109-
/**
110-
* {@inheritdoc}
111-
*/
112-
public function compareEndTime(DateTimeInterface $time): int
113-
{
114-
return $this->endTime->getTimestamp() <=> $time->getTimestamp();
115-
}
11684
}

src/States/InfiniteEndRange.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace Zee\DateRange\States;
44

55
use DateTimeInterface;
6+
use Zee\DateRange\DateRangeException;
67

78
/**
89
* Class InfiniteEndRange.
910
*/
10-
final class InfiniteEndRange extends UndefinedRange
11+
final class InfiniteEndRange extends RangeState
1112
{
1213
/**
1314
* @var DateTimeInterface
@@ -25,33 +26,33 @@ public function __construct(DateTimeInterface $startTime)
2526
/**
2627
* {@inheritdoc}
2728
*/
28-
public function __toString(): string
29+
public function hasStartTime(): bool
2930
{
30-
return sprintf('%s/-', $this->startTime->format('c'));
31+
return true;
3132
}
3233

3334
/**
3435
* {@inheritdoc}
3536
*/
36-
public function jsonSerialize(): array
37+
public function hasEndTime(): bool
3738
{
38-
return ['startTime' => $this->startTime->format('c'), 'endTime' => null];
39+
return false;
3940
}
4041

4142
/**
4243
* {@inheritdoc}
4344
*/
44-
public function hasStartTime(): bool
45+
public function getStartTime(): DateTimeInterface
4546
{
46-
return true;
47+
return $this->startTime;
4748
}
4849

4950
/**
5051
* {@inheritdoc}
5152
*/
52-
public function getStartTime(): DateTimeInterface
53+
public function getEndTime(): DateTimeInterface
5354
{
54-
return $this->startTime;
55+
throw new DateRangeException('Range end is undefined');
5556
}
5657

5758
/**
@@ -73,8 +74,8 @@ public function setEndTime(DateTimeInterface $time): RangeState
7374
/**
7475
* {@inheritdoc}
7576
*/
76-
public function compareStartTime(DateTimeInterface $time): int
77+
public function formatEndTime(string $format = 'c'): ?string
7778
{
78-
return $this->startTime->getTimestamp() <=> $time->getTimestamp();
79+
return null;
7980
}
8081
}

src/States/InfiniteStartRange.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Class InfiniteStartRange.
1010
*/
11-
final class InfiniteStartRange extends UndefinedRange
11+
final class InfiniteStartRange extends RangeState
1212
{
1313
/**
1414
* @var DateTimeInterface
@@ -26,25 +26,17 @@ public function __construct(DateTimeInterface $endTime)
2626
/**
2727
* {@inheritdoc}
2828
*/
29-
public function __toString(): string
30-
{
31-
return sprintf('-/%s', $this->endTime->format('c'));
32-
}
33-
34-
/**
35-
* {@inheritdoc}
36-
*/
37-
public function jsonSerialize(): array
29+
public function hasEndTime(): bool
3830
{
39-
return ['startTime' => null, 'endTime' => $this->endTime->format('c')];
31+
return true;
4032
}
4133

4234
/**
4335
* {@inheritdoc}
4436
*/
45-
public function hasEndTime(): bool
37+
public function getStartTime(): DateTimeInterface
4638
{
47-
return true;
39+
throw new DateRangeException('Range start is undefined');
4840
}
4941

5042
/**
@@ -74,8 +66,8 @@ public function setEndTime(DateTimeInterface $time): RangeState
7466
/**
7567
* {@inheritdoc}
7668
*/
77-
public function compareEndTime(DateTimeInterface $time): int
69+
public function formatStartTime(string $format = 'c'): ?string
7870
{
79-
return $this->endTime->getTimestamp() <=> $time->getTimestamp();
71+
return null;
8072
}
8173
}

src/States/RangeState.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,51 @@
33
namespace Zee\DateRange\States;
44

55
use DateTimeInterface;
6-
use JsonSerializable;
76

87
/**
98
* Interface RangeState.
109
*/
11-
interface RangeState extends JsonSerializable
10+
abstract class RangeState
1211
{
13-
/**
14-
* Returns string representation of range.
15-
*
16-
* @return string
17-
*/
18-
public function __toString(): string;
19-
20-
/**
21-
* {@inheritdoc}
22-
*/
23-
public function jsonSerialize(): array;
24-
2512
/**
2613
* @return bool
2714
*/
28-
public function hasStartTime(): bool;
15+
public function hasStartTime(): bool
16+
{
17+
return false;
18+
}
2919

3020
/**
3121
* @return bool
3222
*/
33-
public function hasEndTime(): bool;
23+
public function hasEndTime(): bool
24+
{
25+
return false;
26+
}
3427

3528
/**
3629
* @return DateTimeInterface
3730
*/
38-
public function getStartTime(): DateTimeInterface;
31+
abstract public function getStartTime(): DateTimeInterface;
3932

4033
/**
4134
* @return DateTimeInterface
4235
*/
43-
public function getEndTime(): DateTimeInterface;
36+
abstract public function getEndTime(): DateTimeInterface;
4437

4538
/**
4639
* @param DateTimeInterface $time
4740
*
4841
* @return RangeState
4942
*/
50-
public function setStartTime(DateTimeInterface $time): RangeState;
43+
abstract public function setStartTime(DateTimeInterface $time): RangeState;
5144

5245
/**
5346
* @param DateTimeInterface $time
5447
*
5548
* @return RangeState
5649
*/
57-
public function setEndTime(DateTimeInterface $time): RangeState;
50+
abstract public function setEndTime(DateTimeInterface $time): RangeState;
5851

5952
/**
6053
* Compares start time with specific time.
@@ -66,7 +59,10 @@ public function setEndTime(DateTimeInterface $time): RangeState;
6659
*
6760
* @return int
6861
*/
69-
public function compareStartTime(DateTimeInterface $time): int;
62+
public function compareStartTime(DateTimeInterface $time): int
63+
{
64+
return $this->getStartTime()->getTimestamp() <=> $time->getTimestamp();
65+
}
7066

7167
/**
7268
* Compares end time with specific time.
@@ -78,5 +74,28 @@ public function compareStartTime(DateTimeInterface $time): int;
7874
*
7975
* @return int
8076
*/
81-
public function compareEndTime(DateTimeInterface $time): int;
77+
public function compareEndTime(DateTimeInterface $time): int
78+
{
79+
return $this->getEndTime()->getTimestamp() <=> $time->getTimestamp();
80+
}
81+
82+
/**
83+
* @param string $format
84+
*
85+
* @return null|string
86+
*/
87+
public function formatStartTime(string $format = 'c'): ?string
88+
{
89+
return $this->getStartTime()->format($format);
90+
}
91+
92+
/**
93+
* @param string $format
94+
*
95+
* @return null|string
96+
*/
97+
public function formatEndTime(string $format = 'c'): ?string
98+
{
99+
return $this->getEndTime()->format($format);
100+
}
82101
}

src/States/UndefinedRange.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,8 @@
88
/**
99
* Class UndefinedRange.
1010
*/
11-
class UndefinedRange implements RangeState
11+
final class UndefinedRange extends RangeState
1212
{
13-
/**
14-
* {@inheritdoc}
15-
*/
16-
public function __toString(): string
17-
{
18-
return '-/-';
19-
}
20-
21-
/**
22-
* {@inheritdoc}
23-
*/
24-
public function jsonSerialize(): array
25-
{
26-
return ['startTime' => null, 'endTime' => null];
27-
}
28-
29-
/**
30-
* {@inheritdoc}
31-
*/
32-
public function hasStartTime(): bool
33-
{
34-
return false;
35-
}
36-
37-
/**
38-
* {@inheritdoc}
39-
*/
40-
public function hasEndTime(): bool
41-
{
42-
return false;
43-
}
44-
4513
/**
4614
* {@inheritdoc}
4715
*/
@@ -77,16 +45,16 @@ public function setEndTime(DateTimeInterface $time): RangeState
7745
/**
7846
* {@inheritdoc}
7947
*/
80-
public function compareStartTime(DateTimeInterface $time): int
48+
public function formatStartTime(string $format = 'c'): ?string
8149
{
82-
throw new DateRangeException('Range start is undefined');
50+
return null;
8351
}
8452

8553
/**
8654
* {@inheritdoc}
8755
*/
88-
public function compareEndTime(DateTimeInterface $time): int
56+
public function formatEndTime(string $format = 'c'): ?string
8957
{
90-
throw new DateRangeException('Range end is undefined');
58+
return null;
9159
}
9260
}

0 commit comments

Comments
 (0)