Skip to content

Commit

Permalink
Merge pull request #6 from Wingly-Company/gkleitz/add_place_id_parame…
Browse files Browse the repository at this point in the history
…ter_to_geocode

Adding place_id parameter to geocode endpoint
  • Loading branch information
guillaumekleitz authored May 22, 2023
2 parents 4677f96 + fb7c22b commit e128f85
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ You can streamline your search by adding the following query params to your url:
- `language`: To get the results in a specific language. Defaults to the application current locale.
- `country`: To get the results for a specific country.
- `types`: To get the results for a specific type. Available only for autocomplete requests.
- `place_id`: (geocoding only) The place id returned by the autocomplete api. Will replace user input if provided.

If you like to prevent the publishing of those routes completely, you can use the `ignoreRoutes` method provided by GooglePlaces.
Typically this method should be called in the register method of your `AppServiceProvider`:
Expand Down
10 changes: 10 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Builder
/** @var string */
public $query;

/** @var string */
public $placeId;

/** @var \Wingly\GooglePlaces\Engines\Engine */
public $engine;

Expand Down Expand Up @@ -65,6 +68,13 @@ public function setFields(string $fields): self
return $this;
}

public function setPlaceId(string $placeId): self
{
$this->placeId = $placeId;

return $this;
}

public function get(): mixed
{
$cacheKey = $this->engine->getHashedCacheKey($this);
Expand Down
7 changes: 6 additions & 1 deletion src/Engines/GeocodeEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function search(Builder $builder): mixed
{
$payload = $this->getRequestPayload($builder);

if (empty($builder->query)) {
if (empty($builder->query) && empty($builder->placeId)) {
return [];
}

Expand Down Expand Up @@ -70,6 +70,11 @@ protected function getRequestPayload(Builder $builder): array
'language' => $builder->language,
];

if ($builder->placeId) {
$parameters['place_id'] = $builder->placeId;
unset($parameters['address']);
}

if ($builder->country) {
$parameters = array_merge(
$parameters,
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/GeocodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
use Wingly\GooglePlaces\GooglePlaces;
use Wingly\GooglePlaces\Pipes\Country;
use Wingly\GooglePlaces\Pipes\Language;
use Wingly\GooglePlaces\Pipes\PlaceId;

class GeocodeController extends Controller
{
public function __invoke(Request $request): JsonResponse
{
$results = app(Pipeline::class)
->send(GooglePlaces::geocode($request->query('input') ?? ''))
->through([Language::class, Country::class])
->through([Language::class, Country::class, PlaceId::class])
->thenReturn()
->get();

Expand Down
17 changes: 17 additions & 0 deletions src/Pipes/PlaceId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Wingly\GooglePlaces\Pipes;

use Wingly\GooglePlaces\Builder;

class PlaceId
{
public function handle(Builder $builder, $next): mixed
{
if (request()->has('place_id')) {
$builder->setPlaceId(request('place_id', ''));
}

return $next($builder);
}
}
11 changes: 10 additions & 1 deletion tests/GoogleGeocodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function test_it_can_translate_the_data(): void
{
$result = GooglePlaces::geocode('Roma, Italy')->get();

$this->assertEquals('Rome, Metropolitan City of Rome, Italy', $result['formatted_address']);
$this->assertEquals('Rome, Metropolitan City of Rome Capital, Italy', $result['formatted_address']);

$result = GooglePlaces::geocode('Roma, Italy')
->setLanguage('it')
Expand Down Expand Up @@ -64,4 +64,13 @@ public function test_it_will_cache_a_request(): void

$this->assertEquals($results, $cachedResults);
}

public function test_place_id_overrides_address_when_provided()
{
$result = GooglePlaces::geocode('Faketown')
->setPlaceId('ChIJ60OJPWsTLUYRA2vzEgdY4q4')
->get();

$this->assertEquals('North Sea', $result['formatted_address']);
}
}
2 changes: 1 addition & 1 deletion tests/GooglePlaceDetailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function test_it_can_translate_the_data(): void
->setFields('formatted_address')
->get();

$this->assertEquals('Rome, Metropolitan City of Rome, Italy', $result['formatted_address']);
$this->assertEquals('Rome, Metropolitan City of Rome Capital, Italy', $result['formatted_address']);

$result = GooglePlaces::details('ChIJu46S-ZZhLxMROG5lkwZ3D7k')
->setFields('formatted_address')
Expand Down

0 comments on commit e128f85

Please sign in to comment.