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
6 changes: 1 addition & 5 deletions src/Geocoder/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ public function getLocale()
}

/**
* Sets the locale to be used.
*
* @param string|null $locale If no locale is set, the provider or service will fallback.
*
* @return AbstractProvider
* {@inheritDoc}
*/
public function setLocale($locale = null)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Geocoder/Provider/LocaleAwareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ interface LocaleAwareProvider extends Provider
* @return string|null
*/
public function getLocale();

/**
* Sets the locale to be used.
*
* @param string|null $locale If no locale is set, the provider or service will fallback.
*
* @return LocaleAwareProvider Self object
*/
public function setLocale($locale = null);
}
17 changes: 16 additions & 1 deletion src/Geocoder/ProviderBasedGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
namespace Geocoder;

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

/**
* @author William Durand <william.durand1@gmail.com>
Expand Down Expand Up @@ -168,6 +169,20 @@ public function getMaxResults()
return $this->maxResults;
}

/**
* Change the locale to be used in locale aware requests.
*
* @param string
*/
public function setLocale($locale)
{
foreach ($this->providers as $provider) {
if ($provider instanceof LocaleAwareProvider) {
$provider->setLocale($locale);
}
}
}

/**
* Return the provider to use.
*
Expand Down
35 changes: 33 additions & 2 deletions tests/Geocoder/TestsProviderBasedGeocoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Geocoder\Tests;

use Geocoder\ProviderBasedGeocoder;
use Geocoder\Provider\Provider;
use Geocoder\Model\Address;
use Geocoder\Model\AddressFactory;
use Geocoder\ProviderBasedGeocoder;
use Geocoder\Provider\LocaleAwareProvider;
use Geocoder\Provider\Provider;

/**
* @author William Durand <william.durand1@gmail.com>
Expand Down Expand Up @@ -150,6 +151,19 @@ public function testSetMaxResults()
$this->assertSame(3, $this->geocoder->getMaxResults());
}

public function testSetLocale()
{
$provider1 = new MockProvider('test1');
$provider2 = new MockLocaleAwareProvider('test2');

$this->geocoder->registerProviders([$provider1, $provider2]);
$this->geocoder->setLocale('en');
$this->assertEquals('en', $provider2->getLocale());

$this->geocoder->setLocale(null);
$this->assertNull($provider2->getLocale());
}

public function testDefaultMaxResults()
{
$this->assertSame(ProviderBasedGeocoder::MAX_RESULTS, $this->geocoder->getMaxResults());
Expand Down Expand Up @@ -191,6 +205,23 @@ public function setMaxResults($maxResults)
}
}

class MockLocaleAwareProvider extends MockProvider implements LocaleAwareProvider
{
protected $locale;

public function getLocale()
{
return $this->locale;
}

public function setLocale($locale = null)
{
$this->locale = $locale;

return $this;
}
}

class MockProviderWithData extends MockProvider
{
public function getGeocodedData($address)
Expand Down