Skip to content

Commit 8dd71cf

Browse files
committed
Merge pull request #359 from geocoder-php/issue-357
[WIP] Extend #357
2 parents e0af340 + 49afa04 commit 8dd71cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1265
-1420
lines changed

src/Geocoder/Dumper/Gpx.php

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

1111
namespace Geocoder\Dumper;
1212

13-
use Geocoder\ProviderBasedGeocoder;
13+
use Geocoder\Geocoder;
1414
use Geocoder\Model\Address;
1515

1616
/**
@@ -35,7 +35,7 @@ public function dump(Address $address)
3535
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
3636
3737
GPX
38-
, ProviderBasedGeocoder::VERSION);
38+
, Geocoder::VERSION);
3939

4040
if ($address->getBounds()->isDefined()) {
4141
$bounds = $address->getBounds();

src/Geocoder/Geocoder.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface Geocoder
2525
/**
2626
* Geocodes a given value.
2727
*
28-
* @param string $value A value to geocode.
28+
* @param string $value
2929
*
3030
* @return Address[]
3131
*/
@@ -34,10 +34,28 @@ public function geocode($value);
3434
/**
3535
* Reverses geocode given latitude and longitude values.
3636
*
37-
* @param double $latitude Latitude.
38-
* @param double $longitude Longitude.
37+
* @param double $latitude.
38+
* @param double $longitude
3939
*
4040
* @return Address[]
4141
*/
4242
public function reverse($latitude, $longitude);
43+
44+
/**
45+
* Returns the maximum number of Address objects that can be
46+
* returned by `geocode()` or `reverse()` methods.
47+
*
48+
* @return integer
49+
*/
50+
public function getLimit();
51+
52+
/**
53+
* Sets the maximum number of `Address` objects that can be
54+
* returned by `geocode()` or `reverse()` methods.
55+
*
56+
* @param integer $limit
57+
*
58+
* @return Geocoder
59+
*/
60+
public function limit($limit);
4361
}

src/Geocoder/Provider/AbstractProvider.php

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
namespace Geocoder\Provider;
1212

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

1617
/**
@@ -21,90 +22,53 @@ abstract class AbstractProvider
2122
/**
2223
* @var HttpAdapterInterface
2324
*/
24-
protected $adapter;
25+
private $adapter;
2526

2627
/**
27-
* @var string
28+
* @var AddressFactory
2829
*/
29-
protected $locale;
30+
private $factory;
3031

3132
/**
3233
* @var integer
3334
*/
34-
protected $maxResults = ProviderBasedGeocoder::MAX_RESULTS;
35+
private $limit = Provider::MAX_RESULTS;
3536

3637
/**
37-
* @param HttpAdapterInterface $adapter An HTTP adapter.
38-
* @param string $locale A locale (optional).
38+
* @param HttpAdapterInterface $adapter An HTTP adapter
3939
*/
40-
public function __construct(HttpAdapterInterface $adapter, $locale = null)
41-
{
42-
$this->setAdapter($adapter);
43-
$this->setLocale($locale);
44-
}
45-
46-
/**
47-
* Returns the HTTP adapter.
48-
*
49-
* @return HttpAdapterInterface
50-
*/
51-
public function getAdapter()
52-
{
53-
return $this->adapter;
54-
}
55-
56-
/**
57-
* Sets the HTTP adapter to be used for further requests.
58-
*
59-
* @param HttpAdapterInterface $adapter
60-
*
61-
* @return AbstractProvider
62-
*/
63-
public function setAdapter($adapter)
40+
public function __construct(HttpAdapterInterface $adapter)
6441
{
6542
$this->adapter = $adapter;
66-
67-
return $this;
68-
}
69-
70-
/**
71-
* Returns the configured locale or null.
72-
*
73-
* @return string
74-
*/
75-
public function getLocale()
76-
{
77-
return $this->locale;
43+
$this->factory = new AddressFactory();
7844
}
7945

8046
/**
8147
* {@inheritDoc}
8248
*/
83-
public function setLocale($locale = null)
49+
public function limit($limit)
8450
{
85-
$this->locale = $locale;
51+
$this->limit = $limit;
8652

8753
return $this;
8854
}
8955

9056
/**
9157
* {@inheritDoc}
9258
*/
93-
public function setMaxResults($maxResults)
59+
public function getLimit()
9460
{
95-
$this->maxResults = $maxResults;
96-
97-
return $this;
61+
return $this->limit;
9862
}
9963

10064
/**
101-
* Returns the maximum of wished results.
65+
* Returns the HTTP adapter.
10266
*
103-
* @return integer
67+
* @return HttpAdapterInterface
10468
*/
105-
public function getMaxResults()
69+
public function getAdapter()
10670
{
107-
return $this->maxResults;
71+
return $this->adapter;
10872
}
10973

11074
/**
@@ -114,10 +78,15 @@ public function getMaxResults()
11478
*/
11579
protected function getDefaults()
11680
{
117-
return array(
81+
return [
11882
'latitude' => null,
11983
'longitude' => null,
120-
'bounds' => null,
84+
'bounds' => [
85+
'south' => null,
86+
'west' => null,
87+
'north' => null,
88+
'east' => null,
89+
],
12190
'streetNumber' => null,
12291
'streetName' => null,
12392
'locality' => null,
@@ -130,7 +99,7 @@ protected function getDefaults()
13099
'country' => null,
131100
'countryCode' => null,
132101
'timezone' => null,
133-
);
102+
];
134103
}
135104

136105
/**
@@ -140,23 +109,25 @@ protected function getDefaults()
140109
*/
141110
protected function getLocalhostDefaults()
142111
{
143-
return array(
112+
return [
144113
'locality' => 'localhost',
145114
'region' => 'localhost',
146115
'county' => 'localhost',
147116
'country' => 'localhost',
148-
);
117+
];
149118
}
150119

151120
/**
152-
* @param array $results
121+
* @param array $data An array of data.
153122
*
154-
* @return array
123+
* @return \Geocoder\Model\Address[]
155124
*/
156-
protected function fixEncoding(array $results)
125+
protected function returnResults(array $data = [])
157126
{
158-
return array_map(function ($value) {
159-
return is_string($value) ? utf8_encode($value) : $value;
160-
}, $results);
127+
if (0 < $this->getLimit()) {
128+
$data = array_slice($data, 0, $this->getLimit());
129+
}
130+
131+
return $this->factory->createFromArray($data);
161132
}
162133
}

0 commit comments

Comments
 (0)