diff --git a/src/Provider/GoogleMaps/GoogleMaps.php b/src/Provider/GoogleMaps/GoogleMaps.php index 2a7983f9c..e4b404e56 100644 --- a/src/Provider/GoogleMaps/GoogleMaps.php +++ b/src/Provider/GoogleMaps/GoogleMaps.php @@ -193,6 +193,11 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string $builder = new AddressBuilder($this->getName()); $this->parseCoordinates($builder, $result); + // set official Google place id + if (isset($result->place_id)) { + $builder->setValue('id', $result->place_id); + } + // update address components foreach ($result->address_components as $component) { foreach ($component->types as $type) { @@ -202,6 +207,7 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string /** @var GoogleAddress $address */ $address = $builder->build(GoogleAddress::class); + $address = $address->withId($builder->getValue('id')); if (isset($result->geometry->location_type)) { $address = $address->withLocationType($result->geometry->location_type); } diff --git a/src/Provider/GoogleMaps/Model/GoogleAddress.php b/src/Provider/GoogleMaps/Model/GoogleAddress.php index 2fff86be6..64ae30a00 100644 --- a/src/Provider/GoogleMaps/Model/GoogleAddress.php +++ b/src/Provider/GoogleMaps/Model/GoogleAddress.php @@ -19,6 +19,11 @@ */ final class GoogleAddress extends Address { + /** + * @var string|null + */ + private $id; + /** * @var string|null */ @@ -99,6 +104,29 @@ final class GoogleAddress extends Address */ private $establishment; + /** + * @param null|string $id + * + * @return GoogleAddress + */ + public function withId(string $id = null) + { + $new = clone $this; + $new->id = $id; + + return $new; + } + + /** + * @see https://developers.google.com/places/place-id + * + * @return null|string + */ + public function getId() + { + return $this->id; + } + /** * @param null|string $locationType * diff --git a/src/Provider/GoogleMaps/Tests/GoogleMapsTest.php b/src/Provider/GoogleMaps/Tests/GoogleMapsTest.php index 4cf71d65d..9c7dc74c1 100644 --- a/src/Provider/GoogleMaps/Tests/GoogleMapsTest.php +++ b/src/Provider/GoogleMaps/Tests/GoogleMapsTest.php @@ -108,6 +108,7 @@ public function testGeocodeWithRealAddress() $this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('France', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); + $this->assertEquals('ChIJ4b303vJt5kcRF9AQdh4ZjWc', $result->getId()); // not provided $this->assertNull($result->getTimezone()); @@ -129,6 +130,7 @@ public function testGeocodeBoundsWithRealAddressForNonRooftopLocation() $this->assertEquals(2.224199, $result->getBounds()->getWest(), '', 0.0001); $this->assertEquals(48.902145, $result->getBounds()->getNorth(), '', 0.0001); $this->assertEquals(2.4699209, $result->getBounds()->getEast(), '', 0.0001); + $this->assertEquals('ChIJD7fiBh9u5kcRYJSMaMOCCwQ', $result->getId()); } /** @@ -160,6 +162,7 @@ public function testReverseWithRealCoordinates() $this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('France', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); + $this->assertEquals('ChIJ9aLL3vJt5kcR61GCze3v6fg', $result->getId()); } public function testGeocodeWithCityDistrict()