forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoIP2.php
More file actions
113 lines (94 loc) · 3.54 KB
/
Copy pathGeoIP2.php
File metadata and controls
113 lines (94 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace _9Code\Geocoder\Provider;
use GeoIp2\Exception\AddressNotFoundException;
use _9Code\Geocoder\Adapter\GeoIP2Adapter;
use _9Code\Geocoder\Exception\NoResult;
use _9Code\Geocoder\Exception\UnsupportedOperation;
/**
* @author Jens Wiese <jens@howtrueisfalse.de>
*/
class GeoIP2 extends AbstractProvider implements LocaleAwareProvider
{
use LocaleTrait;
/**
* @var GeoIP2Adapter
*/
private $adapter;
public function __construct(GeoIP2Adapter $adapter, $locale = 'en')
{
parent::__construct();
$this->adapter = $adapter;
$this->locale = $locale;
}
/**
* {@inheritDoc}
*/
public function geocode($address)
{
if (!filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The GeoIP2 provider does not support street addresses, only IP addresses.');
}
if ('127.0.0.1' === $address) {
return $this->returnResults([ $this->getLocalhostDefaults() ]);
}
$result = json_decode($this->executeQuery($address));
$adminLevels = [];
if (isset($result->subdivisions) && is_array($result->subdivisions)) {
foreach ($result->subdivisions as $i => $subdivision) {
$name = (isset($subdivision->names->{$this->locale}) ? $subdivision->names->{$this->locale} : null);
$code = (isset($subdivision->iso_code) ? $subdivision->iso_code : null);
if (null !== $name || null !== $code) {
$adminLevels[] = ['name' => $name, 'code' => $code, 'level' => $i + 1];
}
}
}
return $this->returnResults([
$this->fixEncoding(array_merge($this->getDefaults(), array(
'countryCode' => (isset($result->country->iso_code) ? $result->country->iso_code : null),
'country' => (isset($result->country->names->{$this->locale}) ? $result->country->names->{$this->locale} : null),
'locality' => (isset($result->city->names->{$this->locale}) ? $result->city->names->{$this->locale} : null),
'latitude' => (isset($result->location->latitude) ? $result->location->latitude : null),
'longitude' => (isset($result->location->longitude) ? $result->location->longitude : null),
'timezone' => (isset($result->location->time_zone) ? $result->location->time_zone : null),
'postalCode' => (isset($result->postal->code) ? $result->postal->code : null),
'adminLevels' => $adminLevels,
)))
]);
}
/**
* {@inheritDoc}
*/
public function reverse($latitude, $longitude)
{
throw new UnsupportedOperation('The GeoIP2 provider is not able to do reverse geocoding.');
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'geoip2';
}
/**
* @param string $address
*/
private function executeQuery($address)
{
$uri = sprintf('file://geoip?%s', $address);
try {
$result = $this->adapter
->setLocale($this->locale)
->getContent($uri);
} catch (AddressNotFoundException $e) {
throw new NoResult(sprintf('No results found for IP address "%s".', $address));
}
return $result;
}
}