@@ -23,17 +23,21 @@ 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
+
26
31
$ this ->database = $ database ;
27
32
28
33
$ this ->init ();
29
34
}
30
35
36
+ /**
37
+ * @throws \Exception
38
+ */
31
39
private function init ()
32
40
{
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
- }
37
41
$ this ->file = @fopen ($ this ->database , 'rb ' );
38
42
if ($ this ->file === FALSE )
39
43
{
@@ -67,7 +71,7 @@ private function init()
67
71
/**
68
72
* @param $ip
69
73
* @param string $language
70
- * @return array|NULL
74
+ * @return array|null
71
75
*/
72
76
public function find ($ ip , $ language = 'CN ' )
73
77
{
@@ -90,32 +94,25 @@ public function find($ip, $language = 'CN')
90
94
{
91
95
if (!$ this ->supportV4 ())
92
96
{
93
-
97
+ throw new \ InvalidArgumentException ( " The Database not support IPv4 address. " );
94
98
}
95
99
}
96
100
elseif (strpos ($ ip , ': ' ) !== FALSE )
97
101
{
98
102
if (!$ this ->supportV6 ())
99
103
{
100
-
104
+ throw new \ InvalidArgumentException ( " The Database not support IPv6 address. " );
101
105
}
102
106
}
103
107
104
- try
108
+ $ node = $ this ->findNode ($ ip );
109
+ if ($ node > 0 )
105
110
{
106
- $ node = $ this ->findNode ($ ip );
107
- if ($ node > 0 )
108
- {
109
- $ data = $ this ->resolve ($ node );
111
+ $ data = $ this ->resolve ($ node );
110
112
111
- $ values = explode ("\t" , $ data );
113
+ $ values = explode ("\t" , $ data );
112
114
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 ' ]));
119
116
}
120
117
121
118
return NULL ;
@@ -124,7 +121,7 @@ public function find($ip, $language = 'CN')
124
121
public function findMap ($ ip , $ language = 'CN ' )
125
122
{
126
123
$ array = $ this ->find ($ ip , $ language );
127
- if (NULL == $ array )
124
+ if (NULL === $ array )
128
125
{
129
126
return NULL ;
130
127
}
@@ -135,7 +132,7 @@ public function findMap($ip, $language = 'CN')
135
132
public function findInfo ($ ip , $ language = 'CN ' )
136
133
{
137
134
$ map = $ this ->findMap ($ ip , $ language );
138
- if (NULL == $ map )
135
+ if (NULL === $ map )
139
136
{
140
137
return NULL ;
141
138
}
@@ -146,7 +143,7 @@ public function findInfo($ip, $language = 'CN')
146
143
/**
147
144
* @param $ip
148
145
* @return int
149
- * @throws \Exception
146
+ * @throws \RuntimeException
150
147
*/
151
148
private function findNode ($ ip )
152
149
{
@@ -218,7 +215,7 @@ private function findNode($ip)
218
215
return $ node ;
219
216
}
220
217
221
- throw new \Exception ("find node failed " );
218
+ throw new \RuntimeException ("find node failed " );
222
219
}
223
220
224
221
/**
0 commit comments