forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeGeoIp.php
More file actions
185 lines (164 loc) · 5.67 KB
/
Copy pathFreeGeoIp.php
File metadata and controls
185 lines (164 loc) · 5.67 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?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 Geocoder\Provider;
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class FreeGeoIp extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
const ENDPOINT_URL = 'http://freegeoip.net/json/%s';
/**
* {@inheritDoc}
*/
public function geocode($address)
{
if (!filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.');
}
if (in_array($address, array('127.0.0.1', '::1'))) {
return $this->returnResults([ $this->getLocalhostDefaults() ]);
}
$query = sprintf(self::ENDPOINT_URL, $address);
return $this->executeQuery($query);
}
/**
* {@inheritDoc}
*/
public function reverse($latitude, $longitude)
{
throw new UnsupportedOperation('The FreeGeoIp provider is not able to do reverse geocoding.');
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'free_geo_ip';
}
/**
* @param string $query
*/
private function executeQuery($query)
{
$content = (string) $this->getAdapter()->get($query)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Could not execute query %s', $query));
}
$data = (array) json_decode($content);
if (empty($data)) {
throw new NoResult(sprintf('Could not execute query %s', $query));
}
// it appears that for US states the region code is not returning the FIPS standard
if ('US' === $data['country_code'] && isset($data['region_code']) && !is_numeric($data['region_code'])) {
$newRegionCode = $this->stateToRegionCode($data['region_code']);
$data['region_code'] = is_numeric($newRegionCode) ? $newRegionCode : null;
}
$adminLevels = [];
if (! empty($data['region_name']) || ! empty($data['region_code'])) {
$adminLevels[] = [
'name' => isset($data['region_name']) ? $data['region_name'] : null,
'code' => isset($data['region_code']) ? $data['region_code'] : null,
'level' => 1
];
}
return $this->returnResults([
array_merge($this->getDefaults(), array(
'latitude' => isset($data['latitude']) ? $data['latitude'] : null,
'longitude' => isset($data['longitude']) ? $data['longitude'] : null,
'locality' => isset($data['city']) ? $data['city'] : null,
'postalCode' => isset($data['zip_code']) ? $data['zip_code'] : null,
'adminLevels' => $adminLevels,
'country' => isset($data['country_name']) ? $data['country_name'] : null,
'countryCode' => isset($data['country_code']) ? $data['country_code'] : null,
))
]);
}
/**
* Converts the state code to FIPS standard.
*
* @param string $state
*
* @return string|integer The FIPS code or the state code if not found
*/
private function stateToRegionCode($state)
{
$codes = $this->getRegionCodes();
return array_key_exists($state, $codes) ? $codes[$state] : $state;
}
/**
* Returns an array of state codes => FIPS codes
* @see http://www.epa.gov/enviro/html/codes/state.html
*
* @return array
*/
private function getRegionCodes()
{
return array(
'AK' => 2, //ALASKA
'AL' => 1, //ALABAMA
'AR' => 5, //ARKANSAS
'AS' => 60, //AMERICAN SAMOA
'AZ' => 4, //ARIZONA
'CA' => 6, //CALIFORNIA
'CO' => 8, //COLORADO
'CT' => 9, //CONNECTICUT
'DC' => 11, //DISTRICT OF COLUMBIA
'DE' => 10, //DELAWARE
'FL' => 12, //FLORIDA
'GA' => 13, //GEORGIA
'GU' => 66, //GUAM
'HI' => 15, //HAWAII
'IA' => 19, //IOWA
'ID' => 16, //IDAHO
'IL' => 17, //ILLINOIS
'IN' => 18, //INDIANA
'KS' => 20, //KANSAS
'KY' => 21, //KENTUCKY
'LA' => 22, //LOUISIANA
'MA' => 25, //MASSACHUSETTS
'MD' => 24, //MARYLAND
'ME' => 23, //MAINE
'MI' => 26, //MICHIGAN
'MN' => 27, //MINNESOTA
'MO' => 29, //MISSOURI
'MS' => 28, //MISSISSIPPI
'MT' => 30, //MONTANA
'NC' => 37, //NORTH CAROLINA
'ND' => 38, //NORTH DAKOTA
'NE' => 31, //NEBRASKA
'NH' => 33, //NEW HAMPSHIRE
'NJ' => 34, //NEW JERSEY
'NM' => 35, //NEW MEXICO
'NV' => 32, //NEVADA
'NY' => 36, //NEW YORK
'OH' => 39, //OHIO
'OK' => 40, //OKLAHOMA
'OR' => 41, //OREGON
'PA' => 42, //PENNSYLVANIA
'PR' => 72, //PUERTO RICO
'RI' => 44, //RHODE ISLAND
'SC' => 45, //SOUTH CAROLINA
'SD' => 46, //SOUTH DAKOTA
'TN' => 47, //TENNESSEE
'TX' => 48, //TEXAS
'UT' => 49, //UTAH
'VA' => 51, //VIRGINIA
'VI' => 78, //VIRGIN ISLANDS
'VT' => 50, //VERMONT
'WA' => 53, //WASHINGTON
'WI' => 55, //WISCONSIN
'WV' => 54, //WEST VIRGINIA
);
}
}