Skip to content

Commit 4a32622

Browse files
authored
[Photon] Improve locality for city layer & Add layer & radius query params (#1245)
* Improve locality for city layer & Add layer & radius query params * CS fix
1 parent b722a3c commit 4a32622

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

src/Provider/Photon/Model/PhotonAddress.php

+28
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ final class PhotonAddress extends Address
5454
*/
5555
private $district;
5656

57+
/**
58+
* @var string|null
59+
*/
60+
private $type;
61+
62+
public function getLocality(): ?string
63+
{
64+
$locality = parent::getLocality();
65+
if (null === $locality && 'city' === $this->type && null !== $this->name) {
66+
$locality = $this->name;
67+
}
68+
69+
return $locality;
70+
}
71+
5772
/**
5873
* @return string|null
5974
*/
@@ -173,4 +188,17 @@ public function withDistrict(?string $district = null): self
173188

174189
return $new;
175190
}
191+
192+
public function getType(): ?string
193+
{
194+
return $this->type;
195+
}
196+
197+
public function withType(?string $type = null): self
198+
{
199+
$new = clone $this;
200+
$new->type = $type;
201+
202+
return $new;
203+
}
176204
}

src/Provider/Photon/Photon.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function geocodeQuery(GeocodeQuery $query): Collection
6868
.'/api?'
6969
.http_build_query([
7070
'q' => $address,
71+
'layer' => $query->getData('layer'),
7172
'limit' => $query->getLimit(),
7273
'lang' => $query->getLocale(),
7374
]);
@@ -102,6 +103,8 @@ public function reverseQuery(ReverseQuery $query): Collection
102103
.http_build_query([
103104
'lat' => $latitude,
104105
'lon' => $longitude,
106+
'layer' => $query->getData('layer'),
107+
'radius' => $query->getData('radius'),
105108
'limit' => $query->getLimit(),
106109
'lang' => $query->getLocale(),
107110
]);
@@ -157,7 +160,8 @@ private function featureToAddress(\stdClass $feature): Location
157160
->withName($properties->name ?? null)
158161
->withState($properties->state ?? null)
159162
->withCounty($properties->county ?? null)
160-
->withDistrict($properties->district ?? null);
163+
->withDistrict($properties->district ?? null)
164+
->withType($properties->type ?? null);
161165

162166
return $address;
163167
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}";

src/Provider/Photon/Tests/PhotonTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,18 @@ public function testReverseQueryWithOsmTagFilter(): void
178178
$this->assertEquals('pharmacy', $result->getOSMTag()->value);
179179
}
180180
}
181+
182+
public function testReverseQueryWithLayerCityAndRadiusFilter(): void
183+
{
184+
$provider = Photon::withKomootServer($this->getHttpClient());
185+
$reverseQuery = ReverseQuery::fromCoordinates(52.51644, 13.38890)
186+
->withData('layer', 'city')
187+
->withData('radius', 20)
188+
->withLimit(1);
189+
$result = $provider->reverseQuery($reverseQuery)->first();
190+
191+
$this->assertInstanceOf(PhotonAddress::class, $result);
192+
$this->assertEquals('city', $result->getType());
193+
$this->assertEquals('Berlin', $result->getLocality());
194+
}
181195
}

0 commit comments

Comments
 (0)