|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DucCnzj\Ip; |
|
4
|
|
|
|
|
5
|
|
|
use DucCnzj\Ip\Imp\DataMapImp; |
|
6
|
|
|
use DucCnzj\Ip\Imp\CacheStoreImp; |
|
7
|
|
|
use DucCnzj\Ip\Imp\RequestHandlerImp; |
|
8
|
|
|
use DucCnzj\Ip\Traits\HandleProvider; |
|
9
|
|
|
use DucCnzj\Ip\Exceptions\InvalidIpAddress; |
|
10
|
|
|
use DucCnzj\Ip\Exceptions\ServerErrorException; |
|
11
|
|
|
use DucCnzj\Ip\Exceptions\IpProviderClassNotExistException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @method string getCity() |
|
15
|
|
|
* @method string getCountry() |
|
16
|
|
|
* @method string getAddress() |
|
17
|
|
|
* @method string getRegion() |
|
18
|
|
|
* |
|
19
|
|
|
* Class IpClient |
|
20
|
|
|
* |
|
21
|
|
|
* @package DucCnzj\Ip |
|
22
|
|
|
*/ |
|
23
|
|
|
class IpClient |
|
24
|
|
|
{ |
|
25
|
|
|
use HandleProvider; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $ip; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var DataMapImp |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $dataMapper; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var RequestHandlerImp|null |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $requestHandler; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param int $times |
|
44
|
|
|
* |
|
45
|
|
|
* @return $this |
|
46
|
|
|
* |
|
47
|
|
|
* @author duc <[email protected]> |
|
48
|
|
|
*/ |
|
49
|
5 |
|
public function try(int $times) |
|
50
|
|
|
{ |
|
51
|
5 |
|
$this->requestHandler = $this->getRequestHandler()->setTryTimes($times); |
|
52
|
|
|
|
|
53
|
5 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $ip |
|
58
|
|
|
* |
|
59
|
|
|
* @throws InvalidIpAddress |
|
60
|
|
|
* |
|
61
|
|
|
* @author duc <[email protected]> |
|
62
|
|
|
*/ |
|
63
|
19 |
|
public function checkIp(string $ip) |
|
64
|
|
|
{ |
|
65
|
19 |
|
$b = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip); |
|
66
|
|
|
|
|
67
|
19 |
|
if (! $b) { |
|
68
|
1 |
|
throw new InvalidIpAddress; |
|
69
|
|
|
} |
|
70
|
18 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $msg |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
* |
|
77
|
|
|
* @author duc <[email protected]> |
|
78
|
|
|
*/ |
|
79
|
5 |
|
protected function responseWithError(string $msg) |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
5 |
|
'success' => 0, |
|
83
|
5 |
|
'message' => $msg, |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return DataMapper|DataMapImp|NullDataMapper |
|
89
|
|
|
* |
|
90
|
|
|
* @throws IpProviderClassNotExistException |
|
91
|
|
|
* @throws \Exception |
|
92
|
|
|
* @author duc <[email protected]> |
|
93
|
|
|
*/ |
|
94
|
8 |
|
public function getDataMapper() |
|
95
|
|
|
{ |
|
96
|
8 |
|
$response = $this->getOriginalInfo(); |
|
97
|
|
|
|
|
98
|
8 |
|
if (! $response['success']) { |
|
99
|
4 |
|
return (new NullDataMapper())->setInfo(['ip' => $this->getIp()]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
4 |
|
if (! $this->dataMapper) { |
|
103
|
4 |
|
$this->dataMapper = new DataMapper(); |
|
104
|
|
|
|
|
105
|
4 |
|
return $this->dataMapper->setInfo($response); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $this->dataMapper->setInfo($response); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return RequestHandlerImp|RequestHandler|null |
|
113
|
|
|
* |
|
114
|
|
|
* @author duc <[email protected]> |
|
115
|
|
|
*/ |
|
116
|
15 |
|
public function getRequestHandler() |
|
117
|
|
|
{ |
|
118
|
15 |
|
if (! $this->requestHandler) { |
|
119
|
12 |
|
return $this->requestHandler = new RequestHandler(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
7 |
|
return $this->requestHandler; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return array|mixed |
|
127
|
|
|
* |
|
128
|
|
|
* @throws IpProviderClassNotExistException |
|
129
|
|
|
* @throws \Exception |
|
130
|
|
|
* @author duc <[email protected]> |
|
131
|
|
|
*/ |
|
132
|
13 |
|
public function getOriginalInfo() |
|
133
|
|
|
{ |
|
134
|
|
|
try { |
|
135
|
13 |
|
$result = $this->getRequestHandler() |
|
136
|
13 |
|
->send($this->resolveProviders(), $this->getIp()); |
|
137
|
8 |
|
} catch (ServerErrorException $e) { |
|
138
|
5 |
|
return $this->responseWithError($e->getMessage()); |
|
139
|
3 |
|
} catch (\RuntimeException $exception) { |
|
140
|
3 |
|
throw $exception; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
6 |
|
return $result; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return array |
|
148
|
|
|
* |
|
149
|
|
|
* @author duc <[email protected]> |
|
150
|
|
|
*/ |
|
151
|
3 |
|
public function getErrors(): array |
|
152
|
|
|
{ |
|
153
|
3 |
|
return $this->getRequestHandler()->getErrors(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return string |
|
158
|
|
|
* @throws \Exception |
|
159
|
|
|
* |
|
160
|
|
|
* @author duc <[email protected]> |
|
161
|
|
|
*/ |
|
162
|
19 |
|
public function getIp() |
|
163
|
|
|
{ |
|
164
|
19 |
|
if (! $this->ip) { |
|
165
|
1 |
|
throw new \Exception('请先设置 ip'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
18 |
|
return $this->ip; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return CacheStore|CacheStoreImp |
|
173
|
|
|
* |
|
174
|
|
|
* @author duc <[email protected]> |
|
175
|
|
|
*/ |
|
176
|
8 |
|
public function getCacheStore() |
|
177
|
|
|
{ |
|
178
|
8 |
|
return $this->getRequestHandler()->getCacheStore(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $ip |
|
183
|
|
|
* |
|
184
|
|
|
* @return $this |
|
185
|
|
|
* |
|
186
|
|
|
* @throws InvalidIpAddress |
|
187
|
|
|
* @author duc <[email protected]> |
|
188
|
|
|
*/ |
|
189
|
18 |
|
public function setIp(string $ip) |
|
190
|
|
|
{ |
|
191
|
18 |
|
$this->checkIp($ip); |
|
192
|
|
|
|
|
193
|
18 |
|
$this->ip = $ip; |
|
194
|
|
|
|
|
195
|
18 |
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param DataMapImp $dataMapper |
|
200
|
|
|
* |
|
201
|
|
|
* @return $this |
|
202
|
|
|
* |
|
203
|
|
|
* @author duc <[email protected]> |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function setDataMapper(DataMapImp $dataMapper) |
|
206
|
|
|
{ |
|
207
|
1 |
|
$this->dataMapper = $dataMapper; |
|
208
|
|
|
|
|
209
|
1 |
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param RequestHandlerImp $requestHandler |
|
214
|
|
|
* |
|
215
|
|
|
* @return $this |
|
216
|
|
|
* |
|
217
|
|
|
* @author duc <[email protected]> |
|
218
|
|
|
*/ |
|
219
|
3 |
|
public function setRequestHandler(RequestHandlerImp $requestHandler) |
|
220
|
|
|
{ |
|
221
|
3 |
|
$this->requestHandler = $requestHandler; |
|
222
|
|
|
|
|
223
|
3 |
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param CacheStoreImp $cacheStore |
|
228
|
|
|
* |
|
229
|
|
|
* @return $this |
|
230
|
|
|
* |
|
231
|
|
|
* @author duc <[email protected]> |
|
232
|
|
|
*/ |
|
233
|
2 |
|
public function setCacheStore(CacheStoreImp $cacheStore) |
|
234
|
|
|
{ |
|
235
|
2 |
|
$this->requestHandler = $this->getRequestHandler()->setCacheStore($cacheStore); |
|
236
|
|
|
|
|
237
|
2 |
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @param string $name |
|
242
|
|
|
* @param $arguments |
|
243
|
|
|
* @return mixed |
|
244
|
|
|
* @throws \Exception |
|
245
|
|
|
* |
|
246
|
|
|
* @author duc <[email protected]> |
|
247
|
|
|
*/ |
|
248
|
1 |
|
public function __call(string $name, $arguments) |
|
249
|
|
|
{ |
|
250
|
1 |
|
return $this->getDataMapper()->{$name}(...$arguments); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param string $name |
|
255
|
|
|
* @return mixed|string|null |
|
256
|
|
|
* @throws \Exception |
|
257
|
|
|
* |
|
258
|
|
|
* @author duc <[email protected]> |
|
259
|
|
|
*/ |
|
260
|
1 |
|
public function __get(string $name) |
|
261
|
|
|
{ |
|
262
|
1 |
|
return $this->getDataMapper()->{$name}; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|