Completed
Push — master ( 7486de...6f82e1 )
by Andrii
11:20
created

CIDR::matchBulk()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6111
c 0
b 0
f 0
cc 5
nc 9
nop 2
1
<?php
2
3
namespace hiapi\Core\Utils;
4
5
class CIDR
6
{
7
    public static function match ($ip, $range)
8
    {
9
        list ($subnet, $bits) = explode('/', $range);
10
        $ip = ip2long($ip);
11
        $subnet = ip2long($subnet);
12
        $mask = -1 << (32 - $bits);
13
        $subnet &= $mask;
14
        return ($ip & $mask) == $subnet;
15
    }
16
17
    public static function matchBulk ($ip, $ranges)
18
    {
19
        $match = false;
20
        foreach ($ranges as $range => $value) {
21
            $match = $match ? : (self::match($ip, $range) ? $value : false);
22
            if ($match) break;
23
        }
24
        return $match;
25
    }
26
}
27