diff --git a/src/Provider/Ipstack/Ipstack.php b/src/Provider/Ipstack/Ipstack.php index 29a6a14e4..eff4ac72f 100644 --- a/src/Provider/Ipstack/Ipstack.php +++ b/src/Provider/Ipstack/Ipstack.php @@ -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); @@ -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'; } } diff --git a/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_8c54dbc41778b06f3d6701959abd823ece0ac790 b/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_8c54dbc41778b06f3d6701959abd823ece0ac790 index a1eacec32..59e2e663f 100644 --- a/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_8c54dbc41778b06f3d6701959abd823ece0ac790 +++ b/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_8c54dbc41778b06f3d6701959abd823ece0ac790 @@ -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}}"; \ No newline at end of file +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"}}"; \ No newline at end of file diff --git a/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_f92238d9cf3edf827c645b7d7b20046b310dbf29 b/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_f92238d9cf3edf827c645b7d7b20046b310dbf29 index 23138a6a6..07d94baf9 100644 --- a/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_f92238d9cf3edf827c645b7d7b20046b310dbf29 +++ b/src/Provider/Ipstack/Tests/.cached_responses/api.ipstack.com_f92238d9cf3edf827c645b7d7b20046b310dbf29 @@ -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}}"; \ No newline at end of file +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"}}"; \ No newline at end of file diff --git a/src/Provider/Ipstack/Tests/IpstackTest.php b/src/Provider/Ipstack/Tests/IpstackTest.php index f8a53d77d..b7247ac59 100644 --- a/src/Provider/Ipstack/Tests/IpstackTest.php +++ b/src/Provider/Ipstack/Tests/IpstackTest.php @@ -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 @@ -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 @@ -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 @@ -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