|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* GpsLab component. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace GpsLab\Component\Interval\Time; |
|
11
|
|
|
|
|
12
|
|
|
use GpsLab\Component\Interval\IntervalType; |
|
13
|
|
|
|
|
14
|
|
View Code Duplication |
class TimeIntervalComparator |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var TimeInterval |
|
18
|
|
|
*/ |
|
19
|
|
|
private $interval; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param TimeInterval $interval |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(TimeInterval $interval) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->interval = $interval; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param TimeIntervalPoint $point |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function contains(TimeIntervalPoint $point) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->interval->startPoint()->eq($point)) { |
|
37
|
|
|
return !$this->interval->type()->startExcluded(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($this->interval->endPoint()->eq($point)) { |
|
41
|
|
|
return !$this->interval->type()->endExcluded(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this->interval->startPoint()->lt($point) && $this->interval->endPoint()->gt($point); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param TimeInterval $interval |
|
49
|
|
|
* @param bool $check_interval_type |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function intersect(TimeInterval $interval, $check_interval_type = true) |
|
54
|
|
|
{ |
|
55
|
|
|
if ( |
|
56
|
|
|
$this->interval->startPoint()->gt($interval->endPoint()) || |
|
57
|
|
|
$this->interval->endPoint()->lt($interval->startPoint()) |
|
58
|
|
|
) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($check_interval_type) { |
|
63
|
|
|
if ($this->interval->startPoint()->eq($interval->endPoint())) { |
|
64
|
|
|
return !$this->interval->type()->startExcluded() && !$interval->type()->endExcluded(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->interval->endPoint()->eq($interval->startPoint())) { |
|
68
|
|
|
return !$this->interval->type()->endExcluded() && !$interval->type()->startExcluded(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param TimeInterval $interval |
|
77
|
|
|
* |
|
78
|
|
|
* @return TimeInterval|null |
|
79
|
|
|
*/ |
|
80
|
|
|
public function intersectInterval(TimeInterval $interval) |
|
81
|
|
|
{ |
|
82
|
|
|
// intervals is not intersect or impossible create interval from one point |
|
83
|
|
|
if ( |
|
84
|
|
|
$this->interval->startPoint()->gte($interval->endPoint()) || |
|
85
|
|
|
$this->interval->endPoint()->lte($interval->startPoint()) |
|
86
|
|
|
) { |
|
87
|
|
|
// ignore closed intervals: |
|
88
|
|
|
// [a, b] | [b, c] = [b, b] = (b-1, b+1) |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$type = IntervalType::TYPE_CLOSED; |
|
93
|
|
|
|
|
94
|
|
|
if ($this->interval->startPoint()->lt($interval->startPoint())) { |
|
95
|
|
|
$start = $interval->startPoint(); |
|
96
|
|
|
if ($interval->type()->startExcluded()) { |
|
97
|
|
|
$type |= IntervalType::TYPE_START_EXCLUDED; |
|
98
|
|
|
} |
|
99
|
|
|
} else { |
|
100
|
|
|
$start = $this->interval->startPoint(); |
|
101
|
|
|
if ($this->interval->type()->startExcluded()) { |
|
102
|
|
|
$type |= IntervalType::TYPE_START_EXCLUDED; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->interval->endPoint()->gt($interval->endPoint())) { |
|
107
|
|
|
$end = $interval->endPoint(); |
|
108
|
|
|
if ($interval->type()->endExcluded()) { |
|
109
|
|
|
$type |= IntervalType::TYPE_END_EXCLUDED; |
|
110
|
|
|
} |
|
111
|
|
|
} else { |
|
112
|
|
|
$end = $this->interval->endPoint(); |
|
113
|
|
|
if ($this->interval->type()->endExcluded()) { |
|
114
|
|
|
$type |= IntervalType::TYPE_END_EXCLUDED; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return TimeInterval::create($start->value(), $end->value(), IntervalType::create($type)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* The point is before the interval |
|
123
|
|
|
* |
|
124
|
|
|
* @param TimeIntervalPoint $point |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function before(TimeIntervalPoint $point) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->interval->startPoint()->gt($point); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* The point is after the interval |
|
135
|
|
|
* |
|
136
|
|
|
* @param TimeIntervalPoint $point |
|
137
|
|
|
* |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
public function after(TimeIntervalPoint $point) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->interval->endPoint()->lt($point); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.