|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yiicod\geo; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yiicod\geo\base\GeoFinderInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class GeoGetter |
|
10
|
|
|
* |
|
11
|
|
|
* @package yiicod\geo |
|
12
|
|
|
* |
|
13
|
|
|
* @author Alexey Orlov |
|
14
|
|
|
* @author Dmitry Turchanin |
|
15
|
|
|
*/ |
|
16
|
|
|
class GeoGetter |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Gets country code by IP |
|
20
|
|
|
* |
|
21
|
|
|
* @param null|string |
|
22
|
|
|
* |
|
23
|
|
|
* @return null|string |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function getCountryCode($ip = null): ?string |
|
26
|
|
|
{ |
|
27
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
28
|
|
|
|
|
29
|
|
|
return static::getGeoManager()->getCountryCode($ip); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Gets country name by IP |
|
34
|
|
|
* |
|
35
|
|
|
* @param null|string |
|
36
|
|
|
* |
|
37
|
|
|
* @return null|string |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function getCountryName($ip = null): ?string |
|
40
|
|
|
{ |
|
41
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
42
|
|
|
|
|
43
|
|
|
return static::getGeoManager()->getCountryName($ip); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Gets region by IP |
|
48
|
|
|
* |
|
49
|
|
|
* @param null|string $ip |
|
50
|
|
|
* |
|
51
|
|
|
* @return null|string |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getRegion($ip = null): ?string |
|
54
|
|
|
{ |
|
55
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
56
|
|
|
|
|
57
|
|
|
return static::getGeoManager()->getRegion($ip); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Gets region name by IP |
|
62
|
|
|
* |
|
63
|
|
|
* @param null|string $ip |
|
64
|
|
|
* |
|
65
|
|
|
* @return null|string |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getRegionName($ip = null): ?string |
|
68
|
|
|
{ |
|
69
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
70
|
|
|
|
|
71
|
|
|
return static::getGeoManager()->getRegionName($ip); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Gets region code by IP |
|
76
|
|
|
* |
|
77
|
|
|
* @param null|string $ip |
|
78
|
|
|
* |
|
79
|
|
|
* @return null|string |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function getRegionCode($ip = null): ?string |
|
82
|
|
|
{ |
|
83
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
84
|
|
|
|
|
85
|
|
|
return static::getGeoManager()->getRegionCode($ip); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Gets city by IP |
|
90
|
|
|
* |
|
91
|
|
|
* @param null|string $ip |
|
92
|
|
|
* |
|
93
|
|
|
* @return null|string |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function getCity($ip = null): ?string |
|
96
|
|
|
{ |
|
97
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
98
|
|
|
|
|
99
|
|
|
return static::getGeoManager()->getCity($ip); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets latitude by IP |
|
104
|
|
|
* |
|
105
|
|
|
* @param null|string $ip |
|
106
|
|
|
* |
|
107
|
|
|
* @return null|string |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function getLatitude($ip = null): ?string |
|
110
|
|
|
{ |
|
111
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
112
|
|
|
|
|
113
|
|
|
return static::getGeoManager()->getLatitude($ip); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Gets longitude by IP |
|
118
|
|
|
* |
|
119
|
|
|
* @param null|string $ip |
|
120
|
|
|
* |
|
121
|
|
|
* @return null|string |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function getLongitude($ip = null): ?string |
|
124
|
|
|
{ |
|
125
|
|
|
$ip = $ip ?? Yii::$app->request->getUserIP(); |
|
126
|
|
|
|
|
127
|
|
|
return static::getGeoManager()->getLongitude($ip); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns geo component |
|
132
|
|
|
* |
|
133
|
|
|
* @return GeoFinderInterface |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function getGeoManager(): GeoFinderInterface |
|
136
|
|
|
{ |
|
137
|
|
|
return Yii::$app->geoFinder; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|