1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Remorhaz\IntRangeSets; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Immutable non-empty range of integers. |
9
|
|
|
* |
10
|
|
|
* @psalm-immutable |
11
|
|
|
*/ |
12
|
|
|
final class Range implements RangeInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $start; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $finish; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param int $start First value of the range. |
27
|
|
|
* @param int|null $finish Last value of the range. If null than equals first value, otherwise must be greater or |
28
|
|
|
* equal than first value. |
29
|
|
|
* @throws Exception\InvalidRangeException |
30
|
|
|
*/ |
31
|
54 |
|
public function __construct(int $start, ?int $finish = null) |
32
|
|
|
{ |
33
|
54 |
|
if (!isset($finish)) { |
34
|
2 |
|
$finish = $start; |
35
|
52 |
|
} elseif ($finish < $start) { |
36
|
3 |
|
throw new Exception\InvalidRangeException($start, $finish); |
37
|
|
|
} |
38
|
|
|
|
39
|
53 |
|
$this->start = $start; |
40
|
53 |
|
$this->finish = $finish; |
41
|
53 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
* |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
30 |
|
public function getStart(): int |
49
|
|
|
{ |
50
|
30 |
|
return $this->start; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
* |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
30 |
|
public function getFinish(): int |
59
|
|
|
{ |
60
|
30 |
|
return $this->finish; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
2 |
|
public function getLength(): int |
69
|
|
|
{ |
70
|
2 |
|
return $this->finish - $this->start + 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
* |
76
|
|
|
* @param RangeInterface $range |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
5 |
|
public function equals(RangeInterface $range): bool |
80
|
|
|
{ |
81
|
5 |
|
return $range->getStart() == $this->start && $range->getFinish() == $this->finish; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
* |
87
|
|
|
* @param int $value |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
6 |
|
public function containsValue(int $value): bool |
91
|
|
|
{ |
92
|
6 |
|
return $this->start <= $value && $value <= $this->finish; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
* |
98
|
|
|
* @param RangeInterface $range |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
11 |
|
public function contains(RangeInterface $range): bool |
102
|
|
|
{ |
103
|
11 |
|
return $range->getStart() >= $this->start && $range->getFinish() <= $this->finish; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
* |
109
|
|
|
* @param RangeInterface $range |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
11 |
|
public function intersects(RangeInterface $range): bool |
113
|
|
|
{ |
114
|
11 |
|
return $this->finish >= $range->getStart() && $range->getFinish() >= $this->start; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
* |
120
|
|
|
* @param RangeInterface $range |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
5 |
|
public function follows(RangeInterface $range): bool |
124
|
|
|
{ |
125
|
5 |
|
return $this->start == $range->getFinish() + 1; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritDoc} |
130
|
|
|
* |
131
|
|
|
* @return RangeSetInterface |
132
|
|
|
*/ |
133
|
1 |
|
public function asRangeSet(): RangeSetInterface |
134
|
|
|
{ |
135
|
1 |
|
return RangeSet::createUnsafe($this); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritDoc} |
140
|
|
|
* |
141
|
|
|
* @param int $value |
142
|
|
|
* @return RangeInterface |
143
|
|
|
*/ |
144
|
4 |
|
public function withStart(int $value): RangeInterface |
145
|
|
|
{ |
146
|
4 |
|
return new self($value, $this->finish); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
* |
152
|
|
|
* @param int $value |
153
|
|
|
* @return RangeInterface |
154
|
|
|
*/ |
155
|
4 |
|
public function withFinish(int $value): RangeInterface |
156
|
|
|
{ |
157
|
4 |
|
return new self($this->start, $value); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|