Wildcard::convert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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