Skip to content

Commit

Permalink
Merge branch 'master' into azure-municipality
Browse files Browse the repository at this point in the history
  • Loading branch information
kryber authored Feb 18, 2025
2 parents 491b29b + 47e33bd commit 4e62d3e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 27 deletions.
11 changes: 11 additions & 0 deletions src/Provider/Ipstack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 0.5.0

### Added

- Add support for PHP 8.3 and 8.4
- Add support for region name (as admin level)

### Removed

- Drop support for PHP 7.4

## 0.4.0

### Added
Expand Down
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
7 changes: 7 additions & 0 deletions src/Provider/Photon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 0.8.0

### Added

- Add support for `layer` and `radius` parameters
- Improve locality extraction

## 0.7.0

### Added
Expand Down
28 changes: 28 additions & 0 deletions src/Provider/Photon/Model/PhotonAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ final class PhotonAddress extends Address
*/
private $district;

/**
* @var string|null
*/
private $type;

public function getLocality(): ?string
{
$locality = parent::getLocality();
if (null === $locality && 'city' === $this->type && null !== $this->name) {
$locality = $this->name;
}

return $locality;
}

/**
* @return string|null
*/
Expand Down Expand Up @@ -173,4 +188,17 @@ public function withDistrict(?string $district = null): self

return $new;
}

public function getType(): ?string
{
return $this->type;
}

public function withType(?string $type = null): self
{
$new = clone $this;
$new->type = $type;

return $new;
}
}
6 changes: 5 additions & 1 deletion src/Provider/Photon/Photon.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function geocodeQuery(GeocodeQuery $query): Collection
.'/api?'
.http_build_query([
'q' => $address,
'layer' => $query->getData('layer'),
'limit' => $query->getLimit(),
'lang' => $query->getLocale(),
]);
Expand Down Expand Up @@ -102,6 +103,8 @@ public function reverseQuery(ReverseQuery $query): Collection
.http_build_query([
'lat' => $latitude,
'lon' => $longitude,
'layer' => $query->getData('layer'),
'radius' => $query->getData('radius'),
'limit' => $query->getLimit(),
'lang' => $query->getLocale(),
]);
Expand Down Expand Up @@ -157,7 +160,8 @@ private function featureToAddress(\stdClass $feature): Location
->withName($properties->name ?? null)
->withState($properties->state ?? null)
->withCounty($properties->county ?? null)
->withDistrict($properties->district ?? null);
->withDistrict($properties->district ?? null)
->withType($properties->type ?? null);

return $address;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:334:"{"features":[{"geometry":{"coordinates":[13.3989367,52.510885],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":62422,"extent":[13.088345,52.6755087,13.7611609,52.3382448],"country":"Deutschland","osm_key":"place","countrycode":"DE","osm_value":"city","name":"Berlin","type":"city"}}],"type":"FeatureCollection"}";
14 changes: 14 additions & 0 deletions src/Provider/Photon/Tests/PhotonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,18 @@ public function testReverseQueryWithOsmTagFilter(): void
$this->assertEquals('pharmacy', $result->getOSMTag()->value);
}
}

public function testReverseQueryWithLayerCityAndRadiusFilter(): void
{
$provider = Photon::withKomootServer($this->getHttpClient());
$reverseQuery = ReverseQuery::fromCoordinates(52.51644, 13.38890)
->withData('layer', 'city')
->withData('radius', 20)
->withLimit(1);
$result = $provider->reverseQuery($reverseQuery)->first();

$this->assertInstanceOf(PhotonAddress::class, $result);
$this->assertEquals('city', $result->getType());
$this->assertEquals('Berlin', $result->getLocality());
}
}

0 comments on commit 4e62d3e

Please sign in to comment.