|
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\Week; |
|
11
|
|
|
|
|
12
|
|
|
use GpsLab\Component\Interval\Exception\IncorrectIntervalException; |
|
13
|
|
|
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException; |
|
14
|
|
|
use GpsLab\Component\Interval\IntervalInterface; |
|
15
|
|
|
use GpsLab\Component\Interval\IntervalType; |
|
16
|
|
|
|
|
17
|
|
View Code Duplication |
class WeekInterval implements IntervalInterface |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
const REGEXP = '/^ |
|
23
|
|
|
(?:\(|\[) # start type char |
|
24
|
|
|
\s* |
|
25
|
|
|
(?<start>\d{4}-\d{2}-\d{2}) # start point |
|
26
|
|
|
\s*,\s* # separator |
|
27
|
|
|
(?<end>\d{4}-\d{2}-\d{2}) # end point |
|
28
|
|
|
\s* |
|
29
|
|
|
(?:\)|\]) # end type char |
|
30
|
|
|
$/x'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var IntervalType |
|
34
|
|
|
*/ |
|
35
|
|
|
private $type; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var WeekIntervalComparator |
|
39
|
|
|
*/ |
|
40
|
|
|
private $comparator; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var WeekIntervalPoint |
|
44
|
|
|
*/ |
|
45
|
|
|
private $start; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var WeekIntervalPoint |
|
49
|
|
|
*/ |
|
50
|
|
|
private $end; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param WeekIntervalPoint $start |
|
54
|
|
|
* @param WeekIntervalPoint $end |
|
55
|
|
|
* @param IntervalType $type |
|
56
|
|
|
*/ |
|
57
|
|
|
private function __construct(WeekIntervalPoint $start, WeekIntervalPoint $end, IntervalType $type) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($start->gte($end)) { |
|
60
|
|
|
throw IncorrectIntervalException::create(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->type = $type; |
|
64
|
|
|
$this->start = $start; |
|
65
|
|
|
$this->end = $end; |
|
66
|
|
|
$this->comparator = new WeekIntervalComparator($this); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \DateTime $start |
|
71
|
|
|
* @param \DateTime $end |
|
72
|
|
|
* @param IntervalType $type |
|
73
|
|
|
* |
|
74
|
|
|
* @return self |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function create(\DateTime $start, \DateTime $end, IntervalType $type) |
|
77
|
|
|
{ |
|
78
|
|
|
return new self(new WeekIntervalPoint($start), new WeekIntervalPoint($end), $type); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param \DateTime $start |
|
83
|
|
|
* @param \DateTime $end |
|
84
|
|
|
* |
|
85
|
|
|
* @return self |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function closed(\DateTime $start, \DateTime $end) |
|
88
|
|
|
{ |
|
89
|
|
|
return static::create($start, $end, IntervalType::closed()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param \DateTime $start |
|
94
|
|
|
* @param \DateTime $end |
|
95
|
|
|
* |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function halfClosed(\DateTime $start, \DateTime $end) |
|
99
|
|
|
{ |
|
100
|
|
|
return static::create($start, $end, IntervalType::halfClosed()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param \DateTime $start |
|
105
|
|
|
* @param \DateTime $end |
|
106
|
|
|
* |
|
107
|
|
|
* @return self |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function halfOpen(\DateTime $start, \DateTime $end) |
|
110
|
|
|
{ |
|
111
|
|
|
return static::create($start, $end, IntervalType::halfOpen()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param \DateTime $start |
|
116
|
|
|
* @param \DateTime $end |
|
117
|
|
|
* |
|
118
|
|
|
* @return self |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function open(\DateTime $start, \DateTime $end) |
|
121
|
|
|
{ |
|
122
|
|
|
return static::create($start, $end, IntervalType::open()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Create interval from string. |
|
127
|
|
|
* |
|
128
|
|
|
* Example formats for all interval types: |
|
129
|
|
|
* [2016-12-09, 2016-12-21] |
|
130
|
|
|
* (2015-03-07, 2015-10-19] |
|
131
|
|
|
* [2014-09-11, 2015-02-08) |
|
132
|
|
|
* (2013-10-02, 2013-10-30) |
|
133
|
|
|
* |
|
134
|
|
|
* Spaces are ignored in format. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $string |
|
137
|
|
|
* |
|
138
|
|
|
* @return self |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function fromString($string) |
|
141
|
|
|
{ |
|
142
|
|
|
if (!preg_match(self::REGEXP, $string, $match)) { |
|
143
|
|
|
throw InvalidIntervalFormatException::create('[YYYY-MM-DD, YYYY-MM-DD]', $string); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return self::create( |
|
147
|
|
|
new \DateTime($match['start']), |
|
148
|
|
|
new \DateTime($match['end']), |
|
149
|
|
|
IntervalType::fromString($string) |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param \DateTime $point |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
|
|
public function contains(\DateTime $point) |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->comparator->contains(new WeekIntervalPoint($point)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param WeekInterval $interval |
|
165
|
|
|
* @param bool $check_interval_type |
|
166
|
|
|
* |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
|
|
public function intersect(WeekInterval $interval, $check_interval_type = true) |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->comparator->intersect($interval, $check_interval_type); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param WeekInterval $interval |
|
176
|
|
|
* |
|
177
|
|
|
* @return WeekInterval|null |
|
178
|
|
|
*/ |
|
179
|
|
|
public function intersectInterval(WeekInterval $interval) |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->comparator->intersectInterval($interval); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* The point is before the interval |
|
186
|
|
|
* |
|
187
|
|
|
* @param \DateTime $point |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
|
|
public function before(\DateTime $point) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->comparator->before(new WeekIntervalPoint($point)); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* The point is after the interval |
|
198
|
|
|
* |
|
199
|
|
|
* @param \DateTime $point |
|
200
|
|
|
* |
|
201
|
|
|
* @return bool |
|
202
|
|
|
*/ |
|
203
|
|
|
public function after(\DateTime $point) |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->comparator->after(new WeekIntervalPoint($point)); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @return IntervalType |
|
210
|
|
|
*/ |
|
211
|
|
|
public function type() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->type; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return \DateTime |
|
218
|
|
|
*/ |
|
219
|
|
|
public function start() |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->start->value(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return \DateTime |
|
226
|
|
|
*/ |
|
227
|
|
|
public function end() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->end->value(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return WeekIntervalPoint |
|
234
|
|
|
*/ |
|
235
|
|
|
public function startPoint() |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->start; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return WeekIntervalPoint |
|
242
|
|
|
*/ |
|
243
|
|
|
public function endPoint() |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->end; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
|
|
public function __toString() |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->type->getReadable($this); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
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.