1 | <?php namespace Arcanedev\LaravelTracker\Detectors; |
||
13 | class GeoIpDetector implements GeoIpDetectorContract |
||
14 | { |
||
15 | /* ------------------------------------------------------------------------------------------------ |
||
16 | | Properties |
||
17 | | ------------------------------------------------------------------------------------------------ |
||
18 | */ |
||
19 | private $reader; |
||
20 | |||
21 | /* ------------------------------------------------------------------------------------------------ |
||
22 | | Constructor |
||
23 | | ------------------------------------------------------------------------------------------------ |
||
24 | */ |
||
25 | 18 | public function __construct() |
|
29 | |||
30 | /* ------------------------------------------------------------------------------------------------ |
||
31 | | Main Functions |
||
32 | | ------------------------------------------------------------------------------------------------ |
||
33 | */ |
||
34 | /** |
||
35 | * Get the geoip data. |
||
36 | * |
||
37 | * @param string $ipAddress |
||
38 | * |
||
39 | * @return array|null |
||
40 | */ |
||
41 | 12 | public function search($ipAddress) |
|
42 | { |
||
43 | try { |
||
44 | 12 | if ($cityModel = $this->reader->city($ipAddress)) { |
|
45 | 6 | return $this->renderData($cityModel); |
|
46 | } |
||
47 | } |
||
48 | 6 | catch (AddressNotFoundException $e) { |
|
49 | // do nothing |
||
50 | } |
||
51 | |||
52 | 6 | return null; |
|
53 | } |
||
54 | |||
55 | /* ------------------------------------------------------------------------------------------------ |
||
56 | | Other Functions |
||
57 | | ------------------------------------------------------------------------------------------------ |
||
58 | */ |
||
59 | /** |
||
60 | * Render the data. |
||
61 | * |
||
62 | * @param \GeoIp2\Model\City $cityModel |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | 6 | private function renderData($cityModel) |
|
67 | { |
||
68 | return [ |
||
69 | 6 | 'latitude' => $cityModel->location->latitude, |
|
70 | 6 | 'longitude' => $cityModel->location->longitude, |
|
71 | 6 | 'country_code' => $cityModel->country->isoCode, |
|
72 | 3 | 'country_code3' => null, |
|
73 | 6 | 'country_name' => $cityModel->country->name, |
|
74 | 6 | 'region' => $cityModel->continent->code, |
|
75 | 6 | 'city' => $cityModel->city->name, |
|
76 | 6 | 'postal_code' => $cityModel->postal->code, |
|
77 | 3 | 'area_code' => null, |
|
78 | 3 | 'dma_code' => null, |
|
79 | 6 | 'metro_code' => $cityModel->location->metroCode, |
|
80 | 6 | 'continent_code' => $cityModel->continent->code, |
|
81 | 3 | ]; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get the GeoLiteFileName. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 18 | private function getGeoliteFileName() |
|
93 | } |
||
94 |