Skip to content

Commit

Permalink
Add place_id to GoogleAddress (#750)
Browse files Browse the repository at this point in the history
* implement `id` property

* add some ID assertions to tests
  • Loading branch information
arubacao authored and Nyholm committed Jul 25, 2017
1 parent 0d6d337 commit 5d031ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
28 changes: 28 additions & 0 deletions Model/GoogleAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*/
final class GoogleAddress extends Address
{
/**
* @var string|null
*/
private $id;

/**
* @var string|null
*/
Expand Down Expand Up @@ -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
*
Expand Down
3 changes: 3 additions & 0 deletions Tests/GoogleMapsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5d031ba

Please sign in to comment.