Passed
Branch master (00a87c)
by Kashyap
01:08
created

Helper::getOctalParts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * Part of the IPFuscator package.
5
 * Author: Kashyap Merai <[email protected]>
6
 *
7
 */
8
9
10
namespace kamerk22\IPFuscator\Helper;
11
12
13
use kamerk22\IPFuscator\Exception\InvalidArgument;
14
15
class Helper
16
{
17
18
    /**
19
     * getParts
20
     *
21
     * @param string $ip
22
     *
23
     * @return array
24
     * @throws InvalidArgument
25
     */
26
    public static function getParts(string $ip): array
27
    {
28
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
29
            return explode('.', $ip);
30
        }
31
        throw InvalidArgument::invalidIp('IP Adress format is invalid.');
32
33
    }
34
35
    /**
36
     * getHexParts
37
     *
38
     * @param string $ip
39
     *
40
     * @return array
41
     * @throws InvalidArgument
42
     */
43
    public static function getHexParts(string $ip): array
44
    {
45
        $hexParts = [];
46
        foreach (self::getParts($ip) as $part) {
47
            $hexParts[] = '0x' . dechex((int)$part);
48
        }
49
        return $hexParts;
50
    }
51
52
    /**
53
     * getOctalParts
54
     *
55
     * @param string $ip
56
     *
57
     * @return array
58
     * @throws InvalidArgument
59
     */
60
    public static function getOctalParts(string $ip): array
61
    {
62
        $octParts = [];
63
        foreach (self::getParts($ip) as $part) {
64
            $octParts[] = '0' . decoct((int)$part);
65
        }
66
        return $octParts;
67
    }
68
}