IPAddress   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A getValue() 0 3 1
A __toString() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Phypes\Type;
4
5
use Phypes\Exception\InvalidValue;
6
use Phypes\Result\Failure;
7
use Phypes\Validator\IPAddressValidator;
8
use Phypes\Validator\Validator;
9
10
class IPAddress implements Type
11
{
12
    /**
13
     * @var string $ip
14
     */
15
    private $ip;
16
17
    /**
18
     * Create an ip address object if data is valid.
19
     * @param string $ip
20
     * @param Validator $validator
21
     * @throws \InvalidArgumentException
22
     */
23
    public function __construct(string $ip, Validator $validator = null)
24
    {
25
        if ($validator == null) {
26
            // use the default validator
27
            $validator = new IPAddressValidator();
28
        }
29
30
        $result = $validator->validate($ip);
31
        if (!$result->isValid()) {
32
            /**
33
             * @var Failure $result
34
             */
35
            throw new InvalidValue($result->getFirstError()->getMessage(), $result->getErrors());
36
        }
37
        $this->ip = $ip;
38
    }
39
40
    /**
41
     * Get string representation of the ip address object.
42
     * @return string
43
     */
44
    public function __toString(): string
45
    {
46
        return $this->ip;
47
    }
48
49
    /**
50
     * Get real value of the ip address object.
51
     * @return string
52
     */
53
    public function getValue() : string
54
    {
55
        return $this->ip;
56
    }
57
}