Skip to content

Commit

Permalink
[IpStack] Provide Administrative area
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Feb 14, 2025
1 parent b722a3c commit 36a36e8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/Provider/Ipstack/Ipstack.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ 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 +115,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';
}
}
6 changes: 6 additions & 0 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 @@ -96,6 +98,8 @@ public function testGeocodeWithRealIPv4(): void
$this->assertEqualsWithDelta(-97.822, $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 @@ -113,6 +117,8 @@ public function testGeocodeWithRealIPv4InFrench(): void
$this->assertEqualsWithDelta(-97.822, $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

0 comments on commit 36a36e8

Please sign in to comment.