|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yiicod\geo\adapters\geoIp2; |
|
4
|
|
|
|
|
5
|
|
|
use GeoIp2\Database\Reader; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yiicod\geo\base\GeoAdapterInterface; |
|
8
|
|
|
use yiicod\geo\base\GeoDataInterface; |
|
9
|
|
|
use yiicod\geo\exceptions\EmptyLocationDataException; |
|
10
|
|
|
use yiicod\geo\GeoData; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class GeoIp2OfflineAdapter |
|
14
|
|
|
* |
|
15
|
|
|
* @package yiicod\geo\adapters\geoIpOffline |
|
16
|
|
|
*/ |
|
17
|
|
|
class GeoIp2CityAdapter implements GeoAdapterInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
public $databaseConfig = [ |
|
23
|
|
|
'class' => GeoIp2Database::class, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* GeoIp2 Reader |
|
28
|
|
|
* |
|
29
|
|
|
* @var Reader |
|
30
|
|
|
*/ |
|
31
|
|
|
private $geoIpReader; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Loads location data from source for $ip |
|
35
|
|
|
* |
|
36
|
|
|
* @param $ip |
|
37
|
|
|
* |
|
38
|
|
|
* @return \GeoIp2\Model\City |
|
39
|
|
|
*/ |
|
40
|
|
|
public function loadLocationData($ip) |
|
41
|
|
|
{ |
|
42
|
|
|
$reader = $this->getGeoIpReader(); |
|
43
|
|
|
|
|
44
|
|
|
$data = $reader ? $this->getGeoIpReader()->city($ip) : null; |
|
45
|
|
|
|
|
46
|
|
|
return $data; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Gets location data from loaded data |
|
51
|
|
|
* |
|
52
|
|
|
* @param $ip |
|
53
|
|
|
* |
|
54
|
|
|
* @return GeoDataInterface |
|
55
|
|
|
* |
|
56
|
|
|
* @throws EmptyLocationDataException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getLocationData($ip): GeoDataInterface |
|
59
|
|
|
{ |
|
60
|
|
|
$locationData = $this->loadLocationData($ip); |
|
61
|
|
|
if (empty($locationData)) { |
|
62
|
|
|
throw new EmptyLocationDataException("Location data is empty"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$region = $locationData->subdivisions[0] ?? null; |
|
66
|
|
|
$data = [ |
|
67
|
|
|
'ip' => $ip, |
|
68
|
|
|
'countryCode' => $locationData->country->isoCode, |
|
69
|
|
|
'countryName' => $locationData->country->names['en'], |
|
70
|
|
|
'regionName' => $region->names['en'] ?? null, |
|
71
|
|
|
'regionCode' => $region->isoCode ?? null, |
|
72
|
|
|
'city' => $locationData->city->names['en'], |
|
73
|
|
|
'latitude' => $locationData->location->latitude, |
|
74
|
|
|
'longitude' => $locationData->location->longitude, |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
$result = new GeoData($data); |
|
78
|
|
|
return $result; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Gets adapter's name |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getName(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return 'geo_ip_city_offline'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Gets GeoIP Reader |
|
93
|
|
|
* |
|
94
|
|
|
* @return Reader |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getGeoIpReader(): ?Reader |
|
97
|
|
|
{ |
|
98
|
|
|
if (is_null($this->geoIpReader)) { |
|
99
|
|
|
/** @var GeoIpDatabaseInterface $db */ |
|
100
|
|
|
$db = Yii::createObject($this->databaseConfig); |
|
101
|
|
|
if (true === $db->hasDbFile()) { |
|
102
|
|
|
$this->geoIpReader = new Reader($db->getFileName()); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->geoIpReader; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|