Skip to content

Commit dca4d1f

Browse files
committed
Merge pull request #393 from giosh94mhz/locale_traits
Add LocaleTrait to reduce code duplication
2 parents 7cbd119 + 8b5028a commit dca4d1f

File tree

11 files changed

+76
-220
lines changed

11 files changed

+76
-220
lines changed

src/Geocoder/Adapter/GeoIP2Adapter.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use GeoIp2\ProviderInterface;
1414
use Geocoder\Exception\InvalidArgument;
1515
use Geocoder\Exception\UnsupportedOperation;
16+
use Geocoder\Provider\LocaleTrait;
1617

1718
/**
1819
* @author Jens Wiese <[email protected]>
@@ -25,6 +26,8 @@ class GeoIP2Adapter
2526
const GEOIP2_MODEL_CITY = 'city';
2627
const GEOIP2_MODEL_COUNTRY = 'country';
2728

29+
use LocaleTrait;
30+
2831
/**
2932
* @var ProviderInterface
3033
*/
@@ -35,11 +38,6 @@ class GeoIP2Adapter
3538
*/
3639
protected $geoIP2Model;
3740

38-
/**
39-
* @var string
40-
*/
41-
protected $locale;
42-
4341
/**
4442
* @param \GeoIp2\ProviderInterface $geoIpProvider
4543
* @param string $geoIP2Model (e.g. self::GEOIP2_MODEL_CITY)
@@ -57,25 +55,6 @@ public function __construct(ProviderInterface $geoIpProvider, $geoIP2Model = sel
5755
$this->geoIP2Model = $geoIP2Model;
5856
}
5957

60-
/**
61-
* @param string $locale
62-
* @return GeoIP2Adapter
63-
*/
64-
public function setLocale($locale)
65-
{
66-
$this->locale = $locale;
67-
68-
return $this;
69-
}
70-
71-
/**
72-
* @return string
73-
*/
74-
public function getLocale()
75-
{
76-
return $this->locale;
77-
}
78-
7958
/**
8059
* Returns the content fetched from a given resource.
8160
*

src/Geocoder/Provider/BingMaps.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
use Geocoder\Exception\InvalidCredentials;
1414
use Geocoder\Exception\NoResult;
1515
use Geocoder\Exception\UnsupportedOperation;
16+
use Geocoder\Provider\LocaleTrait;
1617
use Ivory\HttpAdapter\HttpAdapterInterface;
1718

1819
/**
1920
* @author David Guyon <[email protected]>
2021
*/
2122
class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider
2223
{
24+
use LocaleTrait;
25+
2326
/**
2427
* @var string
2528
*/
@@ -35,11 +38,6 @@ class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider
3538
*/
3639
private $apiKey;
3740

38-
/**
39-
* @var string
40-
*/
41-
private $locale;
42-
4341
/**
4442
* @param HttpAdapterInterface $adapter An HTTP adapter
4543
* @param string $apiKey An API key
@@ -94,24 +92,6 @@ public function getName()
9492
return 'bing_maps';
9593
}
9694

97-
/**
98-
* {@inheritDoc}
99-
*/
100-
public function getLocale()
101-
{
102-
return $this->locale;
103-
}
104-
105-
/**
106-
* {@inheritDoc}
107-
*/
108-
public function setLocale($locale)
109-
{
110-
$this->locale = $locale;
111-
112-
return $this;
113-
}
114-
11595
/**
11696
* @param string $query
11797
*/

src/Geocoder/Provider/GeoIP2.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414
use Geocoder\Adapter\GeoIP2Adapter;
1515
use Geocoder\Exception\NoResult;
1616
use Geocoder\Exception\UnsupportedOperation;
17+
use Geocoder\Provider\LocaleTrait;
1718

1819
/**
1920
* @author Jens Wiese <[email protected]>
2021
*/
2122
class GeoIP2 extends AbstractProvider implements LocaleAwareProvider
2223
{
24+
use LocaleTrait;
25+
2326
/**
2427
* @var GeoIP2Adapter
2528
*/
2629
private $adapter;
2730

28-
/**
29-
* @var string
30-
*/
31-
private $locale;
32-
3331
public function __construct(GeoIP2Adapter $adapter, $locale = 'en')
3432
{
3533
parent::__construct();
@@ -94,24 +92,6 @@ public function getName()
9492
return 'geoip2';
9593
}
9694

97-
/**
98-
* {@inheritDoc}
99-
*/
100-
public function getLocale()
101-
{
102-
return $this->locale;
103-
}
104-
105-
/**
106-
* {@inheritDoc}
107-
*/
108-
public function setLocale($locale)
109-
{
110-
$this->locale = $locale;
111-
112-
return $this;
113-
}
114-
11595
/**
11696
* @param string $address
11797
*/

src/Geocoder/Provider/Geonames.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Geocoder\Exception\InvalidCredentials;
1414
use Geocoder\Exception\NoResult;
1515
use Geocoder\Exception\UnsupportedOperation;
16+
use Geocoder\Provider\LocaleTrait;
1617
use Ivory\HttpAdapter\HttpAdapterInterface;
1718

1819
/**
@@ -30,15 +31,12 @@ class Geonames extends AbstractHttpProvider implements LocaleAwareProvider
3031
*/
3132
const REVERSE_ENDPOINT_URL = 'http://api.geonames.org/findNearbyPlaceNameJSON?lat=%F&lng=%F&style=full&maxRows=%d&username=%s';
3233

33-
/**
34-
* @var string
35-
*/
36-
private $username;
34+
use LocaleTrait;
3735

3836
/**
3937
* @var string
4038
*/
41-
private $locale;
39+
private $username;
4240

4341
/**
4442
* @param HttpAdapterInterface $adapter An HTTP adapter
@@ -94,24 +92,6 @@ public function getName()
9492
return 'geonames';
9593
}
9694

97-
/**
98-
* {@inheritDoc}
99-
*/
100-
public function getLocale()
101-
{
102-
return $this->locale;
103-
}
104-
105-
/**
106-
* {@inheritDoc}
107-
*/
108-
public function setLocale($locale)
109-
{
110-
$this->locale = $locale;
111-
112-
return $this;
113-
}
114-
11595
/**
11696
* @param string $query
11797
*/

src/Geocoder/Provider/GoogleMaps.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Geocoder\Exception\NoResult;
1515
use Geocoder\Exception\QuotaExceeded;
1616
use Geocoder\Exception\UnsupportedOperation;
17+
use Geocoder\Provider\LocaleTrait;
1718
use Ivory\HttpAdapter\HttpAdapterInterface;
1819

1920
/**
@@ -31,10 +32,7 @@ class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider
3132
*/
3233
const ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s';
3334

34-
/**
35-
* @var string
36-
*/
37-
private $locale;
35+
use LocaleTrait;
3836

3937
/**
4038
* @var string
@@ -103,24 +101,6 @@ public function getName()
103101
return 'google_maps';
104102
}
105103

106-
/**
107-
* {@inheritDoc}
108-
*/
109-
public function getLocale()
110-
{
111-
return $this->locale;
112-
}
113-
114-
/**
115-
* {@inheritDoc}
116-
*/
117-
public function setLocale($locale)
118-
{
119-
$this->locale = $locale;
120-
121-
return $this;
122-
}
123-
124104
public function setRegion($region)
125105
{
126106
$this->region = $region;

src/Geocoder/Provider/LocaleTrait.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Provider;
12+
13+
/**
14+
* @author Premi Giorgio <[email protected]>
15+
*/
16+
trait LocaleTrait
17+
{
18+
/**
19+
* @var string|null
20+
*/
21+
private $locale;
22+
23+
/**
24+
* Return the locale to be used in locale aware requests.
25+
*
26+
* In case there is no locale in use, null is returned.
27+
*
28+
* @return string|null
29+
*/
30+
public function getLocale()
31+
{
32+
return $this->locale;
33+
}
34+
35+
/**
36+
* Sets the locale to be used.
37+
*
38+
* @param string|null
39+
*
40+
* @return LocaleAwareProvider
41+
*/
42+
public function setLocale($locale)
43+
{
44+
$this->locale = $locale ?: null;
45+
46+
return $this;
47+
}
48+
}

src/Geocoder/Provider/Nominatim.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212

1313
use Geocoder\Exception\NoResult;
1414
use Geocoder\Exception\UnsupportedOperation;
15+
use Geocoder\Provider\LocaleTrait;
1516
use Ivory\HttpAdapter\HttpAdapterInterface;
1617

1718
/**
1819
* @author Niklas Närhinen <[email protected]>
1920
*/
2021
class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider
2122
{
22-
/**
23-
* @var string
24-
*/
25-
private $rootUrl;
23+
use LocaleTrait;
2624

2725
/**
2826
* @var string
2927
*/
30-
private $locale;
28+
private $rootUrl;
3129

3230
/**
3331
* @param HttpAdapterInterface $adapter An HTTP adapter.
@@ -147,24 +145,6 @@ public function getName()
147145
return 'nominatim';
148146
}
149147

150-
/**
151-
* {@inheritDoc}
152-
*/
153-
public function getLocale()
154-
{
155-
return $this->locale;
156-
}
157-
158-
/**
159-
* {@inheritDoc}
160-
*/
161-
public function setLocale($locale)
162-
{
163-
$this->locale = $locale;
164-
165-
return $this;
166-
}
167-
168148
/**
169149
* @param string $query
170150
*/

0 commit comments

Comments
 (0)