IPAddress::validateIPv4Address()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Ping for Laravel.
5
 *
6
 * This class makes Ping request to a host.
7
 *
8
 * Ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.
9
 *
10
 * @author  Angel Campos <[email protected]>
11
 * @requires PHP 8.0
12
 *
13
 * @version  2.1.2
14
 */
15
16
namespace Acamposm\Ping;
17
18
class IPAddress
19
{
20
    public const IPV4_SEPARATOR = '.';
21
    public const IPV6_SEPARATOR = ':';
22
23
    /**
24
     * Performs the validation of the IP Address.
25
     *
26
     * @param string $ip_address
27
     *
28
     * @return bool
29
     */
30
    public static function Validate(string $ip_address): bool
31
    {
32
        if (str_contains($ip_address, IPAddress::IPV4_SEPARATOR)) {
33
            return self::validateIPv4Address($ip_address);
34
        }
35
36
        if (str_contains($ip_address, IPAddress::IPV6_SEPARATOR)) {
37
            return self::validateIPv6Address($ip_address);
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * Performs a IPv4 validation.
45
     *
46
     * Validate an IPv4 address
47
     *
48
     * @param string $ip_address
49
     *
50
     * @return bool
51
     */
52
    private static function validateIPv4Address(string $ip_address): bool
53
    {
54
        if (filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
55
            return false;
56
        }
57
58
        return true;
59
    }
60
61
    /**
62
     * Performs a IPv6 validation.
63
     *
64
     * @param string $ip_address
65
     *
66
     * @return bool
67
     */
68
    private static function validateIPv6Address(string $ip_address): bool
69
    {
70
        if (filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
71
            return false;
72
        }
73
74
        return true;
75
    }
76
}
77