Skip to content

Commit c5cb0e2

Browse files
committed
Merge pull request #473 from mattsches/master
Return ISO 3166-2 region codes for the US and the rest of the world.
2 parents 68aef29 + 9923793 commit c5cb0e2

6 files changed

+14
-94
lines changed

src/Geocoder/Provider/FreeGeoIp.php

+3-86
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Geocoder\Exception\NoResult;
1414
use Geocoder\Exception\UnsupportedOperation;
15+
use Geocoder\Model\AddressCollection;
1516

1617
/**
1718
* @author William Durand <william.durand1@gmail.com>
@@ -59,6 +60,8 @@ public function getName()
5960

6061
/**
6162
* @param string $query
63+
*
64+
* @return AddressCollection
6265
*/
6366
private function executeQuery($query)
6467
{
@@ -74,12 +77,6 @@ private function executeQuery($query)
7477
throw new NoResult(sprintf('Could not execute query %s', $query));
7578
}
7679

77-
// it appears that for US states the region code is not returning the FIPS standard
78-
if ('US' === $data['country_code'] && isset($data['region_code']) && !is_numeric($data['region_code'])) {
79-
$newRegionCode = $this->stateToRegionCode($data['region_code']);
80-
$data['region_code'] = is_numeric($newRegionCode) ? $newRegionCode : null;
81-
}
82-
8380
$adminLevels = [];
8481

8582
if (! empty($data['region_name']) || ! empty($data['region_code'])) {
@@ -103,84 +100,4 @@ private function executeQuery($query)
103100
))
104101
]);
105102
}
106-
107-
/**
108-
* Converts the state code to FIPS standard.
109-
*
110-
* @param string $state
111-
*
112-
* @return string|integer The FIPS code or the state code if not found
113-
*/
114-
private function stateToRegionCode($state)
115-
{
116-
$codes = $this->getRegionCodes();
117-
118-
return array_key_exists($state, $codes) ? $codes[$state] : $state;
119-
}
120-
121-
/**
122-
* Returns an array of state codes => FIPS codes
123-
* @see http://www.epa.gov/enviro/html/codes/state.html
124-
*
125-
* @return array
126-
*/
127-
private function getRegionCodes()
128-
{
129-
return array(
130-
'AK' => 2, //ALASKA
131-
'AL' => 1, //ALABAMA
132-
'AR' => 5, //ARKANSAS
133-
'AS' => 60, //AMERICAN SAMOA
134-
'AZ' => 4, //ARIZONA
135-
'CA' => 6, //CALIFORNIA
136-
'CO' => 8, //COLORADO
137-
'CT' => 9, //CONNECTICUT
138-
'DC' => 11, //DISTRICT OF COLUMBIA
139-
'DE' => 10, //DELAWARE
140-
'FL' => 12, //FLORIDA
141-
'GA' => 13, //GEORGIA
142-
'GU' => 66, //GUAM
143-
'HI' => 15, //HAWAII
144-
'IA' => 19, //IOWA
145-
'ID' => 16, //IDAHO
146-
'IL' => 17, //ILLINOIS
147-
'IN' => 18, //INDIANA
148-
'KS' => 20, //KANSAS
149-
'KY' => 21, //KENTUCKY
150-
'LA' => 22, //LOUISIANA
151-
'MA' => 25, //MASSACHUSETTS
152-
'MD' => 24, //MARYLAND
153-
'ME' => 23, //MAINE
154-
'MI' => 26, //MICHIGAN
155-
'MN' => 27, //MINNESOTA
156-
'MO' => 29, //MISSOURI
157-
'MS' => 28, //MISSISSIPPI
158-
'MT' => 30, //MONTANA
159-
'NC' => 37, //NORTH CAROLINA
160-
'ND' => 38, //NORTH DAKOTA
161-
'NE' => 31, //NEBRASKA
162-
'NH' => 33, //NEW HAMPSHIRE
163-
'NJ' => 34, //NEW JERSEY
164-
'NM' => 35, //NEW MEXICO
165-
'NV' => 32, //NEVADA
166-
'NY' => 36, //NEW YORK
167-
'OH' => 39, //OHIO
168-
'OK' => 40, //OKLAHOMA
169-
'OR' => 41, //OREGON
170-
'PA' => 42, //PENNSYLVANIA
171-
'PR' => 72, //PUERTO RICO
172-
'RI' => 44, //RHODE ISLAND
173-
'SC' => 45, //SOUTH CAROLINA
174-
'SD' => 46, //SOUTH DAKOTA
175-
'TN' => 47, //TENNESSEE
176-
'TX' => 48, //TEXAS
177-
'UT' => 49, //UTAH
178-
'VA' => 51, //VIRGINIA
179-
'VI' => 78, //VIRGIN ISLANDS
180-
'VT' => 50, //VERMONT
181-
'WA' => 53, //WASHINGTON
182-
'WI' => 55, //WISCONSIN
183-
'WV' => 54, //WEST VIRGINIA
184-
);
185-
}
186103
}

tests/.cached_responses/1ec836744699c92949abd980774e110560bc27b3

-2
This file was deleted.

tests/.cached_responses/3117e4255511124c8c1a097f85f1cad5ae01437f

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
s:235:"{"ip":"129.67.242.154","country_code":"GB","country_name":"United Kingdom","region_code":"ENG","region_name":"England","city":"Oxford","zip_code":"OX1","time_zone":"Europe/London","latitude":51.7158,"longitude":-1.2925,"metro_code":0}
2+
";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
s:235:"{"ip":"129.67.242.154","country_code":"GB","country_name":"United Kingdom","region_code":"ENG","region_name":"England","city":"Oxford","zip_code":"OX1","time_zone":"Europe/London","latitude":51.7158,"longitude":-1.2925,"metro_code":0}
2+
";

tests/Geocoder/Tests/Provider/FreeGeoIpTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function testGeocodeWithUSIPv4()
154154
$this->assertCount(1, $results);
155155

156156
$this->assertCount(1, $results->first()->getAdminLevels());
157-
$this->assertEquals('48', $results->first()->getAdminLevels()->get(1)->getCode());
157+
$this->assertEquals('TX', $results->first()->getAdminLevels()->get(1)->getCode());
158158
}
159159

160160
public function testGeocodeWithUSIPv6()
@@ -166,23 +166,26 @@ public function testGeocodeWithUSIPv6()
166166
$this->assertCount(1, $results);
167167

168168
$this->assertCount(1, $results->first()->getAdminLevels());
169-
$this->assertEquals('48', $results->first()->getAdminLevels()->get(1)->getCode());
169+
$this->assertEquals('TX', $results->first()->getAdminLevels()->get(1)->getCode());
170170
}
171171

172172
public function testGeocodeWithUKIPv4()
173173
{
174174
$provider = new FreeGeoIp($this->getAdapter());
175-
$results = $provider->geocode('132.185.255.60');
175+
$results = $provider->geocode('129.67.242.154');
176176

177177
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
178178
$this->assertCount(1, $results);
179179
$this->assertEquals('GB', $results->first()->getCountry()->getCode());
180+
181+
$this->assertCount(1, $results->first()->getAdminLevels());
182+
$this->assertEquals('ENG', $results->first()->getAdminLevels()->get(1)->getCode());
180183
}
181184

182185
public function testGeocodeWithUKIPv6()
183186
{
184187
$provider = new FreeGeoIp($this->getAdapter());
185-
$results = $provider->geocode('::ffff:132.185.255.60');
188+
$results = $provider->geocode('::ffff:129.67.242.154');
186189

187190
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
188191
$this->assertCount(1, $results);

0 commit comments

Comments
 (0)