Skip to content

Commit f5fd8c8

Browse files
committed
update
1 parent 183be89 commit f5fd8c8

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

example.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
require_once __DIR__ . '/src/ipip/db/Info.php';
44
require_once __DIR__ . '/src/ipip/db/Reader.php';
55

6-
$db = new ipip\db\Reader('c:\work\tiantexin\bb\v6\mydatavipday2.ipdb');
6+
$db = new ipip\db\Reader('c:\work\tiantexin\bb\v6\mydata6vipday4.ipdb');
77
$loc = $db->find("2001:250:200::");
88
$map = $db->findMap("2001:250:200::");
99
$obj = $db->findInfo("2001:250:200::");
10-
var_dump($loc, $map, $obj);
10+
var_dump($loc, $map, $obj);
11+
12+
try
13+
{
14+
var_dump($db->find("255.255.255.1"));
15+
}
16+
catch (Exception $e)
17+
{
18+
exit($e->getMessage());
19+
}

src/ipip/db/Reader.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ class Reader
2323
*/
2424
public function __construct($database)
2525
{
26+
if (is_readable($database) === FALSE)
27+
{
28+
throw new \InvalidArgumentException("The IP Database file \"{$database}\" does not exist or is not readable.");
29+
}
30+
2631
$this->database = $database;
2732

2833
$this->init();
2934
}
3035

36+
/**
37+
* @throws \Exception
38+
*/
3139
private function init()
3240
{
33-
if (is_readable($this->database) === FALSE)
34-
{
35-
throw new \InvalidArgumentException("The IP Database file \"{$this->database}\" does not exist or is not readable.");
36-
}
3741
$this->file = @fopen($this->database, 'rb');
3842
if ($this->file === FALSE)
3943
{
@@ -67,7 +71,7 @@ private function init()
6771
/**
6872
* @param $ip
6973
* @param string $language
70-
* @return array|NULL
74+
* @return array|null
7175
*/
7276
public function find($ip, $language = 'CN')
7377
{
@@ -90,32 +94,25 @@ public function find($ip, $language = 'CN')
9094
{
9195
if (!$this->supportV4())
9296
{
93-
97+
throw new \InvalidArgumentException("The Database not support IPv4 address.");
9498
}
9599
}
96100
elseif (strpos($ip, ':') !== FALSE)
97101
{
98102
if (!$this->supportV6())
99103
{
100-
104+
throw new \InvalidArgumentException("The Database not support IPv6 address.");
101105
}
102106
}
103107

104-
try
108+
$node = $this->findNode($ip);
109+
if ($node > 0)
105110
{
106-
$node = $this->findNode($ip);
107-
if ($node > 0)
108-
{
109-
$data = $this->resolve($node);
111+
$data = $this->resolve($node);
110112

111-
$values = explode("\t", $data);
113+
$values = explode("\t", $data);
112114

113-
return array_slice($values, $this->meta['languages'][$language], count($this->meta['fields']));
114-
}
115-
}
116-
catch (\Exception $e)
117-
{
118-
return NULL;
115+
return array_slice($values, $this->meta['languages'][$language], count($this->meta['fields']));
119116
}
120117

121118
return NULL;
@@ -124,7 +121,7 @@ public function find($ip, $language = 'CN')
124121
public function findMap($ip, $language = 'CN')
125122
{
126123
$array = $this->find($ip, $language);
127-
if (NULL == $array)
124+
if (NULL === $array)
128125
{
129126
return NULL;
130127
}
@@ -135,7 +132,7 @@ public function findMap($ip, $language = 'CN')
135132
public function findInfo($ip, $language = 'CN')
136133
{
137134
$map = $this->findMap($ip, $language);
138-
if (NULL == $map)
135+
if (NULL === $map)
139136
{
140137
return NULL;
141138
}
@@ -146,7 +143,7 @@ public function findInfo($ip, $language = 'CN')
146143
/**
147144
* @param $ip
148145
* @return int
149-
* @throws \Exception
146+
* @throws \RuntimeException
150147
*/
151148
private function findNode($ip)
152149
{
@@ -218,7 +215,7 @@ private function findNode($ip)
218215
return $node;
219216
}
220217

221-
throw new \Exception("find node failed");
218+
throw new \RuntimeException("find node failed");
222219
}
223220

224221
/**

0 commit comments

Comments
 (0)