Skip to content

Commit b3e9649

Browse files
committed
Merge pull request #283 from kaiwa/patch-1
Updating GeoIPs provider to expect a single location response
2 parents d50f506 + 0be895c commit b3e9649

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/Geocoder/Provider/GeoIPsProvider.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,23 @@ protected function executeQuery($query)
157157
}
158158

159159
// Make sure that we do have proper result array
160-
if (empty($response['locations']) || !is_array($response['locations']) || empty($response['locations'][0])) {
160+
if (empty($response['location']) || !is_array($response['location'])) {
161161
throw new NoResultException(sprintf('Invalid response from GeoIPs server for query %s', $query));
162162
}
163163

164-
// Pick the first location from the response
165164
$locations = array();
166-
foreach ($response['locations'] as $location) {
167-
$locations[] = array_merge($this->getDefaults(), array(
168-
'country' => '' === $location['country_name'] ? null : $location['country_name'],
169-
'countryCode' => '' === $location['country_code'] ? null : $location['country_code'],
170-
'region' => '' === $location['region_name'] ? null : $location['region_name'],
171-
'regionCode' => '' === $location['region_code'] ? null : $location['region_code'],
172-
'county' => '' === $location['county_name'] ? null : $location['county_name'],
173-
'city' => '' === $location['city_name'] ? null : $location['city_name'],
174-
'latitude' => '' === $location['latitude'] ? null : $location['latitude'],
175-
'longitude' => '' === $location['longitude'] ? null : $location['longitude'],
176-
'timezone' => '' === $location['timezone'] ? null : $location['timezone'],
177-
));
178-
}
165+
$location = $response['location'];
166+
$locations[] = array_merge($this->getDefaults(), array(
167+
'country' => '' === $location['country_name'] ? null : $location['country_name'],
168+
'countryCode' => '' === $location['country_code'] ? null : $location['country_code'],
169+
'region' => '' === $location['region_name'] ? null : $location['region_name'],
170+
'regionCode' => '' === $location['region_code'] ? null : $location['region_code'],
171+
'county' => '' === $location['county_name'] ? null : $location['county_name'],
172+
'city' => '' === $location['city_name'] ? null : $location['city_name'],
173+
'latitude' => '' === $location['latitude'] ? null : $location['latitude'],
174+
'longitude' => '' === $location['longitude'] ? null : $location['longitude'],
175+
'timezone' => '' === $location['timezone'] ? null : $location['timezone'],
176+
));
179177

180178
return $locations;
181179
}

tests/Geocoder/Tests/Provider/GeoIPsProviderTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContentFormattedEmpty()
110110
"message": "Success",
111111
"notes": "The following results has been returned",
112112
"code": "200_1",
113-
"locations": [{
113+
"location": {
114114
"ip" : "66.147.244.214",
115115
"owner" : "",
116116
"continent_name" : "",
@@ -124,7 +124,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContentFormattedEmpty()
124124
"latitude" : "",
125125
"longitude" : "",
126126
"timezone" : ""
127-
}],
127+
},
128128
"unit_test": {
129129
"elapsed_time": "0.0676",
130130
"memory_usage": "2.2MB"
@@ -156,7 +156,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContent()
156156
"message": "Success",
157157
"notes": "The following results has been returned",
158158
"code": "200_1",
159-
"locations": [{
159+
"location": {
160160
"ip" : "66.147.244.214",
161161
"owner" : "BLUEHOST INC.",
162162
"continent_name" : "NORTH AMERICA",
@@ -170,7 +170,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContent()
170170
"latitude" : "40.3402",
171171
"longitude" : "-111.6073",
172172
"timezone" : "MST"
173-
}]
173+
}
174174
}}';
175175

176176
$provider = new GeoIPsProvider($this->getMockAdapterReturns($json), 'api_key');

0 commit comments

Comments
 (0)