Completed
Push — master ( 5096ba...da0857 )
by Peter
07:17
created

IPv4Network::cidr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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\IPv4Network;
12
13
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException;
14
use GpsLab\Component\Interval\IntervalInterface;
15
16
class IPv4Network implements IntervalInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    const REGEXP = '/^
22
        \s*
23
        (?<ip>'.IPv4NetworkPoint::IPV4_ADDR_COMPACT.') # IP supported compact format
24
        \/                                             # separator
25
        (?<cidr>'.IPv4NetworkMask::CIDR.')             # CIDR bit
26
        \s*
27
    $/x';
28
29
    /**
30
     * @var IPv4NetworkPoint
31
     */
32
    private $start;
33
34
    /**
35
     * @var IPv4NetworkPoint
36
     */
37
    private $end;
38
39
    /**
40
     * @var IPv4NetworkMask
41
     */
42
    private $mask;
43
44
    /**
45
     * @var IPv4NetworkComparator
46
     */
47
    private $comparator;
48
49
    /**
50
     * @param IPv4NetworkPoint $ip
51
     * @param IPv4NetworkMask $mask
52
     */
53 29
    private function __construct(IPv4NetworkPoint $ip, IPv4NetworkMask $mask)
54
    {
55 29
        $this->start = $ip;
56 29
        $this->mask = $mask;
57 29
        $this->end = new IPv4NetworkPoint(long2ip(
58 29
            $ip->value() + 2 ** (32 - $mask->cidr()) - 1
59 29
        ));
60 29
        $this->comparator = new IPv4NetworkComparator($this);
61 29
    }
62
63
    /**
64
     * @param string $ip
65
     * @param int $cidr
66
     *
67
     * @return IPv4Network
68
     */
69 26
    public static function fromCIDR($ip, $cidr)
70
    {
71 26
        return new self(new IPv4NetworkPoint($ip), IPv4NetworkMask::fromCIDR($cidr));
72
    }
73
74
    /**
75
     * @param string $ip
76
     * @param string $mask
77
     *
78
     * @return IPv4Network
79
     */
80 3
    public static function fromMask($ip, $mask)
81
    {
82 3
        return new self(new IPv4NetworkPoint($ip), IPv4NetworkMask::fromIP($mask));
83
    }
84
85
    /**
86
     * Create network from string.
87
     *
88
     * Example formats of network:
89
     *   10.0.0.0/8
90
     *   172.16.0.0/12
91
     *   192.168.0.0/16
92
     *
93
     * Supported compact format:
94
     *   10/8
95
     *   172.16/12
96
     *   192.168/16
97
     *
98
     * Spaces are ignored in format.
99
     *
100
     * @param string $string
101
     *
102
     * @return self
103
     */
104 23 View Code Duplication
    public static function fromString($string)
0 ignored issues
show
Duplication introduced by
This method 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...
105
    {
106 23
        if (!preg_match(self::REGEXP, $string, $match)) {
107
            throw InvalidIntervalFormatException::create('0.0.0.0/32', $string);
108
        }
109
110
        // fill IP compact format
111 23
        $match['ip'] .= str_repeat('.0', 3 - substr_count($match['ip'], '.'));
112
113 23
        return self::fromCIDR($match['ip'], $match['cidr']);
114
    }
115
116
    /**
117
     * Checks if this network is equal to the specified network.
118
     *
119
     * @param IPv4Network $network
120
     *
121
     * @return bool
122
     */
123
    public function equal(IPv4Network $network)
124
    {
125
        return $this->comparator->equal($network);
126
    }
127
128
    /**
129
     * Does this network contain the specified IP.
130
     *
131
     * @param string $point
132
     *
133
     * @return bool
134
     */
135 6
    public function contains($point)
136
    {
137 6
        return $this->comparator->contains(new IPv4NetworkPoint($point));
138
    }
139
140
    /**
141
     * Does this network intersect the specified network.
142
     *
143
     * @param IPv4Network $network
144
     *
145
     * @return bool
146
     */
147 3
    public function intersects(IPv4Network $network)
148
    {
149 3
        return $this->comparator->intersects($network);
150
    }
151
152
    /**
153
     * Does this network abut with the network specified.
154
     *
155
     * @param IPv4Network $network
156
     *
157
     * @return bool
158
     */
159
    public function abuts(IPv4Network $network)
160
    {
161
        return $this->comparator->abuts($network);
162
    }
163
164
    /**
165
     * @param int $step
166
     *
167
     * @return \Generator
168
     */
169 2 View Code Duplication
    public function iterate($step = 1)
0 ignored issues
show
Duplication introduced by
This method 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...
170
    {
171 2
        $end = $this->endPoint()->value();
172 2
        $ip = $this->startPoint()->value();
173
174 2
        while ($ip <= $end) {
175 2
            yield long2ip($ip);
176 2
            $ip += $step;
177 2
        }
178 2
    }
179
180
    /**
181
     * @return string
182
     */
183 16
    public function start()
184
    {
185 16
        return (string) $this->start;
186
    }
187
188
    /**
189
     * @return string
190
     */
191 16
    public function end()
192
    {
193 16
        return (string) $this->end;
194
    }
195
196
    /**
197
     * @return IPv4NetworkMask
198
     */
199 22
    public function mask()
200
    {
201 22
        return $this->mask;
202
    }
203
204
    /**
205
     * @return int
206
     */
207 16
    public function cidr()
208
    {
209 16
        return $this->mask->cidr();
210
    }
211
212
    /**
213
     * @return IPv4NetworkPoint
214
     */
215 14
    public function startPoint()
216
    {
217 14
        return $this->start;
218
    }
219
220
    /**
221
     * @return IPv4NetworkPoint
222
     */
223 8
    public function endPoint()
224
    {
225 8
        return $this->end;
226
    }
227
228
    /**
229
     * @return string
230
     */
231 1
    public function __toString()
232
    {
233 1
        return $this->start.'/'.$this->mask->cidr();
234
    }
235
}
236