Skip to content

Commit 45f65e3

Browse files
atymicjbelien
authored andcommitted
fix: add postal_code_suffix to google maps provider (#1008)
1 parent 2b9b360 commit 45f65e3

File tree

4 files changed

+163
-26
lines changed

4 files changed

+163
-26
lines changed

src/Provider/GoogleMaps/GoogleMaps.php

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
use Geocoder\Exception\InvalidServerResponse;
1818
use Geocoder\Exception\QuotaExceeded;
1919
use Geocoder\Exception\UnsupportedOperation;
20-
use Geocoder\Model\AddressCollection;
21-
use Geocoder\Model\AddressBuilder;
22-
use Geocoder\Query\GeocodeQuery;
23-
use Geocoder\Query\ReverseQuery;
2420
use Geocoder\Http\Provider\AbstractHttpProvider;
21+
use Geocoder\Model\AddressBuilder;
22+
use Geocoder\Model\AddressCollection;
2523
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress;
2624
use Geocoder\Provider\Provider;
25+
use Geocoder\Query\GeocodeQuery;
26+
use Geocoder\Query\ReverseQuery;
2727
use Http\Client\HttpClient;
2828

2929
/**
@@ -79,8 +79,14 @@ final class GoogleMaps extends AbstractHttpProvider implements Provider
7979
*
8080
* @return GoogleMaps
8181
*/
82-
public static function business(HttpClient $client, string $clientId, string $privateKey = null, string $region = null, string $apiKey = null, string $channel = null)
83-
{
82+
public static function business(
83+
HttpClient $client,
84+
string $clientId,
85+
string $privateKey = null,
86+
string $region = null,
87+
string $apiKey = null,
88+
string $channel = null
89+
) {
8490
$provider = new self($client, $region, $apiKey);
8591
$provider->clientId = $clientId;
8692
$provider->privateKey = $privateKey;
@@ -112,7 +118,13 @@ public function geocodeQuery(GeocodeQuery $query): Collection
112118

113119
$url = sprintf(self::GEOCODE_ENDPOINT_URL_SSL, rawurlencode($query->getText()));
114120
if (null !== $bounds = $query->getBounds()) {
115-
$url .= sprintf('&bounds=%s,%s|%s,%s', $bounds->getSouth(), $bounds->getWest(), $bounds->getNorth(), $bounds->getEast());
121+
$url .= sprintf(
122+
'&bounds=%s,%s|%s,%s',
123+
$bounds->getSouth(),
124+
$bounds->getWest(),
125+
$bounds->getNorth(),
126+
$bounds->getEast()
127+
);
116128
}
117129

118130
if (null !== $components = $query->getData('components')) {
@@ -233,22 +245,24 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
233245
if (isset($result->formatted_address)) {
234246
$address = $address->withFormattedAddress($result->formatted_address);
235247
}
236-
$address = $address->withStreetAddress($builder->getValue('street_address'));
237-
$address = $address->withIntersection($builder->getValue('intersection'));
238-
$address = $address->withPolitical($builder->getValue('political'));
239-
$address = $address->withColloquialArea($builder->getValue('colloquial_area'));
240-
$address = $address->withWard($builder->getValue('ward'));
241-
$address = $address->withNeighborhood($builder->getValue('neighborhood'));
242-
$address = $address->withPremise($builder->getValue('premise'));
243-
$address = $address->withSubpremise($builder->getValue('subpremise'));
244-
$address = $address->withNaturalFeature($builder->getValue('natural_feature'));
245-
$address = $address->withAirport($builder->getValue('airport'));
246-
$address = $address->withPark($builder->getValue('park'));
247-
$address = $address->withPointOfInterest($builder->getValue('point_of_interest'));
248-
$address = $address->withEstablishment($builder->getValue('establishment'));
249-
$address = $address->withSubLocalityLevels($builder->getValue('subLocalityLevel', []));
250-
$address = $address->withPartialMatch($result->partial_match ?? false);
251-
$results[] = $address;
248+
249+
$results[] = $address
250+
->withStreetAddress($builder->getValue('street_address'))
251+
->withIntersection($builder->getValue('intersection'))
252+
->withPolitical($builder->getValue('political'))
253+
->withColloquialArea($builder->getValue('colloquial_area'))
254+
->withWard($builder->getValue('ward'))
255+
->withNeighborhood($builder->getValue('neighborhood'))
256+
->withPremise($builder->getValue('premise'))
257+
->withSubpremise($builder->getValue('subpremise'))
258+
->withNaturalFeature($builder->getValue('natural_feature'))
259+
->withAirport($builder->getValue('airport'))
260+
->withPark($builder->getValue('park'))
261+
->withPointOfInterest($builder->getValue('point_of_interest'))
262+
->withEstablishment($builder->getValue('establishment'))
263+
->withSubLocalityLevels($builder->getValue('subLocalityLevel', []))
264+
->withPostalCodeSuffix($builder->getValue('postal_code_suffix'))
265+
->withPartialMatch($result->partial_match ?? false);
252266

253267
if (count($results) >= $limit) {
254268
break;
@@ -337,6 +351,7 @@ private function updateAddressComponent(AddressBuilder $builder, string $type, $
337351
case 'park':
338352
case 'point_of_interest':
339353
case 'establishment':
354+
case 'postal_code_suffix':
340355
$builder->setValue($type, $values->long_name);
341356

342357
break;
@@ -392,7 +407,7 @@ private function serializeComponents(array $components): string
392407
* @param string $url
393408
* @param string $content
394409
*
395-
* @return mixed result form json_decode()
410+
* @return \Stdclass result form json_decode()
396411
*
397412
* @throws InvalidCredentials
398413
* @throws InvalidServerResponse
@@ -431,10 +446,10 @@ private function validateResponse(string $url, $content)
431446
}
432447

433448
/**
434-
* Parse coordinats and bounds.
449+
* Parse coordinates and bounds.
435450
*
436451
* @param AddressBuilder $builder
437-
* @param $result
452+
* @param \Stdclass $result
438453
*/
439454
private function parseCoordinates(AddressBuilder $builder, $result)
440455
{

src/Provider/GoogleMaps/Model/GoogleAddress.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ final class GoogleAddress extends Address
5151
*/
5252
private $intersection;
5353

54+
/**
55+
* @var string|null
56+
*/
57+
private $postalCodeSuffix;
58+
5459
/**
5560
* @var string|null
5661
*/
@@ -265,6 +270,27 @@ public function withIntersection(string $intersection = null)
265270
return $new;
266271
}
267272

273+
/**
274+
* @return null|string
275+
*/
276+
public function getPostalCodeSuffix()
277+
{
278+
return $this->postalCodeSuffix;
279+
}
280+
281+
/**
282+
* @param string|null $postalCodeSuffix
283+
*
284+
* @return GoogleAddress
285+
*/
286+
public function withPostalCodeSuffix(string $postalCodeSuffix = null)
287+
{
288+
$new = clone $this;
289+
$new->postalCodeSuffix = $postalCodeSuffix;
290+
291+
return $new;
292+
}
293+
268294
/**
269295
* @return null|string
270296
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
s:2474:"{
2+
"results" : [
3+
{
4+
"address_components" : [
5+
{
6+
"long_name" : "900",
7+
"short_name" : "900",
8+
"types" : [ "street_number" ]
9+
},
10+
{
11+
"long_name" : "South Oak Park Avenue",
12+
"short_name" : "S Oak Park Ave",
13+
"types" : [ "route" ]
14+
},
15+
{
16+
"long_name" : "Oak Park",
17+
"short_name" : "Oak Park",
18+
"types" : [ "locality", "political" ]
19+
},
20+
{
21+
"long_name" : "Oak Park Township",
22+
"short_name" : "Oak Park Township",
23+
"types" : [ "administrative_area_level_3", "political" ]
24+
},
25+
{
26+
"long_name" : "Cook County",
27+
"short_name" : "Cook County",
28+
"types" : [ "administrative_area_level_2", "political" ]
29+
},
30+
{
31+
"long_name" : "Illinois",
32+
"short_name" : "IL",
33+
"types" : [ "administrative_area_level_1", "political" ]
34+
},
35+
{
36+
"long_name" : "United States",
37+
"short_name" : "US",
38+
"types" : [ "country", "political" ]
39+
},
40+
{
41+
"long_name" : "60304",
42+
"short_name" : "60304",
43+
"types" : [ "postal_code" ]
44+
},
45+
{
46+
"long_name" : "1936",
47+
"short_name" : "1936",
48+
"types" : [ "postal_code_suffix" ]
49+
}
50+
],
51+
"formatted_address" : "900 S Oak Park Ave, Oak Park, IL 60304, USA",
52+
"geometry" : {
53+
"location" : {
54+
"lat" : 41.8718539,
55+
"lng" : -87.79375659999999
56+
},
57+
"location_type" : "ROOFTOP",
58+
"viewport" : {
59+
"northeast" : {
60+
"lat" : 41.87320288029149,
61+
"lng" : -87.7924076197085
62+
},
63+
"southwest" : {
64+
"lat" : 41.87050491970849,
65+
"lng" : -87.79510558029151
66+
}
67+
}
68+
},
69+
"place_id" : "ChIJK6OxB5M0DogRla1PTqvu7gk",
70+
"plus_code" : {
71+
"compound_code" : "V6C4+PF Oak Park, Oak Park Township, IL, United States",
72+
"global_code" : "86HJV6C4+PF"
73+
},
74+
"types" : [ "street_address" ]
75+
}
76+
],
77+
"status" : "OK"
78+
}
79+
";

src/Provider/GoogleMaps/Tests/GoogleMapsTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function testGeocodeWithRealAddress()
120120

121121
// not provided
122122
$this->assertNull($result->getTimezone());
123+
$this->assertNull($result->getPostalCodeSuffix());
123124
}
124125

125126
public function testGeocodeBoundsWithRealAddressForNonRooftopLocation()
@@ -537,6 +538,22 @@ public function testReverseWithSubLocalityLevels()
537538
$this->assertEquals(false, $result->isPartialMatch());
538539
}
539540

541+
public function testGeocodeWithPostalCodeSuffixComponent()
542+
{
543+
$provider = $this->getGoogleMapsProvider();
544+
$results = $provider->geocodeQuery(GeocodeQuery::create('900 S Oak Park Ave, Oak Park, IL 60304'));
545+
546+
$this->assertInstanceOf(AddressCollection::class, $results);
547+
$this->assertCount(1, $results);
548+
549+
/** @var GoogleAddress $result */
550+
$result = $results->first();
551+
$this->assertInstanceOf(Address::class, $result);
552+
$this->assertEquals('900 S Oak Park Ave, Oak Park, IL 60304, USA', $result->getFormattedAddress());
553+
$this->assertEquals('1936', $result->getPostalCodeSuffix());
554+
$this->assertEquals(false, $result->isPartialMatch());
555+
}
556+
540557
public function testGeocodeBoundsWithRealAddressWithViewportOnly()
541558
{
542559
$provider = $this->getGoogleMapsProvider();

0 commit comments

Comments
 (0)