Completed
Push — master ( caad92...bf4c7a )
by Peter
05:39
created

IPv4Interval::create()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
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\IPv4;
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 IPv4Interval implements IntervalInterface
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...
18
{
19
    /**
20
     * @var string
21
     */
22
    const REGEXP = '/^
23
        (?:\(|\[)                                    # start type char
24
        \s*
25
        (?<start>\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}) # start point
26
        \s*,\s*                                      # separator
27
        (?<end>\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3})   # end point
28
        \s*
29
        (?:\)|\])                                    # end type char
30
    $/x';
31
32
    /**
33
     * @var IntervalType
34
     */
35
    private $type;
36
37
    /**
38
     * @var IPv4IntervalComparator
39
     */
40
    private $comparator;
41
42
    /**
43
     * @var IPv4IntervalPoint
44
     */
45
    private $start;
46
47
    /**
48
     * @var IPv4IntervalPoint
49
     */
50
    private $end;
51
52
    /**
53
     * @param IPv4IntervalPoint $start
54
     * @param IPv4IntervalPoint $end
55
     * @param IntervalType $type
56
     */
57
    private function __construct(IPv4IntervalPoint $start, IPv4IntervalPoint $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 IPv4IntervalComparator($this);
67
    }
68
69
    /**
70
     * @param string $start
71
     * @param string $end
72
     * @param IntervalType $type
73
     *
74
     * @return self
75
     */
76
    public static function create($start, $end, IntervalType $type)
77
    {
78
        return new self(new IPv4IntervalPoint($start), new IPv4IntervalPoint($end), $type);
79
    }
80
81
    /**
82
     * @param string $start
83
     * @param string $end
84
     *
85
     * @return self
86
     */
87
    public static function closed($start, $end)
88
    {
89
        return static::create($start, $end, IntervalType::closed());
90
    }
91
92
    /**
93
     * @param string $start
94
     * @param string $end
95
     *
96
     * @return self
97
     */
98
    public static function halfClosed($start, $end)
99
    {
100
        return static::create($start, $end, IntervalType::halfClosed());
101
    }
102
103
    /**
104
     * @param string $start
105
     * @param string $end
106
     *
107
     * @return self
108
     */
109
    public static function halfOpen($start, $end)
110
    {
111
        return static::create($start, $end, IntervalType::halfOpen());
112
    }
113
114
    /**
115
     * @param string $start
116
     * @param string $end
117
     *
118
     * @return self
119
     */
120
    public static function open($start, $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
     *   [10.0.1.0, 10.0.1.255]
130
     *   (10.0.0.0, 10.255.255.255]
131
     *   [172.16.0.0, 172.31.255.255)
132
     *   (192.168.0.0, 192.168.255.255)
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('[0.0.0.0, 255.255.255.255]', $string);
144
        }
145
146
        return self::create($match['start'], $match['end'], IntervalType::fromString($string));
147
    }
148
149
    /**
150
     * @param string $point
151
     *
152
     * @return bool
153
     */
154
    public function contains($point)
155
    {
156
        return $this->comparator->contains(new IPv4IntervalPoint($point));
157
    }
158
159
    /**
160
     * @param IPv4Interval $interval
161
     * @param bool $check_interval_type
162
     *
163
     * @return bool
164
     */
165
    public function intersect(IPv4Interval $interval, $check_interval_type = true)
166
    {
167
        return $this->comparator->intersect($interval, $check_interval_type);
168
    }
169
170
    /**
171
     * @param IPv4Interval $interval
172
     *
173
     * @return IPv4Interval|null
174
     */
175
    public function intersectInterval(IPv4Interval $interval)
176
    {
177
        return $this->comparator->intersectInterval($interval);
178
    }
179
180
    /**
181
     * The point is before the interval
182
     *
183
     * @param string $point
184
     *
185
     * @return bool
186
     */
187
    public function before($point)
188
    {
189
        return $this->comparator->before(new IPv4IntervalPoint($point));
190
    }
191
192
    /**
193
     * The point is after the interval
194
     *
195
     * @param string $point
196
     *
197
     * @return bool
198
     */
199
    public function after($point)
200
    {
201
        return $this->comparator->after(new IPv4IntervalPoint($point));
202
    }
203
204
    /**
205
     * @return IntervalType
206
     */
207
    public function type()
208
    {
209
        return $this->type;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function start()
216
    {
217
        return (string)$this->start;
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    public function end()
224
    {
225
        return (string)$this->end;
226
    }
227
228
    /**
229
     * @return IPv4IntervalPoint
230
     */
231
    public function startPoint()
232
    {
233
        return $this->start;
234
    }
235
236
    /**
237
     * @return IPv4IntervalPoint
238
     */
239
    public function endPoint()
240
    {
241
        return $this->end;
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function __toString()
248
    {
249
        return $this->type->getReadable($this);
250
    }
251
}
252