Completed
Push — master ( ee1b4e...77c5d1 )
by Tobias
02:56 queued 11s
created

IpAddress::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Talentify\ValueObject\Network;
6
7
use Talentify\ValueObject\ValueObject;
8
9
class IpAddress implements ValueObject
10
{
11
    /** @var string */
12
    private $ipAddress;
13
14
    /**
15
     * @throws \InvalidArgumentException if IP address is not valid
16
     */
17
    public function __construct(string $ipAddress)
18
    {
19
        if (filter_var($ipAddress, FILTER_VALIDATE_IP) === false) {
20
            throw new \InvalidArgumentException(sprintf('The value %s is not a valid IP address.', $ipAddress));
21
        }
22
23
        $this->ipAddress = $ipAddress;
24
    }
25
26
    public function getIpAddress() : string
27
    {
28
        return $this->ipAddress;
29
    }
30
31
    public function equals(?ValueObject $object) : bool
32
    {
33
        if (!$object instanceof self) {
34
            return false;
35
        }
36
37
        return $object->getIpAddress() === $this->getIpAddress();
38
    }
39
40
    public function __toString() : string
41
    {
42
        return $this->ipAddress;
43
    }
44
}
45