1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Detectors; |
2
|
|
|
|
3
|
|
|
use Arcanedev\GeoIP\Contracts\GeoIP; |
4
|
|
|
use Arcanedev\LaravelTracker\Contracts\Detectors\GeoIpDetector as GeoIpDetectorContract; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class GeoIpDetector |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelTracker\Detectors |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class GeoIpDetector implements GeoIpDetectorContract |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Properties |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Arcanedev\GeoIP\Contracts\GeoIP |
21
|
|
|
*/ |
22
|
|
|
private $geoIp; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Constructor |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* GeoIpDetector constructor. |
31
|
|
|
* |
32
|
|
|
* @param \Arcanedev\GeoIP\Contracts\GeoIP $geoIp |
33
|
|
|
*/ |
34
|
9 |
|
public function __construct(GeoIP $geoIp) |
35
|
|
|
{ |
36
|
9 |
|
$this->geoIp = $geoIp; |
37
|
9 |
|
} |
38
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
40
|
|
|
| Main Methods |
41
|
|
|
| ----------------------------------------------------------------- |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the geoip data. |
46
|
|
|
* |
47
|
|
|
* @param string $ipAddress |
48
|
|
|
* |
49
|
|
|
* @return array|null |
50
|
|
|
*/ |
51
|
9 |
|
public function search($ipAddress) |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
9 |
|
if ($location = $this->geoIp->location($ipAddress)) { |
55
|
9 |
|
return $this->transform($location); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
catch (\Exception $e) { |
59
|
|
|
// do nothing |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/* ----------------------------------------------------------------- |
66
|
|
|
| Other Methods |
67
|
|
|
| ----------------------------------------------------------------- |
68
|
|
|
*/ |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Render the data. |
72
|
|
|
* |
73
|
|
|
* @param \Arcanedev\GeoIP\Location $location |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
9 |
|
private function transform($location) |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
9 |
|
'iso_code' => $location->iso_code, |
81
|
9 |
|
'country' => $location->country, |
82
|
9 |
|
'city' => $location->city, |
83
|
9 |
|
'state' => $location->state, |
84
|
9 |
|
'state_code' => $location->state_code, |
85
|
9 |
|
'postal_code' => $location->postal_code, |
86
|
9 |
|
'latitude' => $location->latitude, |
87
|
9 |
|
'longitude' => $location->longitude, |
88
|
9 |
|
'timezone' => $location->timezone, |
89
|
9 |
|
'continent' => $location->continent, |
90
|
9 |
|
'currency' => $location->currency, |
91
|
3 |
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|