Completed
Push — master ( 8b40f4...1a4202 )
by Peter
13:15
created

MonthInterval::equal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2016, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Interval\Month;
12
13
use GpsLab\Component\Interval\Exception\IncorrectIntervalException;
14
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException;
15
use GpsLab\Component\Interval\IntervalComparator;
16
use GpsLab\Component\Interval\ComparableIntervalInterface;
17
use GpsLab\Component\Interval\IntervalPointInterface;
18
use GpsLab\Component\Interval\IntervalType;
19
20 View Code Duplication
class MonthInterval implements ComparableIntervalInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

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

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
271
    }
272
273
    /**
274
     * Returns a copy of this Interval with the end point altered.
275
     *
276
     * @param IntervalPointInterface|MonthIntervalPoint $end
277
     *
278
     * @return self
279
     */
280
    public function withEnd(IntervalPointInterface $end)
281
    {
282
        return new self($this->start, $end, $this->type);
0 ignored issues
show
Compatibility introduced by
$end of type object<GpsLab\Component\...IntervalPointInterface> is not a sub-type of object<GpsLab\Component\...nth\MonthIntervalPoint>. It seems like you assume a concrete implementation of the interface GpsLab\Component\Interval\IntervalPointInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
283
    }
284
285
    /**
286
     * Returns a copy of this Interval with the interval type altered.
287
     *
288
     * @param IntervalType $type
289
     *
290
     * @return self
291
     */
292
    public function withType(IntervalType $type)
293
    {
294
        return new self($this->start, $this->end, $type);
295
    }
296
297
    /**
298
     * @return string
299
     */
300
    public function __toString()
301
    {
302
        return $this->type->getReadable($this);
303
    }
304
}
305