Skip to content

Commit

Permalink
[Geocoding] Allow to filter address components by type
Browse files Browse the repository at this point in the history
  • Loading branch information
GeLoLabs committed Feb 17, 2013
1 parent 6e09bc5 commit beac0c6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
12 changes: 12 additions & 0 deletions doc/usage/services/geocoding/ivory_geocoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ foreach ($results as $result) {
}
```

You can filter the address components by type:

``` php
// Get the geocoder results
$results = $response->getResults();

foreach ($results as $result) {
foreach ($result->getAddressComponents('route') as $addressComponent) {
// ...
}
}

#### Geometry informations

Geometry contains the following information:
Expand Down
18 changes: 16 additions & 2 deletions src/Ivory/GoogleMap/Services/Geocoding/Result/GeocoderResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,25 @@ public function __construct(
/**
* Gets the address components.
*
* @param string|null The type of the address components.
*
* @return array The address components.
*/
public function getAddressComponents()
public function getAddressComponents($type = null)
{
return $this->addressComponents;
if ($type === null) {
return $this->addressComponents;
}

$addressComponents = array();

foreach ($this->addressComponents as $addressComponent) {
if (in_array($type, $addressComponent->getTypes())) {
$addressComponents[] = $addressComponent;
}
}

return $addressComponents;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testInitialState()
$this->assertSame($this->partialMatch, $this->geocoderResult->isPartialMatch());
}

public function testAddressComponents()
public function testAddressComponentsWithoutType()
{
$addressComponent = $this->getMockBuilder('Ivory\GoogleMap\Services\Geocoding\Result\GeocoderAddressComponent')
->disableOriginalConstructor()
Expand All @@ -100,6 +100,25 @@ public function testAddressComponents()
$this->assertSame($addressComponents, $this->geocoderResult->getAddressComponents());
}

public function testAddressComponentsWithType()
{
$addressComponent = $this->getMockBuilder('Ivory\GoogleMap\Services\Geocoding\Result\GeocoderAddressComponent')
->disableOriginalConstructor()
->getMock();

$addressComponent
->expects($this->any())
->method('getTypes')
->will($this->returnValue(array('foo')));

$addressComponents = array($addressComponent);

$this->geocoderResult->setAddressComponents($addressComponents);

$this->assertSame($addressComponents, $this->geocoderResult->getAddressComponents('foo'));
$this->assertEmpty($this->geocoderResult->getAddressComponents('bar'));
}

public function testFormattedAddressWithValidValue()
{
$this->geocoderResult->setFormattedAddress('formatted_address');
Expand Down

0 comments on commit beac0c6

Please sign in to comment.