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\IPv6; |
12
|
|
|
|
13
|
|
|
use GpsLab\Component\Interval\Exception\InvalidPointTypeException; |
14
|
|
|
use GpsLab\Component\Interval\BaseIntervalPoint; |
15
|
|
|
use GpsLab\Component\Interval\IPv4\IPv4IntervalPoint; |
16
|
|
|
|
17
|
|
|
class IPv6IntervalPoint extends BaseIntervalPoint |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const IPV6_SEG = '[0-9a-fA-F]{1,4}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* IPv6 address. |
26
|
|
|
* |
27
|
|
|
* Ignore: |
28
|
|
|
* link-local IPv6 addresses with zone index |
29
|
|
|
* |
30
|
|
|
* @see http://vernon.mauery.com/content/projects/linux/ipv6_regex |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const IPV6_ADDR = '( |
35
|
|
|
('.self::IPV6_SEG.':){7,7}'.self::IPV6_SEG.'| # 1:2:3:4:5:6:7:8 |
36
|
|
|
('.self::IPV6_SEG.':){1,7}:| # 1:: 1:2:3:4:5:6:7:: |
37
|
|
|
('.self::IPV6_SEG.':){1,6}:'.self::IPV6_SEG.'| # 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8 |
38
|
|
|
('.self::IPV6_SEG.':){1,5}(:'.self::IPV6_SEG.'){1,2}| # 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8 |
39
|
|
|
('.self::IPV6_SEG.':){1,4}(:'.self::IPV6_SEG.'){1,3}| # 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8 |
40
|
|
|
('.self::IPV6_SEG.':){1,3}(:'.self::IPV6_SEG.'){1,4}| # 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8 |
41
|
|
|
('.self::IPV6_SEG.':){1,2}(:'.self::IPV6_SEG.'){1,5}| # 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8 |
42
|
|
|
'.self::IPV6_SEG.':((:'.self::IPV6_SEG.'){1,6})| # 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8 |
43
|
|
|
:((:'.self::IPV6_SEG.'){1,7}|:)| # ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: |
44
|
|
|
|
45
|
|
|
# link-local IPv6 addresses with zone index |
46
|
|
|
#fe80:(:'.self::IPV6_SEG.'){0,4}%[0-9a-zA-Z]{1,}| # fe80::7:8%eth0 fe80::7:8%1 |
47
|
|
|
|
48
|
|
|
# IPv4-mapped IPv6 addresses and IPv4-translated addresses |
49
|
|
|
# ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 |
50
|
|
|
::(ffff(:0{1,4}){0,1}:){0,1}'.IPv4IntervalPoint::IPV4_ADDR.'| |
51
|
|
|
|
52
|
|
|
# IPv4-Embedded IPv6 Address |
53
|
|
|
# 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 |
54
|
|
|
('.self::IPV6_SEG.':){1,4}:'.IPv4IntervalPoint::IPV4_ADDR.' |
55
|
|
|
)'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $ip; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
private $long; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $ip |
69
|
|
|
*/ |
70
|
40 |
View Code Duplication |
public function __construct($ip) |
|
|
|
|
71
|
|
|
{ |
72
|
40 |
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { |
73
|
4 |
|
throw InvalidPointTypeException::point('IPv6', $ip); |
74
|
|
|
} |
75
|
|
|
|
76
|
36 |
|
$this->ip = $ip; |
77
|
36 |
|
$this->long = inet_pton($ip); |
|
|
|
|
78
|
36 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
36 |
|
public function value() |
84
|
|
|
{ |
85
|
36 |
|
return $this->long; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
24 |
|
public function __toString() |
92
|
|
|
{ |
93
|
24 |
|
return $this->ip; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.