|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IPLib\Range; |
|
4
|
|
|
|
|
5
|
|
|
use IPLib\Address\AddressInterface; |
|
6
|
|
|
use IPLib\Address\IPv4; |
|
7
|
|
|
use IPLib\Address\IPv6; |
|
8
|
|
|
use IPLib\Address\Type as AddressType; |
|
9
|
|
|
use IPLib\Factory; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractRange implements RangeInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritdoc} |
|
15
|
|
|
* |
|
16
|
|
|
* @see \IPLib\Range\RangeInterface::getRangeType() |
|
17
|
|
|
*/ |
|
18
|
242 |
|
public function getRangeType() |
|
19
|
|
|
{ |
|
20
|
242 |
|
if ($this->rangeType === null) { |
|
21
|
242 |
|
$addressType = $this->getAddressType(); |
|
22
|
242 |
|
if ($addressType === AddressType::T_IPv6 && Subnet::get6to4()->containsRange($this)) { |
|
23
|
9 |
|
$this->rangeType = Factory::rangeFromBoundaries($this->fromAddress->toIPv4(), $this->toAddress->toIPv4())->getRangeType(); |
|
|
|
|
|
|
24
|
|
|
} else { |
|
25
|
|
|
switch ($addressType) { |
|
26
|
233 |
|
case AddressType::T_IPv4: |
|
27
|
170 |
|
$defaultType = IPv4::getDefaultReservedRangeType(); |
|
28
|
170 |
|
$reservedRanges = IPv4::getReservedRanges(); |
|
29
|
170 |
|
break; |
|
30
|
63 |
|
case AddressType::T_IPv6: |
|
31
|
63 |
|
$defaultType = IPv6::getDefaultReservedRangeType(); |
|
32
|
63 |
|
$reservedRanges = IPv6::getReservedRanges(); |
|
33
|
63 |
|
break; |
|
34
|
|
|
default: |
|
35
|
|
|
throw new \Exception('@todo'); // @codeCoverageIgnore |
|
36
|
|
|
} |
|
37
|
233 |
|
$rangeType = null; |
|
38
|
233 |
|
foreach ($reservedRanges as $reservedRange) { |
|
39
|
233 |
|
$rangeType = $reservedRange->getRangeType($this); |
|
40
|
233 |
|
if ($rangeType !== null) { |
|
41
|
233 |
|
break; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
233 |
|
$this->rangeType = $rangeType === null ? $defaultType : $rangeType; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
242 |
|
return $this->rangeType === false ? null : $this->rangeType; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
* |
|
54
|
|
|
* @see \IPLib\Range\RangeInterface::contains() |
|
55
|
|
|
*/ |
|
56
|
241 |
View Code Duplication |
public function contains(AddressInterface $address) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
241 |
|
$result = false; |
|
59
|
241 |
|
if ($address->getAddressType() === $this->getAddressType()) { |
|
60
|
241 |
|
$cmp = $address->getComparableString(); |
|
61
|
241 |
|
$from = $this->getComparableStartString(); |
|
62
|
241 |
|
if ($cmp >= $from) { |
|
63
|
237 |
|
$to = $this->getComparableEndString(); |
|
64
|
237 |
|
if ($cmp <= $to) { |
|
65
|
195 |
|
$result = true; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
241 |
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
* |
|
76
|
|
|
* @see \IPLib\Range\RangeInterface::containsRange() |
|
77
|
|
|
*/ |
|
78
|
79 |
View Code Duplication |
public function containsRange(RangeInterface $range) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
79 |
|
$result = false; |
|
81
|
79 |
|
if ($range->getAddressType() === $this->getAddressType()) { |
|
82
|
79 |
|
$myStart = $this->getComparableStartString(); |
|
83
|
79 |
|
$itsStart = $range->getComparableStartString(); |
|
84
|
79 |
|
if ($itsStart >= $myStart) { |
|
85
|
42 |
|
$myEnd = $this->getComparableEndString(); |
|
86
|
42 |
|
$itsEnd = $range->getComparableEndString(); |
|
87
|
42 |
|
if ($itsEnd <= $myEnd) { |
|
88
|
14 |
|
$result = true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
79 |
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: