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

CIDR   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 22
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 9 1
A matchBulk() 0 9 5
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