Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IpStack] Provide Administrative area #1246

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions src/Provider/Ipstack/Ipstack.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public function geocodeQuery(GeocodeQuery $query): Collection
$url = sprintf('%s&language=%s', $url, $query->getLocale());
}

return $this->executeQuery($url);
}

public function reverseQuery(ReverseQuery $query): Collection
{
throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.');
}

public function getName(): string
{
return 'ipstack';
}

private function executeQuery(string $url): AddressCollection
{
$body = $this->getUrlContents($url);
$data = json_decode($body, true);

Expand All @@ -99,26 +114,17 @@ public function geocodeQuery(GeocodeQuery $query): Collection
return new AddressCollection([]);
}

$locations[] = Address::createFromArray([
'providedBy' => $this->getName(),
'latitude' => $data['latitude'] ?: null,
'longitude' => $data['longitude'] ?: null,
'locality' => $data['city'] ?: null,
'postalCode' => $data['zip'] ?: null,
'country' => $data['country_name'] ?: null,
'countryCode' => $data['country_code'] ?: null,
return new AddressCollection([
Address::createFromArray([
'providedBy' => $this->getName(),
'latitude' => $data['latitude'] ?? null,
'longitude' => $data['longitude'] ?? null,
'locality' => $data['city'] ?? null,
'postalCode' => $data['zip'] ?? null,
'country' => $data['country_name'] ?? null,
'adminLevels' => isset($data['region_name']) ? [['name' => $data['region_name'], 'level' => 1]] : [],
'countryCode' => $data['country_code'] ?? null,
]),
]);

return new AddressCollection($locations);
}

public function reverseQuery(ReverseQuery $query): Collection
{
throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.');
}

public function getName(): string
{
return 'ipstack';
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
s:562:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"Am\u00e9rique du Nord","country_code":"US","country_name":"\u00c9tats-Unis","region_code":null,"region_name":null,"city":null,"zip":null,"latitude":37.751,"longitude":-97.822,"location":{"geoname_id":null,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false}}";
s:1120:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"Am\u00e9rique du Nord","country_code":"US","country_name":"\u00c9tats-Unis","region_code":"NJ","region_name":"New Jersey","city":"Jersey City","zip":"07311","latitude":40.724,"longitude":-74.059,"msa":"35620","dma":"501","radius":null,"ip_routing_type":"fixed","connection_type":"tx","location":{"geoname_id":5099836,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"https://assets.ipstack.com/flags/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false},"time_zone":{"id":"America/New_York","current_time":"2025-02-16T06:13:52-05:00","gmt_offset":-18000,"code":"EST","is_daylight_saving":false},"currency":{"code":"USD","name":"US Dollar","plural":"US dollars","symbol":"$","symbol_native":"$"},"connection":{"asn":19994,"isp":"Rackspace Hosting","sld":null,"tld":null,"carrier":"rackspace hosting","home":false,"organization_type":"Internet Hosting Services","isic_code":"J6311","naics_code":"518210"}}";
Original file line number Diff line number Diff line change
@@ -1 +1 @@
s:552:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"North America","country_code":"US","country_name":"United States","region_code":null,"region_name":null,"city":null,"zip":null,"latitude":37.751,"longitude":-97.822,"location":{"geoname_id":null,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false}}";
s:1110:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"North America","country_code":"US","country_name":"United States","region_code":"NJ","region_name":"New Jersey","city":"Jersey City","zip":"07311","latitude":40.724,"longitude":-74.059,"msa":"35620","dma":"501","radius":null,"ip_routing_type":"fixed","connection_type":"tx","location":{"geoname_id":5099836,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"https://assets.ipstack.com/flags/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false},"time_zone":{"id":"America/New_York","current_time":"2025-02-16T06:03:06-05:00","gmt_offset":-18000,"code":"EST","is_daylight_saving":false},"currency":{"code":"USD","name":"US Dollar","plural":"US dollars","symbol":"$","symbol_native":"$"},"connection":{"asn":19994,"isp":"Rackspace Hosting","sld":null,"tld":null,"carrier":"rackspace hosting","home":false,"organization_type":"Internet Hosting Services","isic_code":"J6311","naics_code":"518210"}}";
14 changes: 10 additions & 4 deletions src/Provider/Ipstack/Tests/IpstackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testGeocodeWithLocalhostIPv4(): void
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEquals('localhost', $result->getLocality());
$this->assertEquals('localhost', $result->getCountry()->getName());
$this->assertEmpty($result->getAdminLevels());
}

public function testGeocodeWithLocalhostIPv6(): void
Expand All @@ -79,6 +80,7 @@ public function testGeocodeWithLocalhostIPv6(): void
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEquals('localhost', $result->getLocality());
$this->assertEquals('localhost', $result->getCountry()->getName());
$this->assertEmpty($result->getAdminLevels());
}

public function testGeocodeWithRealIPv4(): void
Expand All @@ -92,10 +94,12 @@ public function testGeocodeWithRealIPv4(): void
/** @var Location $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(37.751, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-97.822, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEqualsWithDelta(40.724, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-74.059, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('United States', $result->getCountry()->getName());
$this->assertEquals('US', $result->getCountry()->getCode());
$this->assertCount(1, $result->getAdminLevels());
$this->assertEquals('New Jersey', $result->getAdminLevels()->get(1)->getName());
}

public function testGeocodeWithRealIPv4InFrench(): void
Expand All @@ -109,10 +113,12 @@ public function testGeocodeWithRealIPv4InFrench(): void
/** @var Location $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(37.751, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-97.822, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEqualsWithDelta(40.724, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-74.059, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('États-Unis', $result->getCountry()->getName());
$this->assertEquals('US', $result->getCountry()->getCode());
$this->assertCount(1, $result->getAdminLevels());
$this->assertEquals('New Jersey', $result->getAdminLevels()->get(1)->getName());
}

public function testReverse(): void
Expand Down