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
4 changes: 2 additions & 2 deletions src/Geocoder/Dumper/Gpx.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Geocoder\Dumper;

use Geocoder\ProviderBasedGeocoder;
use Geocoder\Geocoder;
use Geocoder\Model\Address;

/**
Expand All @@ -35,7 +35,7 @@ public function dump(Address $address)
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">

GPX
, ProviderBasedGeocoder::VERSION);
, Geocoder::VERSION);

if ($address->getBounds()->isDefined()) {
$bounds = $address->getBounds();
Expand Down
24 changes: 21 additions & 3 deletions src/Geocoder/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Geocoder
/**
* Geocodes a given value.
*
* @param string $value A value to geocode.
* @param string $value
*
* @return Address[]
*/
Expand All @@ -34,10 +34,28 @@ public function geocode($value);
/**
* Reverses geocode given latitude and longitude values.
*
* @param double $latitude Latitude.
* @param double $longitude Longitude.
* @param double $latitude.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a dot to much.

* @param double $longitude
*
* @return Address[]
*/
public function reverse($latitude, $longitude);

/**
* Returns the maximum number of Address objects that can be
* returned by `geocode()` or `reverse()` methods.
*
* @return integer
*/
public function getLimit();

/**
* Sets the maximum number of `Address` objects that can be
* returned by `geocode()` or `reverse()` methods.
*
* @param integer $limit
*
* @return Geocoder
*/
public function limit($limit);
}
99 changes: 35 additions & 64 deletions src/Geocoder/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace Geocoder\Provider;

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

/**
Expand All @@ -21,90 +22,53 @@ abstract class AbstractProvider
/**
* @var HttpAdapterInterface
*/
protected $adapter;
private $adapter;

/**
* @var string
* @var AddressFactory
*/
protected $locale;
private $factory;

/**
* @var integer
*/
protected $maxResults = ProviderBasedGeocoder::MAX_RESULTS;
private $limit = Provider::MAX_RESULTS;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter.
* @param string $locale A locale (optional).
* @param HttpAdapterInterface $adapter An HTTP adapter
*/
public function __construct(HttpAdapterInterface $adapter, $locale = null)
{
$this->setAdapter($adapter);
$this->setLocale($locale);
}

/**
* Returns the HTTP adapter.
*
* @return HttpAdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}

/**
* Sets the HTTP adapter to be used for further requests.
*
* @param HttpAdapterInterface $adapter
*
* @return AbstractProvider
*/
public function setAdapter($adapter)
public function __construct(HttpAdapterInterface $adapter)
{
$this->adapter = $adapter;

return $this;
}

/**
* Returns the configured locale or null.
*
* @return string
*/
public function getLocale()
{
return $this->locale;
$this->factory = new AddressFactory();
}

/**
* {@inheritDoc}
*/
public function setLocale($locale = null)
public function limit($limit)
{
$this->locale = $locale;
$this->limit = $limit;

return $this;
}

/**
* {@inheritDoc}
*/
public function setMaxResults($maxResults)
public function getLimit()
{
$this->maxResults = $maxResults;

return $this;
return $this->limit;
}

/**
* Returns the maximum of wished results.
* Returns the HTTP adapter.
*
* @return integer
* @return HttpAdapterInterface
*/
public function getMaxResults()
public function getAdapter()
{
return $this->maxResults;
return $this->adapter;
}

/**
Expand All @@ -114,10 +78,15 @@ public function getMaxResults()
*/
protected function getDefaults()
{
return array(
return [
'latitude' => null,
'longitude' => null,
'bounds' => null,
'bounds' => [
'south' => null,
'west' => null,
'north' => null,
'east' => null,
],
'streetNumber' => null,
'streetName' => null,
'locality' => null,
Expand All @@ -130,7 +99,7 @@ protected function getDefaults()
'country' => null,
'countryCode' => null,
'timezone' => null,
);
];
}

/**
Expand All @@ -140,23 +109,25 @@ protected function getDefaults()
*/
protected function getLocalhostDefaults()
{
return array(
return [
'locality' => 'localhost',
'region' => 'localhost',
'county' => 'localhost',
'country' => 'localhost',
);
];
}

/**
* @param array $results
* @param array $data An array of data.
*
* @return array
* @return \Geocoder\Model\Address[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FQCN is not needed here ;)

*/
protected function fixEncoding(array $results)
protected function returnResults(array $data = [])
{
return array_map(function ($value) {
return is_string($value) ? utf8_encode($value) : $value;
}, $results);
if (0 < $this->getLimit()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to throw an exception, if getLimit returns a value lower than 0.

$data = array_slice($data, 0, $this->getLimit());
}

return $this->factory->createFromArray($data);
}
}
Loading