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

Skip to content

Add place_id to GoogleAddress #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Provider/GoogleMaps/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 src/Provider/GoogleMaps/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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment explaining that this is the "place 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
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. This is great.

* @return null|string
*/
public function getId()
{
return $this->id;
}

/**
* @param null|string $locationType
*
Expand Down
3 changes: 3 additions & 0 deletions src/Provider/GoogleMaps/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