Skip to content

Commit 245ff56

Browse files
committed
Implement fix for specialized providers
Fixes #24
1 parent a6d9e34 commit 245ff56

File tree

3 files changed

+89
-9
lines changed

3 files changed

+89
-9
lines changed

config/geocoder.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

33
use Ivory\HttpAdapter\CurlHttpAdapter;
4+
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
45
use Geocoder\Provider\BingMaps;
56
use Geocoder\Provider\FreeGeoIp;
67
use Geocoder\Provider\GoogleMaps;
8+
use Geocoder\Provider\MaxMindBinary;
79

810
/**
911
* This file is part of the GeocoderLaravel library.
@@ -18,12 +20,18 @@
1820
// Providers get called in the chain order given here.
1921
// The first one to return a result will be used.
2022
'providers' => [
21-
GoogleMaps::class => null,
23+
GoogleMaps::class => [
24+
'de-DE',
25+
'Wien, Österreich',
26+
true,
27+
env('GOOGLE_MAPS_API_KEY'),
28+
],
2229
BingMaps::class => [
2330
'en-US',
2431
env('BING_MAPS_API_KEY'),
2532
],
26-
FreeGeoIp::class => null,
33+
FreeGeoIp::class => [],
2734
],
28-
'adapter' => CurlHttpAdapter::class,
35+
// 'adapter' => CurlHttpAdapter::class,
36+
'adapter' => Guzzle6HttpAdapter::class,
2937
];

src/GeocoderServiceProvider.php

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ public function register()
5353
$this->app->singleton('geocoder.chain', function ($app) {
5454
$providers = collect(config('geocoder.providers'))
5555
->map(function ($arguments, $provider) {
56+
$arguments = $this->prepArguments($arguments, $provider);
5657
$reflection = new ReflectionClass($provider);
5758

58-
if (is_array($arguments)) {
59-
array_unshift($arguments, $this->app['geocoder.adapter']);
60-
return $reflection->newInstanceArgs($arguments);
61-
}
62-
63-
return $reflection->newInstance($this->app['geocoder.adapter']);
59+
return $reflection->newInstanceArgs($arguments);
6460
});
6561

6662
return new Chain($providers->toArray());
@@ -74,6 +70,60 @@ public function register()
7470
});
7571
}
7672

73+
private function prepArguments(array $arguments, $provider)
74+
{
75+
$specificAdapter = $this->providerRequiresSpecificAdapter($provider);
76+
77+
if ($specificAdapter) {
78+
array_unshift($arguments, $specificAdapter);
79+
80+
return $arguments;
81+
}
82+
83+
if ($this->providerRequiresAdapter($provider)) {
84+
array_unshift($arguments, $this->app['geocoder.adapter']);
85+
86+
return $arguments;
87+
}
88+
89+
return $arguments;
90+
}
91+
92+
private function providerRequiresSpecificAdapter($provider)
93+
{
94+
$specificAdapters = collect([
95+
'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter',
96+
]);
97+
98+
return $specificAdapters->get($provider);
99+
}
100+
101+
private function providerRequiresAdapter($provider)
102+
{
103+
$providersRequiringAdapter = collect([
104+
'Geocoder\Provider\ArcGISOnline',
105+
'Geocoder\Provider\BingMaps',
106+
'Geocoder\Provider\FreeGeoIp',
107+
'Geocoder\Provider\GeoIPs',
108+
'Geocoder\Provider\Geonames',
109+
'Geocoder\Provider\GeoPlugin',
110+
'Geocoder\Provider\GoogleMaps',
111+
'Geocoder\Provider\GoogleMapsBusiness',
112+
'Geocoder\Provider\HostIp',
113+
'Geocoder\Provider\IpInfoDb',
114+
'Geocoder\Provider\MapQuest',
115+
'Geocoder\Provider\MaxMind',
116+
'Geocoder\Provider\Nominatim',
117+
'Geocoder\Provider\OpenCage',
118+
'Geocoder\Provider\OpenStreetMap',
119+
'Geocoder\Provider\Provider',
120+
'Geocoder\Provider\TomTom',
121+
'Geocoder\Provider\Yandex',
122+
]);
123+
124+
return $providersRequiringAdapter->contains($provider);
125+
}
126+
77127
/**
78128
* Get the services provided by the provider.
79129
*

tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,26 @@ public function testItResolvesAGivenIPAddress()
2222
->all();
2323
$this->assertEquals('US', $result[0]->getCountry()->getCode());
2424
}
25+
26+
public function testItResolvesAGivenAddressWithUmlauts()
27+
{
28+
$result = app('geocoder')
29+
->geocode('Obere Donaustrasse 22, Wien, Österreich')
30+
->all();
31+
$this->assertEquals('22', $result[0]->getStreetNumber());
32+
$this->assertEquals('Obere Donaustraße', $result[0]->getStreetName());
33+
$this->assertEquals('Wien', $result[0]->getLocality());
34+
$this->assertEquals('1020', $result[0]->getPostalCode());
35+
}
36+
37+
public function testItCanUseMaxMindBinaryWithoutProvider()
38+
{
39+
$result = app('geocoder')
40+
->geocode('1600 Pennsylvania Ave., Washington, DC USA')
41+
->all();
42+
$this->assertEquals('1600', $result[0]->getStreetNumber());
43+
$this->assertEquals('Pennsylvania Avenue Southeast', $result[0]->getStreetName());
44+
$this->assertEquals('Washington', $result[0]->getLocality());
45+
$this->assertEquals('20003', $result[0]->getPostalCode());
46+
}
2547
}

0 commit comments

Comments
 (0)