Skip to content

Commit 341960f

Browse files
committed
Merge pull request #356 from MattKetmo/feat-locale-setter
Add ability to change providers locale at runtime
2 parents 75ac62b + f29a78c commit 341960f

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

src/Geocoder/Provider/AbstractProvider.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ public function getLocale()
7878
}
7979

8080
/**
81-
* Sets the locale to be used.
82-
*
83-
* @param string|null $locale If no locale is set, the provider or service will fallback.
84-
*
85-
* @return AbstractProvider
81+
* {@inheritDoc}
8682
*/
8783
public function setLocale($locale = null)
8884
{

src/Geocoder/Provider/LocaleAwareProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ interface LocaleAwareProvider extends Provider
2020
* @return string|null
2121
*/
2222
public function getLocale();
23+
24+
/**
25+
* Sets the locale to be used.
26+
*
27+
* @param string|null $locale If no locale is set, the provider or service will fallback.
28+
*
29+
* @return LocaleAwareProvider Self object
30+
*/
31+
public function setLocale($locale = null);
2332
}

src/Geocoder/ProviderBasedGeocoder.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
namespace Geocoder;
1212

1313
use Geocoder\Exception\ProviderNotRegistered;
14-
use Geocoder\Provider\Provider;
1514
use Geocoder\Model\AddressFactory;
15+
use Geocoder\Provider\LocaleAwareProvider;
16+
use Geocoder\Provider\Provider;
1617

1718
/**
1819
* @author William Durand <[email protected]>
@@ -168,6 +169,20 @@ public function getMaxResults()
168169
return $this->maxResults;
169170
}
170171

172+
/**
173+
* Change the locale to be used in locale aware requests.
174+
*
175+
* @param string
176+
*/
177+
public function setLocale($locale)
178+
{
179+
foreach ($this->providers as $provider) {
180+
if ($provider instanceof LocaleAwareProvider) {
181+
$provider->setLocale($locale);
182+
}
183+
}
184+
}
185+
171186
/**
172187
* Return the provider to use.
173188
*

tests/Geocoder/TestsProviderBasedGeocoderTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Geocoder\Tests;
44

5-
use Geocoder\ProviderBasedGeocoder;
6-
use Geocoder\Provider\Provider;
75
use Geocoder\Model\Address;
86
use Geocoder\Model\AddressFactory;
7+
use Geocoder\ProviderBasedGeocoder;
8+
use Geocoder\Provider\LocaleAwareProvider;
9+
use Geocoder\Provider\Provider;
910

1011
/**
1112
* @author William Durand <[email protected]>
@@ -150,6 +151,19 @@ public function testSetMaxResults()
150151
$this->assertSame(3, $this->geocoder->getMaxResults());
151152
}
152153

154+
public function testSetLocale()
155+
{
156+
$provider1 = new MockProvider('test1');
157+
$provider2 = new MockLocaleAwareProvider('test2');
158+
159+
$this->geocoder->registerProviders([$provider1, $provider2]);
160+
$this->geocoder->setLocale('en');
161+
$this->assertEquals('en', $provider2->getLocale());
162+
163+
$this->geocoder->setLocale(null);
164+
$this->assertNull($provider2->getLocale());
165+
}
166+
153167
public function testDefaultMaxResults()
154168
{
155169
$this->assertSame(ProviderBasedGeocoder::MAX_RESULTS, $this->geocoder->getMaxResults());
@@ -191,6 +205,23 @@ public function setMaxResults($maxResults)
191205
}
192206
}
193207

208+
class MockLocaleAwareProvider extends MockProvider implements LocaleAwareProvider
209+
{
210+
protected $locale;
211+
212+
public function getLocale()
213+
{
214+
return $this->locale;
215+
}
216+
217+
public function setLocale($locale = null)
218+
{
219+
$this->locale = $locale;
220+
221+
return $this;
222+
}
223+
}
224+
194225
class MockProviderWithData extends MockProvider
195226
{
196227
public function getGeocodedData($address)

0 commit comments

Comments
 (0)