1 | <?php |
||
16 | class IPv4Network implements IntervalInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | const REGEXP = '/^ |
||
22 | \s* |
||
23 | (?<ip>\d{1,3}(?:\.\d{1,3}(?:\.\d{1,3}(?:\.\d{1,3})?)?)?) # IP supported compact format |
||
24 | \/ # separator |
||
25 | (?<cidr>\d{1,2}) # 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 | 27 | private function __construct(IPv4NetworkPoint $ip, IPv4NetworkMask $mask) |
|
62 | |||
63 | /** |
||
64 | * @param string $ip |
||
65 | * @param int $cidr |
||
66 | * |
||
67 | * @return IPv4Network |
||
68 | */ |
||
69 | 24 | public static function fromCIDR($ip, $cidr) |
|
73 | |||
74 | /** |
||
75 | * @param string $ip |
||
76 | * @param string $mask |
||
77 | * |
||
78 | * @return IPv4Network |
||
79 | */ |
||
80 | 3 | public static function fromMask($ip, $mask) |
|
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 | 21 | public static function fromString($string) |
|
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) |
||
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) |
|
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) |
|
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) |
||
163 | |||
164 | /** |
||
165 | * @param int $step |
||
166 | * |
||
167 | * @return \Generator |
||
168 | */ |
||
169 | public function iterate($step = 1) |
||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | 16 | public function start() |
|
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 16 | public function end() |
|
195 | |||
196 | /** |
||
197 | * @return IPv4NetworkMask |
||
198 | */ |
||
199 | 22 | public function mask() |
|
203 | |||
204 | /** |
||
205 | * @return int |
||
206 | */ |
||
207 | 16 | public function cidr() |
|
211 | |||
212 | /** |
||
213 | * @return IPv4NetworkPoint |
||
214 | */ |
||
215 | 12 | public function startPoint() |
|
219 | |||
220 | /** |
||
221 | * @return IPv4NetworkPoint |
||
222 | */ |
||
223 | 6 | public function endPoint() |
|
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | 1 | public function __toString() |
|
235 | } |
||
236 |