1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JAAulde\IP\V4; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Represents a range of IPV4 addresses. |
7
|
|
|
* |
8
|
|
|
* @author Jim Auldridge <[email protected]> |
9
|
|
|
* @copyright 2006-2015 Jim Auldridge |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
class Range |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \JAAulde\IP\V4\Address The first address in the range being represented |
16
|
|
|
*/ |
17
|
|
|
protected $firstAddress; |
18
|
|
|
/** |
19
|
|
|
* @var \JAAulde\IP\V4\Address The last address in the range being represented |
20
|
|
|
*/ |
21
|
|
|
protected $lastAddress; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @param \JAAulde\IP\V4\Address $firstAddress The first address of the range being created |
27
|
|
|
* @param \JAAulde\IP\V4\Address $lastAddress The last address of the range being created |
28
|
|
|
* |
29
|
|
|
* @throws Exception |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Address $firstAddress, Address $lastAddress) |
32
|
|
|
{ |
33
|
|
|
if ($firstAddress->get() > $lastAddress->get()) { |
34
|
|
|
throw new \Exception(__METHOD__.' first param, $firstAddress, cannot be higher address than second param, $lastAddress'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->firstAddress = $firstAddress; |
38
|
|
|
$this->lastAddress = $lastAddress; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Determine if a given address is contained within the range. |
43
|
|
|
* |
44
|
|
|
* @param \JAAulde\IP\V4\Address $address The address we want to know about |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function contains(Address $address) |
49
|
|
|
{ |
50
|
|
|
$addressValue = $address->get(); |
51
|
|
|
|
52
|
|
|
return $addressValue >= $this->firstAddress->get() && $addressValue <= $this->lastAddress->get(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieve the first address in the range. |
57
|
|
|
* |
58
|
|
|
* @return \JAAulde\IP\V4\Address |
59
|
|
|
*/ |
60
|
|
|
public function getFirstAddress() |
61
|
|
|
{ |
62
|
|
|
return $this->firstAddress; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve the last address in the range. |
67
|
|
|
* |
68
|
|
|
* @return \JAAulde\IP\V4\Address |
69
|
|
|
*/ |
70
|
|
|
public function getLastAddress() |
71
|
|
|
{ |
72
|
|
|
return $this->lastAddress; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|