1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GpsLab component. |
5
|
|
|
* |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Interval\IPv4Network; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The comparator must be used only in IPv4Network. |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class IPv4NetworkComparator |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var IPv4Network |
20
|
|
|
*/ |
21
|
|
|
private $network; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param IPv4Network $network |
25
|
|
|
*/ |
26
|
29 |
|
public function __construct(IPv4Network $network) |
27
|
|
|
{ |
28
|
29 |
|
$this->network = $network; |
29
|
29 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Checks if this network is equal to the specified network. |
33
|
|
|
* |
34
|
|
|
* @param IPv4Network $interval |
35
|
|
|
* |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function equal(IPv4Network $interval) |
39
|
|
|
{ |
40
|
|
|
return |
41
|
|
|
$this->network->startPoint()->eq($interval->startPoint()) && |
42
|
|
|
$this->network->mask()->equal($interval->mask()) |
|
|
|
|
43
|
|
|
; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Does this network contain the specified IP. |
48
|
|
|
* |
49
|
|
|
* @param IPv4NetworkPoint $point |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
6 |
|
public function contains(IPv4NetworkPoint $point) |
54
|
|
|
{ |
55
|
6 |
|
return ($point->value() & $this->network->mask()->ip()) == $this->network->startPoint()->value(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Does this network intersect the specified network. |
60
|
|
|
* |
61
|
|
|
* @param IPv4Network $network |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
3 |
|
public function intersects(IPv4Network $network) |
66
|
|
|
{ |
67
|
|
|
if ( |
68
|
3 |
|
$this->network->startPoint()->gt($network->endPoint()) || |
69
|
3 |
|
$this->network->endPoint()->lt($network->startPoint()) |
70
|
|
|
) { |
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Does this network abut with the network specified. |
79
|
|
|
* |
80
|
|
|
* @param IPv4Network $network |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function abuts(IPv4Network $network) |
85
|
|
|
{ |
86
|
|
|
return |
87
|
|
|
$network->endPoint()->eq($this->network->startPoint()) || |
88
|
|
|
$this->network->endPoint()->eq($network->startPoint()) |
89
|
|
|
; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.