Skip to content

Commit 0f4d24a

Browse files
authored
Merge pull request #3 from MaartenGDev/master
Added matchRange function
2 parents 02fc102 + 4a7e72f commit 0f4d24a

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/Ip.php

+17
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,21 @@ public static function long2ip($dec, $ipv6 = false)
350350
}
351351
return $ipstr;
352352
}
353+
354+
public static function matchRange($ip, $range)
355+
{
356+
$ipItems = explode('.', $ip);
357+
$rangeItems = explode('.', $range);
358+
359+
$ipItems = array_filter($ipItems, function ($subnet) {
360+
return $subnet !== '';
361+
});
362+
$rangeItems = array_filter($rangeItems, function ($subnet) {
363+
return $subnet !== '';
364+
});
365+
366+
$ipItems = array_slice($ipItems, 0, count($rangeItems));
367+
368+
return implode('.', $rangeItems) === implode('.', $ipItems);
369+
}
353370
}

tests/Unit/IpTest.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function test8()
153153
$this->assertTrue($status);
154154

155155
$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:9652',
156-
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:9999');
156+
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:9999');
157157
$this->assertTrue($status);
158158

159159

@@ -166,7 +166,7 @@ public function test8()
166166

167167

168168
$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:7778',
169-
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:7777');
169+
'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:7777');
170170
$this->assertFalse($status);
171171
}
172172

@@ -193,6 +193,20 @@ public function test9()
193193
/**
194194
* @test
195195
*/
196+
197+
198+
public function test_match_range()
199+
{
200+
$range = Ip::matchRange('192.168.100.', '192.168..');
201+
$this->assertTrue($range);
202+
203+
$range = Ip::matchRange('192.168.1.200', '192.168.1.');
204+
$this->assertTrue($range);
205+
206+
$range = Ip::matchRange('192.168.1.200', '192.168.2.');
207+
$this->assertFalse($range);
208+
}
209+
196210
public function testLocal()
197211
{
198212
$status = Ip::isLocal('192.168.5.5');

0 commit comments

Comments
 (0)