Skip to content
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
67 changes: 41 additions & 26 deletions src/Provider/GoogleMaps/GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
use Geocoder\Exception\InvalidServerResponse;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\AddressCollection;
use Geocoder\Model\AddressBuilder;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Model\AddressBuilder;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Http\Client\HttpClient;

/**
Expand Down Expand Up @@ -79,8 +79,14 @@ final class GoogleMaps extends AbstractHttpProvider implements Provider
*
* @return GoogleMaps
*/
public static function business(HttpClient $client, string $clientId, string $privateKey = null, string $region = null, string $apiKey = null, string $channel = null)
{
public static function business(
HttpClient $client,
string $clientId,
string $privateKey = null,
string $region = null,
string $apiKey = null,
string $channel = null
) {
$provider = new self($client, $region, $apiKey);
$provider->clientId = $clientId;
$provider->privateKey = $privateKey;
Expand Down Expand Up @@ -112,7 +118,13 @@ public function geocodeQuery(GeocodeQuery $query): Collection

$url = sprintf(self::GEOCODE_ENDPOINT_URL_SSL, rawurlencode($query->getText()));
if (null !== $bounds = $query->getBounds()) {
$url .= sprintf('&bounds=%s,%s|%s,%s', $bounds->getSouth(), $bounds->getWest(), $bounds->getNorth(), $bounds->getEast());
$url .= sprintf(
'&bounds=%s,%s|%s,%s',
$bounds->getSouth(),
$bounds->getWest(),
$bounds->getNorth(),
$bounds->getEast()
);
}

if (null !== $components = $query->getData('components')) {
Expand Down Expand Up @@ -233,22 +245,24 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
if (isset($result->formatted_address)) {
$address = $address->withFormattedAddress($result->formatted_address);
}
$address = $address->withStreetAddress($builder->getValue('street_address'));
$address = $address->withIntersection($builder->getValue('intersection'));
$address = $address->withPolitical($builder->getValue('political'));
$address = $address->withColloquialArea($builder->getValue('colloquial_area'));
$address = $address->withWard($builder->getValue('ward'));
$address = $address->withNeighborhood($builder->getValue('neighborhood'));
$address = $address->withPremise($builder->getValue('premise'));
$address = $address->withSubpremise($builder->getValue('subpremise'));
$address = $address->withNaturalFeature($builder->getValue('natural_feature'));
$address = $address->withAirport($builder->getValue('airport'));
$address = $address->withPark($builder->getValue('park'));
$address = $address->withPointOfInterest($builder->getValue('point_of_interest'));
$address = $address->withEstablishment($builder->getValue('establishment'));
$address = $address->withSubLocalityLevels($builder->getValue('subLocalityLevel', []));
$address = $address->withPartialMatch($result->partial_match ?? false);
$results[] = $address;

$results[] = $address
->withStreetAddress($builder->getValue('street_address'))
->withIntersection($builder->getValue('intersection'))
->withPolitical($builder->getValue('political'))
->withColloquialArea($builder->getValue('colloquial_area'))
->withWard($builder->getValue('ward'))
->withNeighborhood($builder->getValue('neighborhood'))
->withPremise($builder->getValue('premise'))
->withSubpremise($builder->getValue('subpremise'))
->withNaturalFeature($builder->getValue('natural_feature'))
->withAirport($builder->getValue('airport'))
->withPark($builder->getValue('park'))
->withPointOfInterest($builder->getValue('point_of_interest'))
->withEstablishment($builder->getValue('establishment'))
->withSubLocalityLevels($builder->getValue('subLocalityLevel', []))
->withPostalCodeSuffix($builder->getValue('postal_code_suffix'))
->withPartialMatch($result->partial_match ?? false);

if (count($results) >= $limit) {
break;
Expand Down Expand Up @@ -337,6 +351,7 @@ private function updateAddressComponent(AddressBuilder $builder, string $type, $
case 'park':
case 'point_of_interest':
case 'establishment':
case 'postal_code_suffix':
$builder->setValue($type, $values->long_name);

break;
Expand Down Expand Up @@ -392,7 +407,7 @@ private function serializeComponents(array $components): string
* @param string $url
* @param string $content
*
* @return mixed result form json_decode()
* @return \Stdclass result form json_decode()
*
* @throws InvalidCredentials
* @throws InvalidServerResponse
Expand Down Expand Up @@ -431,10 +446,10 @@ private function validateResponse(string $url, $content)
}

/**
* Parse coordinats and bounds.
* Parse coordinates and bounds.
*
* @param AddressBuilder $builder
* @param $result
* @param \Stdclass $result
*/
private function parseCoordinates(AddressBuilder $builder, $result)
{
Expand Down
26 changes: 26 additions & 0 deletions src/Provider/GoogleMaps/Model/GoogleAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ final class GoogleAddress extends Address
*/
private $intersection;

/**
* @var string|null
*/
private $postalCodeSuffix;

/**
* @var string|null
*/
Expand Down Expand Up @@ -265,6 +270,27 @@ public function withIntersection(string $intersection = null)
return $new;
}

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

/**
* @param string|null $postalCodeSuffix
*
* @return GoogleAddress
*/
public function withPostalCodeSuffix(string $postalCodeSuffix = null)
{
$new = clone $this;
$new->postalCodeSuffix = $postalCodeSuffix;

return $new;
}

/**
* @return null|string
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
s:2474:"{
"results" : [
{
"address_components" : [
{
"long_name" : "900",
"short_name" : "900",
"types" : [ "street_number" ]
},
{
"long_name" : "South Oak Park Avenue",
"short_name" : "S Oak Park Ave",
"types" : [ "route" ]
},
{
"long_name" : "Oak Park",
"short_name" : "Oak Park",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Oak Park Township",
"short_name" : "Oak Park Township",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Cook County",
"short_name" : "Cook County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Illinois",
"short_name" : "IL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "60304",
"short_name" : "60304",
"types" : [ "postal_code" ]
},
{
"long_name" : "1936",
"short_name" : "1936",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "900 S Oak Park Ave, Oak Park, IL 60304, USA",
"geometry" : {
"location" : {
"lat" : 41.8718539,
"lng" : -87.79375659999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 41.87320288029149,
"lng" : -87.7924076197085
},
"southwest" : {
"lat" : 41.87050491970849,
"lng" : -87.79510558029151
}
}
},
"place_id" : "ChIJK6OxB5M0DogRla1PTqvu7gk",
"plus_code" : {
"compound_code" : "V6C4+PF Oak Park, Oak Park Township, IL, United States",
"global_code" : "86HJV6C4+PF"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
";
17 changes: 17 additions & 0 deletions src/Provider/GoogleMaps/Tests/GoogleMapsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function testGeocodeWithRealAddress()

// not provided
$this->assertNull($result->getTimezone());
$this->assertNull($result->getPostalCodeSuffix());
}

public function testGeocodeBoundsWithRealAddressForNonRooftopLocation()
Expand Down Expand Up @@ -537,6 +538,22 @@ public function testReverseWithSubLocalityLevels()
$this->assertEquals(false, $result->isPartialMatch());
}

public function testGeocodeWithPostalCodeSuffixComponent()
{
$provider = $this->getGoogleMapsProvider();
$results = $provider->geocodeQuery(GeocodeQuery::create('900 S Oak Park Ave, Oak Park, IL 60304'));

$this->assertInstanceOf(AddressCollection::class, $results);
$this->assertCount(1, $results);

/** @var GoogleAddress $result */
$result = $results->first();
$this->assertInstanceOf(Address::class, $result);
$this->assertEquals('900 S Oak Park Ave, Oak Park, IL 60304, USA', $result->getFormattedAddress());
$this->assertEquals('1936', $result->getPostalCodeSuffix());
$this->assertEquals(false, $result->isPartialMatch());
}

public function testGeocodeBoundsWithRealAddressWithViewportOnly()
{
$provider = $this->getGoogleMapsProvider();
Expand Down