Range::isValid()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * IP range converter for a range
4
 *
5
 * E.g. 10.0.0.1-10.0.1.100
6
 *
7
 * PHP version 5.5
8
 *
9
 * @category   OpCacheGUI
10
 * @package    Network
11
 * @subpackage Ip
12
 * @author     Pieter Hordijk <[email protected]>
13
 * @copyright  Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa>
14
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
15
 * @version    1.0.0
16
 */
17
namespace OpCacheGUI\Network\Ip;
18
19
/**
20
 * IP range converter for a range
21
 *
22
 * @category   OpCacheGUI
23
 * @package    Network
24
 * @subpackage Ip
25
 * @author     Pieter Hordijk <[email protected]>
26
 */
27
class Range implements Converter
28
{
29
    /**
30
     * Checks whether is certain address is valid for the converter implementation
31
     *
32
     * @param string $address The address to check
33
     *
34
     * @return boolean True when the address is valid
35
     */
36 6
    public function isValid($address)
37
    {
38 6
        return !!preg_match('/^\d+\.\d+\.\d+\.\d+\s?-\s?\d+\.\d+\.\d+\.\d+$/', $address);
39
    }
40
41
    /**
42
     * Converts an IP address or range into a range to easily check for access
43
     *
44
     * @param string $address The IP address / range
45
     *
46
     * @return double[] Array containing the first and last ip in the range
47
     */
48 2
    public function convert($address)
49
    {
50 2
        $parts = explode('-', $address);
51
52
        return [
53 2
            (float) sprintf('%u', ip2long(trim($parts[0]))),
54 2
            (float) sprintf('%u', ip2long(trim($parts[1]))),
55
        ];
56
    }
57
}
58