Skip to content
Closed
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
17 changes: 17 additions & 0 deletions src/Geocoder/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Geocoder\Provider;

use Geocoder\Model\AddressFactory;
use Geocoder\ProviderBasedGeocoder;
use Ivory\HttpAdapter\HttpAdapterInterface;

Expand All @@ -33,6 +34,11 @@ abstract class AbstractProvider
*/
protected $maxResults = ProviderBasedGeocoder::MAX_RESULTS;

/**
* @var AddressFactory
*/
protected $factory;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter.
* @param string $locale A locale (optional).
Expand All @@ -41,6 +47,7 @@ public function __construct(HttpAdapterInterface $adapter, $locale = null)
{
$this->setAdapter($adapter);
$this->setLocale($locale);
$this->factory = new AddressFactory();
}

/**
Expand Down Expand Up @@ -163,4 +170,14 @@ protected function fixEncoding(array $results)
return is_string($value) ? utf8_encode($value) : $value;
}, $results);
}

/**
* @param array $data An array of data.
*
* @return \Geocoder\Model\Address[]
*/
protected function returnResult(array $data = [])
{
return $this->factory->createFromArray($data);
}
}
18 changes: 13 additions & 5 deletions src/Geocoder/Provider/GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

namespace Geocoder\Provider;

use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\NoResult;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Model\AddressFactory;
use Ivory\HttpAdapter\HttpAdapterInterface;

/**
Expand Down Expand Up @@ -62,10 +63,17 @@ public function __construct(HttpAdapterInterface $adapter, $locale = null, $regi
$this->apiKey = $apiKey;
}

public function setRegion($region)
{
$this->region = $region;

return $this;
}

/**
* {@inheritDoc}
*/
public function getGeocodedData($address)
public function geocode($address)
{
// Google API returns invalid data if IP address given
// This API doesn't handle IPs
Expand All @@ -84,9 +92,9 @@ public function getGeocodedData($address)
/**
* {@inheritDoc}
*/
public function getReversedData(array $coordinates)
public function reverse($latitude, $longitude)
{
return $this->getGeocodedData(sprintf('%F,%F', $coordinates[0], $coordinates[1]));
return $this->getGeocodedData(sprintf('%F,%F', $latitude, $longitude));
}

/**
Expand Down Expand Up @@ -197,7 +205,7 @@ private function executeQuery($query)
$results[] = array_merge($this->getDefaults(), $resultset);
}

return $results;
return $this->returnResult($results);
}

/**
Expand Down
31 changes: 3 additions & 28 deletions src/Geocoder/Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,16 @@

namespace Geocoder\Provider;

use Geocoder\Exception\NoResult;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Geocoder;

/**
* @author William Durand <william.durand1@gmail.com>
*/
interface Provider
interface Provider extends Geocoder
{
/**
* Returns an associative array with data treated by the provider.
*
* @param string $address An address (IP or street).
*
* @throws NoResult If the address could not be resolved
* @throws InvalidCredentials If the credentials are invalid
* @throws UnsupportedOperation If IPv4, IPv6 or street is not supported
*
* @return array
*/
public function getGeocodedData($address);

/**
* Returns an associative array with data treated by the provider.
*
* @param array $coordinates Coordinates (latitude, longitude).
*
* @throws NoResult If the coordinates could not be resolved
* @throws InvalidCredentials If the credentials are invalid
* @throws UnsupportedOperation If reverse geocoding is not supported
*
* @return array
*/
public function getReversedData(array $coordinates);

/**
* Returns the provider's name.
*
Expand Down
25 changes: 4 additions & 21 deletions src/Geocoder/ProviderBasedGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Geocoder\Exception\ProviderNotRegistered;
use Geocoder\Provider\Provider;
use Geocoder\Model\AddressFactory;

/**
* @author William Durand <william.durand1@gmail.com>
Expand All @@ -34,11 +33,6 @@ class ProviderBasedGeocoder implements Geocoder
*/
private $provider;

/**
* @var AddressFactory
*/
private $factory;

/**
* @var integer
*/
Expand All @@ -51,7 +45,6 @@ class ProviderBasedGeocoder implements Geocoder
public function __construct(Provider $provider = null, $maxResults = self::MAX_RESULTS)
{
$this->provider = $provider;
$this->factory = new AddressFactory();

$this->limit($maxResults);
}
Expand All @@ -61,15 +54,16 @@ public function __construct(Provider $provider = null, $maxResults = self::MAX_R
*/
public function geocode($value)
{
$value = trim($value);

if (empty($value)) {
// let's save a request
return [];
}

$provider = $this->getProvider()->setMaxResults($this->getMaxResults());
$data = $provider->getGeocodedData(trim($value));

return $this->returnResult($data);
return $provider->geocode($value);
}

/**
Expand All @@ -83,9 +77,8 @@ public function reverse($latitude, $longitude)
}

$provider = $this->getProvider()->setMaxResults($this->getMaxResults());
$data = $provider->getReversedData([ $latitude, $longitude ]);

return $this->returnResult($data);
return $provider->reverse($latitude, $longitude);
}

/**
Expand Down Expand Up @@ -185,14 +178,4 @@ protected function getProvider()

return $this->provider;
}

/**
* @param array $data An array of data.
*
* @return \Geocoder\Model\Address[]
*/
protected function returnResult(array $data = [])
{
return $this->factory->createFromArray($data);
}
}