Completed
Pull Request — master (#10)
by Yılmaz
02:30
created

IpRange::isEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * IP range value object
4
 * 
5
 * Thanks to Ross Tuck for this great article:
6
 * http://rosstuck.com/persisting-value-objects-in-doctrine/
7
 * 
8
 * @author  M. Yilmaz SUSLU <[email protected]>
9
 * @license MIT
10
 *
11
 * @since   Sep 2016
12
 */
13
namespace DDD\Embeddable;
14
15
use Doctrine\ORM\Mapping as ORM;
16
use JsonSerializable;
17
18
/**
19
 * @ORM\Embeddable
20
 */
21
class IpRange implements JsonSerializable
22
{
23
    /**
24
     * beginning IP address of the range
25
     * 
26
     * @ORM\Embedded(class="DDD\Embeddable\IpAddress")
27
     * 
28
     * @var IpAddress
29
     */
30
    protected $startIp;
31
32
    /**
33
     * high IP address of the range
34
     *
35
     * @ORM\Embedded(class="DDD\Embeddable\IpAddress")
36
     * 
37
     * @var IpAddress
38
     */
39
    protected $endIp;
40
41
    /**
42
     * Constructor.
43
     * 
44
     * @param IpAddress $startIp
45
     * @param IpAddress $endIp
46
     */
47 3
    public function __construct(IpAddress $startIp = null, IpAddress $endIp = null)
48
    {
49 3
        $this->startIp = $startIp;
50 3
        $this->endIp   = $endIp;
51 3
    }
52
53
    /**
54
     * Returns the low IP addresses of this range.
55
     * 
56
     * @return IpAddress
57
     */
58 2
    public function getStartIp()
59
    {
60 2
        return $this->startIp;
61
    }
62
63
    /**
64
     * Returns the high IP addresses of this range.
65
     * 
66
     * @return IpAddress
67
     */
68 2
    public function getEndIp()
69
    {
70 2
        return $this->endIp;
71
    }
72
73
    /**
74
     * Create a new range from CIDR notation.
75
     * CIDR notation is a compact representation of an IP address(es)
76
     * and its associated routing prefix.
77
     * 
78
     * @static
79
     *
80
     * @param string $cidr
81
     * 
82
     * @return self
83
     */
84 2
    public static function createFromCidrNotation($cidr)
85
    {
86 2
        list($subnet, $bits) = explode('/', $cidr);
87 2
        $start               = long2ip((ip2long($subnet)) & ((-1 << (32 - (int)$bits))));
88 2
        $end                 = long2ip((ip2long($subnet)) + pow(2, (32 - (int)$bits))-1);
89
90 2
        return new IpRange(new IpAddress($start), new IpAddress($end));
91
    }
92
93
    /**
94
     * String representation of a range.
95
     * 
96
     * Example output: "192.168.0.10 - 192.168.0.255"
97
     *    
98
     * @return string
99
     */
100 1
    public function __toString()
101
    {
102 1
        return $this->isEmpty() ? '' : (string) $this->startIp.' - '. $this->endIp;
103
    }
104
105
    /**
106
     * Array representation of the ip range
107
     * 
108
     * @return array
109
     */
110 3
    public function toArray()
111
    {
112 3
        if ($this->isEmpty()) {
113 1
            return [];
114
        }
115
116
        return [
117 2
            'startIp' => (string) $this->getStartIp(),
118 2
            'endIp'   => (string) $this->getEndIp(),
119 2
        ];
120
    }
121
122
    /**
123
     * Returns boolean TRUE if the range is empty, false otherwise.
124
     * 
125
     * @return boolean
126
     */
127 3
    public function isEmpty()
128
    {
129 3
        return $this->startIp === null || $this->endIp === null;
130
    }
131
132
    /**
133
     * Implement json serializable interface.
134
     * 
135
     * @return array
136
     */
137 2
    public function jsonSerialize()
138
    {
139 2
        return $this->toArray();
140
    }
141
}
142