1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IPLib\Range; |
4
|
|
|
|
5
|
|
|
use IPLib\Address\AddressInterface; |
6
|
|
|
use IPLib\Address\IPv4; |
7
|
|
|
use IPLib\Address\Type as AddressType; |
8
|
|
|
use IPLib\Factory; |
9
|
|
|
use IPLib\ParseStringFlag; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Represents a single address (eg a range that contains just one address). |
13
|
|
|
* |
14
|
|
|
* @example 127.0.0.1 |
15
|
|
|
* @example ::1 |
16
|
|
|
*/ |
17
|
|
|
class Single extends AbstractRange |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \IPLib\Address\AddressInterface |
21
|
|
|
*/ |
22
|
|
|
protected $address; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Initializes the instance. |
26
|
|
|
* |
27
|
|
|
* @param \IPLib\Address\AddressInterface $address |
28
|
|
|
*/ |
29
|
116 |
|
protected function __construct(AddressInterface $address) |
30
|
|
|
{ |
31
|
116 |
|
$this->address = $address; |
32
|
116 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* |
37
|
|
|
* @see \IPLib\Range\RangeInterface::__toString() |
38
|
|
|
*/ |
39
|
13 |
|
public function __toString() |
40
|
|
|
{ |
41
|
13 |
|
return $this->address->__toString(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @deprecated since 1.17.0: use the parseString() method instead. |
46
|
|
|
* For upgrading: |
47
|
|
|
* - if $supportNonDecimalIPv4 is true, use the ParseStringFlag::IPV4_MAYBE_NON_DECIMAL flag |
48
|
|
|
* |
49
|
|
|
* @param string|mixed $range |
50
|
|
|
* @param bool $supportNonDecimalIPv4 |
51
|
|
|
* |
52
|
|
|
* @return static|null |
53
|
|
|
* |
54
|
|
|
* @see \IPLib\Range\Single::parseString() |
55
|
|
|
* @since 1.10.0 added the $supportNonDecimalIPv4 argument |
56
|
|
|
*/ |
57
|
9 |
|
public static function fromString($range, $supportNonDecimalIPv4 = false) |
58
|
|
|
{ |
59
|
9 |
|
return static::parseString($range, ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::MAY_INCLUDE_ZONEID | ($supportNonDecimalIPv4 ? ParseStringFlag::IPV4_MAYBE_NON_DECIMAL : 0)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Try get the range instance starting from its string representation. |
64
|
|
|
* |
65
|
|
|
* @param string|mixed $range |
66
|
|
|
* @param int $flags A combination or zero or more flags |
67
|
|
|
* |
68
|
|
|
* @return static|null |
69
|
|
|
* |
70
|
|
|
* @see \IPLib\ParseStringFlag |
71
|
|
|
* @since 1.17.0 |
72
|
|
|
*/ |
73
|
68 |
|
public static function parseString($range, $flags = 0) |
74
|
|
|
{ |
75
|
68 |
|
$result = null; |
76
|
68 |
|
$flags = (int) $flags; |
77
|
68 |
|
$address = Factory::parseAddressString($range, $flags); |
78
|
68 |
|
if ($address !== null) { |
79
|
47 |
|
$result = new static($address); |
80
|
|
|
} |
81
|
|
|
|
82
|
68 |
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create the range instance starting from an address instance. |
87
|
|
|
* |
88
|
|
|
* @param \IPLib\Address\AddressInterface $address |
89
|
|
|
* |
90
|
|
|
* @return static |
91
|
|
|
* |
92
|
|
|
* @since 1.2.0 |
93
|
|
|
*/ |
94
|
69 |
|
public static function fromAddress(AddressInterface $address) |
95
|
|
|
{ |
96
|
69 |
|
return new static($address); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
* |
102
|
|
|
* @see \IPLib\Range\RangeInterface::toString() |
103
|
|
|
*/ |
104
|
14 |
|
public function toString($long = false) |
105
|
|
|
{ |
106
|
14 |
|
return $this->address->toString($long); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
* |
112
|
|
|
* @see \IPLib\Range\RangeInterface::getAddressType() |
113
|
|
|
*/ |
114
|
87 |
|
public function getAddressType() |
115
|
|
|
{ |
116
|
87 |
|
return $this->address->getAddressType(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
* |
122
|
|
|
* @see \IPLib\Range\RangeInterface::getRangeType() |
123
|
|
|
*/ |
124
|
9 |
|
public function getRangeType() |
125
|
|
|
{ |
126
|
9 |
|
return $this->address->getRangeType(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
* |
132
|
|
|
* @see \IPLib\Range\RangeInterface::contains() |
133
|
|
|
*/ |
134
|
7 |
|
public function contains(AddressInterface $address) |
135
|
|
|
{ |
136
|
7 |
|
$result = false; |
137
|
7 |
|
if ($address->getAddressType() === $this->getAddressType()) { |
138
|
5 |
|
if ($address->toString(false) === $this->address->toString(false)) { |
139
|
2 |
|
$result = true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
7 |
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
* |
149
|
|
|
* @see \IPLib\Range\RangeInterface::getStartAddress() |
150
|
|
|
*/ |
151
|
62 |
|
public function getStartAddress() |
152
|
|
|
{ |
153
|
62 |
|
return $this->address; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
* |
159
|
|
|
* @see \IPLib\Range\RangeInterface::getEndAddress() |
160
|
|
|
*/ |
161
|
1 |
|
public function getEndAddress() |
162
|
|
|
{ |
163
|
1 |
|
return $this->address; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
* |
169
|
|
|
* @see \IPLib\Range\RangeInterface::getComparableStartString() |
170
|
|
|
*/ |
171
|
18 |
|
public function getComparableStartString() |
172
|
|
|
{ |
173
|
18 |
|
return $this->address->getComparableString(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
* |
179
|
|
|
* @see \IPLib\Range\RangeInterface::getComparableEndString() |
180
|
|
|
*/ |
181
|
17 |
|
public function getComparableEndString() |
182
|
|
|
{ |
183
|
17 |
|
return $this->address->getComparableString(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
* |
189
|
|
|
* @see \IPLib\Range\RangeInterface::asSubnet() |
190
|
|
|
*/ |
191
|
57 |
|
public function asSubnet() |
192
|
|
|
{ |
193
|
57 |
|
return new Subnet($this->address, $this->address, $this->getNetworkPrefix()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
* |
199
|
|
|
* @see \IPLib\Range\RangeInterface::asPattern() |
200
|
|
|
*/ |
201
|
56 |
|
public function asPattern() |
202
|
|
|
{ |
203
|
56 |
|
return new Pattern($this->address, $this->address, 0); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
|
|
* |
209
|
|
|
* @see \IPLib\Range\RangeInterface::getSubnetMask() |
210
|
|
|
*/ |
211
|
2 |
|
public function getSubnetMask() |
212
|
|
|
{ |
213
|
2 |
|
if ($this->getAddressType() !== AddressType::T_IPv4) { |
214
|
1 |
|
return null; |
215
|
|
|
} |
216
|
|
|
|
217
|
1 |
|
return IPv4::fromBytes(array(255, 255, 255, 255)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
* |
223
|
|
|
* @see \IPLib\Range\RangeInterface::getReverseDNSLookupName() |
224
|
|
|
*/ |
225
|
3 |
|
public function getReverseDNSLookupName() |
226
|
|
|
{ |
227
|
3 |
|
return array($this->getStartAddress()->getReverseDNSLookupName()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
* |
233
|
|
|
* @see \IPLib\Range\RangeInterface::getSize() |
234
|
|
|
*/ |
235
|
2 |
|
public function getSize() |
236
|
|
|
{ |
237
|
2 |
|
return 1; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
* |
243
|
|
|
* @see \IPLib\Range\RangeInterface::getNetworkPrefix() |
244
|
|
|
*/ |
245
|
67 |
|
public function getNetworkPrefix() |
246
|
|
|
{ |
247
|
67 |
|
switch ($this->getAddressType()) { |
248
|
|
|
case AddressType::T_IPv4: |
249
|
24 |
|
return 32; |
250
|
|
|
case AddressType::T_IPv6: |
251
|
43 |
|
return 128; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|