Skip to content

Commit

Permalink
Merge branch 'geocoder-php:master' into add-admin-levels-in-ip-stack
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz authored Feb 16, 2025
2 parents e6565dd + eb44f3f commit 8d08833
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
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 8d08833

Please sign in to comment.