IpConverter::fromHexToIp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\IpFilterBundle\Tool;
13
14
class IpConverter
15
{
16
    public static function fromIpToHex($ip)
17
    {
18
        $hex = bin2hex(inet_pton($ip));
19
        if (8 === strlen($hex)) {
20
            $hex = str_pad($hex, 32, '0', STR_PAD_LEFT);
21
        }
22
23
        return $hex;
24
    }
25
26
    public static function fromHexToIp($ip)
27
    {
28
        $hex = pack('H*', $ip);
29
        if (str_repeat('0', 24) === substr($hex, 0, 24)) {
30
            $hex = substr($hex, 24);
31
        }
32
33
        return inet_ntop($hex);
34
    }
35
}
36