@@ -23,21 +23,17 @@ class Reader
23
23
*/
24
24
public function __construct ($ database )
25
25
{
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
-
31
26
$ this ->database = $ database ;
32
27
33
28
$ this ->init ();
34
29
}
35
30
36
- /**
37
- * @throws \Exception
38
- */
39
31
private function init ()
40
32
{
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
+ }
41
37
$ this ->file = @fopen ($ this ->database , 'rb ' );
42
38
if ($ this ->file === FALSE )
43
39
{
@@ -71,7 +67,7 @@ private function init()
71
67
/**
72
68
* @param $ip
73
69
* @param string $language
74
- * @return array|null
70
+ * @return array|NULL
75
71
*/
76
72
public function find ($ ip , $ language = 'CN ' )
77
73
{
@@ -90,29 +86,30 @@ public function find($ip, $language = 'CN')
90
86
throw new \InvalidArgumentException ("The value \"$ ip \" is not a valid IP address. " );
91
87
}
92
88
93
- if (strpos ($ ip , '. ' ) !== FALSE )
89
+ if (strpos ($ ip , '. ' ) !== FALSE && ! $ this -> supportV4 () )
94
90
{
95
- if (!$ this ->supportV4 ())
96
- {
97
- throw new \InvalidArgumentException ("The Database not support IPv4 address. " );
98
- }
91
+ throw new \InvalidArgumentException ("The Database not support IPv4 address. " );
99
92
}
100
- elseif (strpos ($ ip , ': ' ) !== FALSE )
93
+ elseif (strpos ($ ip , ': ' ) !== FALSE && ! $ this -> supportV6 () )
101
94
{
102
- if (!$ this ->supportV6 ())
103
- {
104
- throw new \InvalidArgumentException ("The Database not support IPv6 address. " );
105
- }
95
+ throw new \InvalidArgumentException ("The Database not support IPv6 address. " );
106
96
}
107
97
108
- $ node = $ this ->findNode ($ ip );
109
- if ($ node > 0 )
98
+ try
110
99
{
111
- $ data = $ this ->resolve ($ node );
100
+ $ node = $ this ->findNode ($ ip );
101
+ if ($ node > 0 )
102
+ {
103
+ $ data = $ this ->resolve ($ node );
112
104
113
- $ values = explode ("\t" , $ data );
105
+ $ values = explode ("\t" , $ data );
114
106
115
- return array_slice ($ values , $ this ->meta ['languages ' ][$ language ], count ($ this ->meta ['fields ' ]));
107
+ return array_slice ($ values , $ this ->meta ['languages ' ][$ language ], count ($ this ->meta ['fields ' ]));
108
+ }
109
+ }
110
+ catch (\Exception $ e )
111
+ {
112
+ return NULL ;
116
113
}
117
114
118
115
return NULL ;
@@ -121,7 +118,7 @@ public function find($ip, $language = 'CN')
121
118
public function findMap ($ ip , $ language = 'CN ' )
122
119
{
123
120
$ array = $ this ->find ($ ip , $ language );
124
- if (NULL === $ array )
121
+ if (NULL == $ array )
125
122
{
126
123
return NULL ;
127
124
}
@@ -132,7 +129,7 @@ public function findMap($ip, $language = 'CN')
132
129
public function findInfo ($ ip , $ language = 'CN ' )
133
130
{
134
131
$ map = $ this ->findMap ($ ip , $ language );
135
- if (NULL === $ map )
132
+ if (NULL == $ map )
136
133
{
137
134
return NULL ;
138
135
}
@@ -143,7 +140,7 @@ public function findInfo($ip, $language = 'CN')
143
140
/**
144
141
* @param $ip
145
142
* @return int
146
- * @throws \RuntimeException
143
+ * @throws \Exception
147
144
*/
148
145
private function findNode ($ ip )
149
146
{
@@ -215,7 +212,7 @@ private function findNode($ip)
215
212
return $ node ;
216
213
}
217
214
218
- throw new \RuntimeException ("find node failed " );
215
+ throw new \Exception ("find node failed " );
219
216
}
220
217
221
218
/**
@@ -278,7 +275,7 @@ private function read($stream, $offset, $length)
278
275
}
279
276
}
280
277
281
- throw new \Exception ("The DB file read bad data " );
278
+ throw new \Exception ("The Database file read bad data " );
282
279
}
283
280
284
281
return '' ;
0 commit comments