1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IPLib\Address; |
4
|
|
|
|
5
|
|
|
use IPLib\Range\RangeInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Represents an IP address range with an assigned range type. |
9
|
|
|
* |
10
|
|
|
* @since 1.5.0 |
11
|
|
|
*/ |
12
|
|
|
class AssignedRange |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The range definition. |
16
|
|
|
* |
17
|
|
|
* @var \IPLib\Range\RangeInterface |
18
|
|
|
*/ |
19
|
|
|
protected $range; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The range type. |
23
|
|
|
* |
24
|
|
|
* @var int one of the \IPLib\Range\Type::T_ constants |
25
|
|
|
*/ |
26
|
|
|
protected $type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The list of exceptions for this range type. |
30
|
|
|
* |
31
|
|
|
* @var \IPLib\Address\AssignedRange[] |
32
|
|
|
*/ |
33
|
|
|
protected $exceptions; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initialize the instance. |
37
|
|
|
* |
38
|
|
|
* @param \IPLib\Range\RangeInterface $range the range definition |
39
|
|
|
* @param int $type The range type (one of the \IPLib\Range\Type::T_ constants) |
40
|
|
|
* @param \IPLib\Address\AssignedRange[] $exceptions the list of exceptions for this range type |
41
|
|
|
*/ |
42
|
2 |
|
public function __construct(RangeInterface $range, $type, array $exceptions = array()) |
43
|
|
|
{ |
44
|
2 |
|
$this->range = $range; |
45
|
2 |
|
$this->type = $type; |
46
|
2 |
|
$this->exceptions = $exceptions; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the range definition. |
51
|
|
|
* |
52
|
|
|
* @return \IPLib\Range\RangeInterface |
53
|
|
|
*/ |
54
|
|
|
public function getRange() |
55
|
|
|
{ |
56
|
|
|
return $this->range; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the range type. |
61
|
|
|
* |
62
|
|
|
* @return int one of the \IPLib\Range\Type::T_ constants |
63
|
|
|
*/ |
64
|
177 |
|
public function getType() |
65
|
|
|
{ |
66
|
177 |
|
return $this->type; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the list of exceptions for this range type. |
71
|
|
|
* |
72
|
|
|
* @return \IPLib\Address\AssignedRange[] |
73
|
|
|
*/ |
74
|
|
|
public function getExceptions() |
75
|
|
|
{ |
76
|
|
|
return $this->exceptions; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the assigned type for a specific address. |
81
|
|
|
* |
82
|
|
|
* @param \IPLib\Address\AddressInterface $address |
83
|
|
|
* |
84
|
|
|
* @return int|null return NULL of the address is outside this address; a \IPLib\Range\Type::T_ constant otherwise |
85
|
|
|
*/ |
86
|
224 |
|
public function getAddressType(AddressInterface $address) |
87
|
|
|
{ |
88
|
224 |
|
$result = null; |
89
|
224 |
|
if ($this->range->contains($address)) { |
90
|
184 |
|
foreach ($this->exceptions as $exception) { |
91
|
26 |
|
$result = $exception->getAddressType($address); |
92
|
26 |
|
if ($result !== null) { |
93
|
14 |
|
break; |
94
|
|
|
} |
95
|
|
|
} |
96
|
184 |
|
if ($result === null) { |
97
|
184 |
|
$result = $this->type; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
224 |
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the assigned type for a specific address range. |
106
|
|
|
* |
107
|
|
|
* @param \IPLib\Range\RangeInterface $range |
108
|
|
|
* |
109
|
|
|
* @return int|false|null return NULL of the range is fully outside this range; false if it's partly crosses this range (or it contains mixed types); a \IPLib\Range\Type::T_ constant otherwise |
110
|
|
|
*/ |
111
|
233 |
|
public function getRangeType(RangeInterface $range) |
112
|
|
|
{ |
113
|
233 |
|
$myStart = $this->range->getComparableStartString(); |
114
|
233 |
|
$rangeEnd = $range->getComparableEndString(); |
115
|
233 |
|
if ($myStart > $rangeEnd) { |
116
|
44 |
|
$result = null; |
117
|
|
|
} else { |
118
|
233 |
|
$myEnd = $this->range->getComparableEndString(); |
119
|
233 |
|
$rangeStart = $range->getComparableStartString(); |
120
|
233 |
|
if ($myEnd < $rangeStart) { |
121
|
220 |
|
$result = null; |
122
|
198 |
|
} elseif ($rangeStart < $myStart || $rangeEnd > $myEnd) { |
123
|
21 |
|
$result = false; |
124
|
|
|
} else { |
125
|
190 |
|
$result = null; |
126
|
190 |
|
foreach ($this->exceptions as $exception) { |
127
|
32 |
|
$result = $exception->getRangeType($range); |
128
|
32 |
|
if ($result !== null) { |
129
|
19 |
|
break; |
130
|
|
|
} |
131
|
|
|
} |
132
|
190 |
|
if ($result === null) { |
133
|
177 |
|
$result = $this->getType(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
233 |
|
return $result; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|