IpValue::fromString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace talismanfr\rosselhozbank\shared;
5
6
7
class IpValue
8
{
9
    /**
10
     * Represents the version 4 of the protocol.
11
     */
12
    public const IPV4 = 4;
13
14
    /**
15
     * Represents the version 6 of the protocol.
16
     */
17
    public const IPV6 = 6;
18
19
    /**
20
     * @var string
21
     */
22
    private $address;
23
24
    /**
25
     * @var integer
26
     */
27
    private $version;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param string $address The IP address.
33
     */
34
    public function __construct($address)
35
    {
36
        if (false === filter_var($address, FILTER_VALIDATE_IP)) {
37
            throw new \InvalidArgumentException('Invalid IP address.');
38
        }
39
40
        $version = self::IPV6;
41
        if (false === filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
42
            $version = self::IPV4;
43
        }
44
45
        $this->address = (string) $address;
46
        $this->version = $version;
47
48
    }
49
50
    /**
51
     * Parses an IP address into an object.
52
     *
53
     * @param string $address The IP address.
54
     *
55
     * @return IpValue
56
     */
57
    public static function fromString($address)
58
    {
59
        return new self($address);
60
    }
61
62
    /**
63
     * Returns the IP version.
64
     *
65
     * @return integer
66
     *
67
     * @see Ip::IPV4
68
     * @see Ip::IPV6
69
     */
70
    public function getVersion()
71
    {
72
        return $this->version;
73
    }
74
75
    /**
76
     * Returns the email address as string.
77
     *
78
     * @return string
79
     */
80
    public function __toString()
81
    {
82
        return $this->address;
83
    }
84
}