Skip to content

Commit

Permalink
Merge pull request #1 from navjobs/address-components
Browse files Browse the repository at this point in the history
added address components to google repsonse
  • Loading branch information
joshforbes authored Jul 5, 2018
2 parents 96f489b + 61fea91 commit e8c119d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/GoogleGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ private function buildResults($results)
'latitude' => $coordinates['lat'],
'longitude' => $coordinates['lng'],
'place_id' => $result['place_id'],
'types' => $result['types']
'types' => $result['types'],
'address_components' => $this->buildAddressComponents($result)
];

if (isset($result['geometry']['bounds'])) {
Expand All @@ -197,4 +198,14 @@ private function buildResults($results)
}, $results);
}

private function buildAddressComponents($result)
{
$test = [];

foreach ($result['address_components'] as $component) {
$test[$component['types'][0]] = $component['long_name'];
}

return $test;
}
}
16 changes: 16 additions & 0 deletions tests/GoogleGeocoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testItCanGeocode()
$geocoder = new GoogleGeocoder($mockClient);

$response = $geocoder->geocode('asheville');

$first = array_pop($response);

$this->assertEquals('Asheville, NC, USA', $first['address']);
Expand All @@ -40,6 +41,7 @@ public function testItCanGeocode()
'longitude' => -82.6708731
]
], $first['bounds']);
$this->assertEquals('Asheville', $first['address_components']['locality']);
}

public function testItCanReverseGeocodeByAPlaceId()
Expand Down Expand Up @@ -107,6 +109,20 @@ public function testItCanAddTheOptionalArguments()
$geocoder->geocode('asheville');
}

public function testItReturnsTheAddressComponents()
{
$mockClient = $this->getMockClient(TestResponses::getCityResponse());
$geocoder = new GoogleGeocoder($mockClient);

$response = $geocoder->geocode('asheville');

$first = array_pop($response);

$this->assertEquals('Asheville', $first['address_components']['locality']);
$this->assertEquals('North Carolina', $first['address_components']['administrative_area_level_1']);
$this->assertEquals('United States', $first['address_components']['country']);
}

public function testItThrowsANoResultExceptionWhenNoResponse()
{
$mockClient = $this->getMockClient(null);
Expand Down

0 comments on commit e8c119d

Please sign in to comment.