Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5bbf3f7

Browse files
arubacaoNyholm
authored andcommitted
Add place_id to GoogleAddress (#750)
* implement `id` property * add some ID assertions to tests
1 parent 780d2b3 commit 5bbf3f7

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Provider/GoogleMaps/GoogleMaps.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
193193
$builder = new AddressBuilder($this->getName());
194194
$this->parseCoordinates($builder, $result);
195195

196+
// set official Google place id
197+
if (isset($result->place_id)) {
198+
$builder->setValue('id', $result->place_id);
199+
}
200+
196201
// update address components
197202
foreach ($result->address_components as $component) {
198203
foreach ($component->types as $type) {
@@ -202,6 +207,7 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
202207

203208
/** @var GoogleAddress $address */
204209
$address = $builder->build(GoogleAddress::class);
210+
$address = $address->withId($builder->getValue('id'));
205211
if (isset($result->geometry->location_type)) {
206212
$address = $address->withLocationType($result->geometry->location_type);
207213
}

src/Provider/GoogleMaps/Model/GoogleAddress.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
*/
2020
final class GoogleAddress extends Address
2121
{
22+
/**
23+
* @var string|null
24+
*/
25+
private $id;
26+
2227
/**
2328
* @var string|null
2429
*/
@@ -99,6 +104,29 @@ final class GoogleAddress extends Address
99104
*/
100105
private $establishment;
101106

107+
/**
108+
* @param null|string $id
109+
*
110+
* @return GoogleAddress
111+
*/
112+
public function withId(string $id = null)
113+
{
114+
$new = clone $this;
115+
$new->id = $id;
116+
117+
return $new;
118+
}
119+
120+
/**
121+
* @see https://developers.google.com/places/place-id
122+
*
123+
* @return null|string
124+
*/
125+
public function getId()
126+
{
127+
return $this->id;
128+
}
129+
102130
/**
103131
* @param null|string $locationType
104132
*

src/Provider/GoogleMaps/Tests/GoogleMapsTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function testGeocodeWithRealAddress()
108108
$this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName());
109109
$this->assertEquals('France', $result->getCountry()->getName());
110110
$this->assertEquals('FR', $result->getCountry()->getCode());
111+
$this->assertEquals('ChIJ4b303vJt5kcRF9AQdh4ZjWc', $result->getId());
111112

112113
// not provided
113114
$this->assertNull($result->getTimezone());
@@ -129,6 +130,7 @@ public function testGeocodeBoundsWithRealAddressForNonRooftopLocation()
129130
$this->assertEquals(2.224199, $result->getBounds()->getWest(), '', 0.0001);
130131
$this->assertEquals(48.902145, $result->getBounds()->getNorth(), '', 0.0001);
131132
$this->assertEquals(2.4699209, $result->getBounds()->getEast(), '', 0.0001);
133+
$this->assertEquals('ChIJD7fiBh9u5kcRYJSMaMOCCwQ', $result->getId());
132134
}
133135

134136
/**
@@ -160,6 +162,7 @@ public function testReverseWithRealCoordinates()
160162
$this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName());
161163
$this->assertEquals('France', $result->getCountry()->getName());
162164
$this->assertEquals('FR', $result->getCountry()->getCode());
165+
$this->assertEquals('ChIJ9aLL3vJt5kcR61GCze3v6fg', $result->getId());
163166
}
164167

165168
public function testGeocodeWithCityDistrict()

0 commit comments

Comments
 (0)