Code Duplication    Length = 275-275 lines in 5 locations

src/Date/DateInterval.php 1 location

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

src/DateTime/DateTimeInterval.php 1 location

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

src/Month/MonthInterval.php 1 location

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

src/Time/TimeInterval.php 1 location

@@ 19-293 (lines=275) @@
16
use GpsLab\Component\Interval\IntervalPointInterface;
17
use GpsLab\Component\Interval\IntervalType;
18
19
class TimeInterval implements ComparableIntervalInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    const REGEXP = '/^
25
        (?:\(|\[)                   # start type char
26
        \s*
27
        (?<start>\d{2}:\d{2}:\d{2}) # start point
28
        \s*,\s*                     # separator
29
        (?<end>\d{2}:\d{2}:\d{2})   # end point
30
        \s*
31
        (?:\)|\])                   # end type char
32
    $/x';
33
34
    /**
35
     * @var IntervalType
36
     */
37
    private $type;
38
39
    /**
40
     * @var IntervalComparator
41
     */
42
    private $comparator;
43
44
    /**
45
     * @var TimeIntervalPoint
46
     */
47
    private $start;
48
49
    /**
50
     * @var TimeIntervalPoint
51
     */
52
    private $end;
53
54
    /**
55
     * @param TimeIntervalPoint $start
56
     * @param TimeIntervalPoint $end
57
     * @param IntervalType $type
58
     */
59
    private function __construct(TimeIntervalPoint $start, TimeIntervalPoint $end, IntervalType $type)
60
    {
61
        if ($start->gte($end)) {
62
            throw IncorrectIntervalException::create();
63
        }
64
65
        $this->type = $type;
66
        $this->start = $start;
67
        $this->end = $end;
68
        $this->comparator = new IntervalComparator($this);
69
    }
70
71
    /**
72
     * @param \DateTime $start
73
     * @param \DateTime $end
74
     * @param IntervalType $type
75
     *
76
     * @return self
77
     */
78
    public static function create(\DateTime $start, \DateTime $end, IntervalType $type)
79
    {
80
        return new self(new TimeIntervalPoint($start), new TimeIntervalPoint($end), $type);
81
    }
82
83
    /**
84
     * @param \DateTime $start
85
     * @param \DateTime $end
86
     *
87
     * @return self
88
     */
89
    public static function closed(\DateTime $start, \DateTime $end)
90
    {
91
        return static::create($start, $end, IntervalType::closed());
92
    }
93
94
    /**
95
     * @param \DateTime $start
96
     * @param \DateTime $end
97
     *
98
     * @return self
99
     */
100
    public static function halfClosed(\DateTime $start, \DateTime $end)
101
    {
102
        return static::create($start, $end, IntervalType::halfClosed());
103
    }
104
105
    /**
106
     * @param \DateTime $start
107
     * @param \DateTime $end
108
     *
109
     * @return self
110
     */
111
    public static function halfOpen(\DateTime $start, \DateTime $end)
112
    {
113
        return static::create($start, $end, IntervalType::halfOpen());
114
    }
115
116
    /**
117
     * @param \DateTime $start
118
     * @param \DateTime $end
119
     *
120
     * @return self
121
     */
122
    public static function open(\DateTime $start, \DateTime $end)
123
    {
124
        return static::create($start, $end, IntervalType::open());
125
    }
126
127
    /**
128
     * Create interval from string.
129
     *
130
     * Example formats for all interval types:
131
     *   [02:55:00, 12:30:12]
132
     *   (12:04:45, 19:38:14]
133
     *   [17:31:09, 23:45:58)
134
     *   (15:03:37, 15:06:34)
135
     *
136
     * Spaces are ignored in format.
137
     *
138
     * @param string $string
139
     *
140
     * @return self
141
     */
142
    public static function fromString($string)
143
    {
144
        if (!preg_match(self::REGEXP, $string, $match)) {
145
            throw InvalidIntervalFormatException::create('[HH:II:SS, HH:II:SS]', $string);
146
        }
147
148
        return self::create(
149
            new \DateTime($match['start']),
150
            new \DateTime($match['end']),
151
            IntervalType::fromString($string)
152
        );
153
    }
154
155
    /**
156
     * @param \DateTime $point
157
     *
158
     * @return bool
159
     */
160
    public function contains(\DateTime $point)
161
    {
162
        return $this->comparator->contains(new TimeIntervalPoint($point));
163
    }
164
165
    /**
166
     * @param TimeInterval $interval
167
     * @param bool $check_interval_type
168
     *
169
     * @return bool
170
     */
171
    public function intersects(TimeInterval $interval, $check_interval_type = true)
172
    {
173
        return $this->comparator->intersects($interval, $check_interval_type);
174
    }
175
176
    /**
177
     * @param TimeInterval $interval
178
     *
179
     * @return TimeInterval|null
180
     */
181
    public function intersection(TimeInterval $interval)
182
    {
183
        return $this->comparator->intersection($interval);
184
    }
185
186
    /**
187
     * The point is before the interval
188
     *
189
     * @param \DateTime $point
190
     *
191
     * @return bool
192
     */
193
    public function before(\DateTime $point)
194
    {
195
        return $this->comparator->before(new TimeIntervalPoint($point));
196
    }
197
198
    /**
199
     * The point is after the interval
200
     *
201
     * @param \DateTime $point
202
     *
203
     * @return bool
204
     */
205
    public function after(\DateTime $point)
206
    {
207
        return $this->comparator->after(new TimeIntervalPoint($point));
208
    }
209
210
    /**
211
     * @return IntervalType
212
     */
213
    public function type()
214
    {
215
        return $this->type;
216
    }
217
218
    /**
219
     * @return \DateTime
220
     */
221
    public function start()
222
    {
223
        return $this->start->value();
224
    }
225
226
    /**
227
     * @return \DateTime
228
     */
229
    public function end()
230
    {
231
        return $this->end->value();
232
    }
233
234
    /**
235
     * @return TimeIntervalPoint
236
     */
237
    public function startPoint()
238
    {
239
        return $this->start;
240
    }
241
242
    /**
243
     * @return TimeIntervalPoint
244
     */
245
    public function endPoint()
246
    {
247
        return $this->end;
248
    }
249
250
    /**
251
     * Returns a copy of this Interval with the start point altered.
252
     *
253
     * @param IntervalPointInterface|TimeIntervalPoint $start
254
     *
255
     * @return self
256
     */
257
    public function withStart(IntervalPointInterface $start)
258
    {
259
        return new self($start, $this->end, $this->type);
260
    }
261
262
    /**
263
     * Returns a copy of this Interval with the end point altered.
264
     *
265
     * @param IntervalPointInterface|TimeIntervalPoint $end
266
     *
267
     * @return self
268
     */
269
    public function withEnd(IntervalPointInterface $end)
270
    {
271
        return new self($this->start, $end, $this->type);
272
    }
273
274
    /**
275
     * Returns a copy of this Interval with the interval type altered.
276
     *
277
     * @param IntervalType $type
278
     *
279
     * @return self
280
     */
281
    public function withType(IntervalType $type)
282
    {
283
        return new self($this->start, $this->end, $type);
284
    }
285
286
    /**
287
     * @return string
288
     */
289
    public function __toString()
290
    {
291
        return $this->type->getReadable($this);
292
    }
293
}
294

src/Week/WeekInterval.php 1 location

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