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
|
|
|
* @var \Arcanedev\GeoIP\Contracts\GeoIP |
20
|
|
|
*/ |
21
|
|
|
private $geoip; |
22
|
|
|
|
23
|
|
|
/* ------------------------------------------------------------------------------------------------ |
24
|
|
|
| Constructor |
25
|
|
|
| ------------------------------------------------------------------------------------------------ |
26
|
|
|
*/ |
27
|
18 |
|
public function __construct(GeoIP $geoip) |
28
|
|
|
{ |
29
|
18 |
|
$this->geoip = $geoip; |
30
|
18 |
|
} |
31
|
|
|
|
32
|
|
|
/* ------------------------------------------------------------------------------------------------ |
33
|
|
|
| Main Functions |
34
|
|
|
| ------------------------------------------------------------------------------------------------ |
35
|
|
|
*/ |
36
|
|
|
/** |
37
|
|
|
* Get the geoip data. |
38
|
|
|
* |
39
|
|
|
* @param string $ipAddress |
40
|
|
|
* |
41
|
|
|
* @return array|null |
42
|
|
|
*/ |
43
|
18 |
|
public function search($ipAddress) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
18 |
|
if ($location = $this->geoip->location($ipAddress)) { |
47
|
18 |
|
return $this->renderData($location); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
catch (\Exception $e) { |
51
|
|
|
// do nothing |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/* ------------------------------------------------------------------------------------------------ |
58
|
|
|
| Other Functions |
59
|
|
|
| ------------------------------------------------------------------------------------------------ |
60
|
|
|
*/ |
61
|
|
|
/** |
62
|
|
|
* Render the data. |
63
|
|
|
* |
64
|
|
|
* @param \Arcanedev\GeoIP\Location $location |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
18 |
|
private function renderData($location) |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
18 |
|
'iso_code' => $location->iso_code, |
72
|
18 |
|
'country' => $location->country, |
73
|
18 |
|
'city' => $location->city, |
74
|
18 |
|
'state' => $location->state, |
75
|
18 |
|
'state_code' => $location->state_code, |
76
|
18 |
|
'postal_code' => $location->postal_code, |
77
|
18 |
|
'latitude' => $location->latitude, |
78
|
18 |
|
'longitude' => $location->longitude, |
79
|
18 |
|
'timezone' => $location->timezone, |
80
|
18 |
|
'continent' => $location->continent, |
81
|
18 |
|
'currency' => $location->currency, |
82
|
9 |
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|