Skip to content

Commit 925b4d8

Browse files
MattKetmowilldurand
authored andcommitted
Make Provider extends Geocoder
1 parent 8cb6b10 commit 925b4d8

File tree

4 files changed

+37
-55
lines changed

4 files changed

+37
-55
lines changed

src/Geocoder/Provider/AbstractProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Geocoder\Provider;
1212

13+
use Geocoder\Model\AddressFactory;
1314
use Geocoder\ProviderBasedGeocoder;
1415
use Ivory\HttpAdapter\HttpAdapterInterface;
1516

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

37+
/**
38+
* @var AddressFactory
39+
*/
40+
protected $factory;
41+
3642
/**
3743
* @param HttpAdapterInterface $adapter An HTTP adapter.
3844
* @param string $locale A locale (optional).
@@ -41,6 +47,7 @@ public function __construct(HttpAdapterInterface $adapter, $locale = null)
4147
{
4248
$this->setAdapter($adapter);
4349
$this->setLocale($locale);
50+
$this->factory = new AddressFactory();
4451
}
4552

4653
/**
@@ -159,4 +166,14 @@ protected function fixEncoding(array $results)
159166
return is_string($value) ? utf8_encode($value) : $value;
160167
}, $results);
161168
}
169+
170+
/**
171+
* @param array $data An array of data.
172+
*
173+
* @return \Geocoder\Model\Address[]
174+
*/
175+
protected function returnResult(array $data = [])
176+
{
177+
return $this->factory->createFromArray($data);
178+
}
162179
}

src/Geocoder/Provider/GoogleMaps.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
namespace Geocoder\Provider;
1212

13+
use Geocoder\Exception\InvalidCredentials;
1314
use Geocoder\Exception\NoResult;
1415
use Geocoder\Exception\QuotaExceeded;
1516
use Geocoder\Exception\UnsupportedOperation;
16-
use Geocoder\Exception\InvalidCredentials;
17+
use Geocoder\Model\AddressFactory;
1718
use Ivory\HttpAdapter\HttpAdapterInterface;
1819

1920
/**
@@ -62,10 +63,17 @@ public function __construct(HttpAdapterInterface $adapter, $locale = null, $regi
6263
$this->apiKey = $apiKey;
6364
}
6465

66+
public function setRegion($region)
67+
{
68+
$this->region = $region;
69+
70+
return $this;
71+
}
72+
6573
/**
6674
* {@inheritDoc}
6775
*/
68-
public function getGeocodedData($address)
76+
public function geocode($address)
6977
{
7078
// Google API returns invalid data if IP address given
7179
// This API doesn't handle IPs
@@ -84,9 +92,9 @@ public function getGeocodedData($address)
8492
/**
8593
* {@inheritDoc}
8694
*/
87-
public function getReversedData(array $coordinates)
95+
public function reverse($latitude, $longitude)
8896
{
89-
return $this->getGeocodedData(sprintf('%F,%F', $coordinates[0], $coordinates[1]));
97+
return $this->getGeocodedData(sprintf('%F,%F', $latitude, $longitude));
9098
}
9199

92100
/**
@@ -197,7 +205,7 @@ private function executeQuery($query)
197205
$results[] = array_merge($this->getDefaults(), $resultset);
198206
}
199207

200-
return $results;
208+
return $this->returnResult($results);
201209
}
202210

203211
/**

src/Geocoder/Provider/Provider.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,16 @@
1010

1111
namespace Geocoder\Provider;
1212

13-
use Geocoder\Exception\NoResult;
1413
use Geocoder\Exception\InvalidCredentials;
14+
use Geocoder\Exception\NoResult;
1515
use Geocoder\Exception\UnsupportedOperation;
16+
use Geocoder\Geocoder;
1617

1718
/**
1819
* @author William Durand <[email protected]>
1920
*/
20-
interface Provider
21+
interface Provider extends Geocoder
2122
{
22-
/**
23-
* Returns an associative array with data treated by the provider.
24-
*
25-
* @param string $address An address (IP or street).
26-
*
27-
* @throws NoResult If the address could not be resolved
28-
* @throws InvalidCredentials If the credentials are invalid
29-
* @throws UnsupportedOperation If IPv4, IPv6 or street is not supported
30-
*
31-
* @return array
32-
*/
33-
public function getGeocodedData($address);
34-
35-
/**
36-
* Returns an associative array with data treated by the provider.
37-
*
38-
* @param array $coordinates Coordinates (latitude, longitude).
39-
*
40-
* @throws NoResult If the coordinates could not be resolved
41-
* @throws InvalidCredentials If the credentials are invalid
42-
* @throws UnsupportedOperation If reverse geocoding is not supported
43-
*
44-
* @return array
45-
*/
46-
public function getReversedData(array $coordinates);
47-
4823
/**
4924
* Returns the provider's name.
5025
*

src/Geocoder/ProviderBasedGeocoder.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
namespace Geocoder;
1212

1313
use Geocoder\Exception\ProviderNotRegistered;
14-
use Geocoder\Model\AddressFactory;
15-
use Geocoder\Provider\LocaleAwareProvider;
1614
use Geocoder\Provider\Provider;
1715

1816
/**
@@ -35,11 +33,6 @@ class ProviderBasedGeocoder implements Geocoder
3533
*/
3634
private $provider;
3735

38-
/**
39-
* @var AddressFactory
40-
*/
41-
private $factory;
42-
4336
/**
4437
* @var integer
4538
*/
@@ -52,7 +45,6 @@ class ProviderBasedGeocoder implements Geocoder
5245
public function __construct(Provider $provider = null, $maxResults = self::MAX_RESULTS)
5346
{
5447
$this->provider = $provider;
55-
$this->factory = new AddressFactory();
5648

5749
$this->limit($maxResults);
5850
}
@@ -62,15 +54,16 @@ public function __construct(Provider $provider = null, $maxResults = self::MAX_R
6254
*/
6355
public function geocode($value)
6456
{
57+
$value = trim($value);
58+
6559
if (empty($value)) {
6660
// let's save a request
6761
return [];
6862
}
6963

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

73-
return $this->returnResult($data);
66+
return $provider->geocode($value);
7467
}
7568

7669
/**
@@ -84,9 +77,8 @@ public function reverse($latitude, $longitude)
8477
}
8578

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

89-
return $this->returnResult($data);
81+
return $provider->reverse($latitude, $longitude);
9082
}
9183

9284
/**
@@ -200,14 +192,4 @@ protected function getProvider()
200192

201193
return $this->provider;
202194
}
203-
204-
/**
205-
* @param array $data An array of data.
206-
*
207-
* @return \Geocoder\Model\Address[]
208-
*/
209-
protected function returnResult(array $data = [])
210-
{
211-
return $this->factory->createFromArray($data);
212-
}
213195
}

0 commit comments

Comments
 (0)