Skip to content

Commit f115148

Browse files
committed
Create chainable provider with dump functionality
Fixes #16
1 parent 1b25952 commit f115148

File tree

6 files changed

+157
-7
lines changed

6 files changed

+157
-7
lines changed

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
},
3535
"autoload": {
3636
"psr-4": {
37-
"Toin0u\\Geocoder\\": "src/",
38-
"Toin0u\\GeocoderLaravel\\Tests\\Laravel5_3\\": "tests/Laravel5_3/"
37+
"Toin0u\\Geocoder\\": "src/"
3938
}
4039
},
4140
"autoload-dev": {

config/geocoder.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@
4141
env('GOOGLE_MAPS_API_KEY'),
4242
],
4343
],
44-
// 'adapter' => CurlHttpAdapter::class,
45-
'adapter' => Guzzle6HttpAdapter::class,
44+
'adapter' => CurlHttpAdapter::class,
4645
];
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace Toin0u\Geocoder\Exceptions;
2+
3+
/**
4+
* This file is part of the GeocoderLaravel library.
5+
*
6+
* (c) Antoine Corcy <contact@sbin.dk>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Geocoder\Exception\Exception;
13+
use Exception as BaseException;
14+
15+
/**
16+
* Exception to indicate an invalidly specified dumper identifier when calling
17+
* the `dump()` method on the ProviderAndDumperAggregator class.
18+
*
19+
* @author Mike Bronner <hello@genealabs.com>
20+
*/
21+
class InvalidDumperException extends BaseException implements Exception
22+
{
23+
24+
}

src/GeocoderServiceProvider.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Geocoder\ProviderAggregator;
1312
use Geocoder\Provider\Chain;
1413
use Illuminate\Support\Collection;
1514
use Illuminate\Support\ServiceProvider;
1615
use ReflectionClass;
1716

17+
/**
18+
* Geocoder service provider
19+
*
20+
* @author Antoine Corcy <contact@sbin.dk>
21+
* @author Mike Bronner <hello@genealabs.com>
22+
*/
1823
class GeocoderServiceProvider extends ServiceProvider
1924
{
2025
/**
@@ -37,7 +42,7 @@ public function boot()
3742
public function register()
3843
{
3944
$this->app->singleton('geocoder', function () {
40-
$geocoder = new ProviderAggregator();
45+
$geocoder = new ProviderAndDumperAggregator();
4146
$geocoder->registerProviders(
4247
$this->getProviders(collect(config('geocoder.providers')))
4348
);

src/ProviderAndDumperAggregator.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php namespace Toin0u\Geocoder;
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+
use Geocoder\ProviderAggregator;
12+
use Geocoder\Geocoder;
13+
use Geocoder\Dumper\Gpx;
14+
use Geocoder\Dumper\Kml;
15+
use Geocoder\Dumper\Wkb;
16+
use Geocoder\Dumper\Wkt;
17+
use Geocoder\Dumper\GeoJson;
18+
use Illuminate\Support\Collection;
19+
use Toin0u\Geocoder\Exceptions\InvalidDumperException;
20+
21+
/**
22+
* @author Mike Bronner <hello@genealabs.com>
23+
*/
24+
class ProviderAndDumperAggregator extends ProviderAggregator implements Geocoder
25+
{
26+
/**
27+
* @var AddressCollection
28+
*/
29+
protected $results;
30+
31+
/**
32+
* @return array
33+
*/
34+
public function all()
35+
{
36+
return $this->results->all();
37+
}
38+
39+
/**
40+
* @param string
41+
* @return Collection
42+
*/
43+
public function dump($dumper)
44+
{
45+
$dumperClasses = collect([
46+
'geojson' => GeoJson::class,
47+
'gpx' => Gpx::class,
48+
'kml' => Kml::class,
49+
'wkb' => Wkb::class,
50+
'wkt' => Wkt::class,
51+
]);
52+
53+
if (! $dumperClasses->has($dumper)) {
54+
$errorMessage = implode('', [
55+
"The dumper specified ('{$dumper}') is invalid. Valid dumpers ",
56+
"are: geojson, gpx, kml, wkb, wkt.",
57+
]);
58+
throw new InvalidDumperException($errorMessage);
59+
}
60+
61+
$dumperClass = $dumperClasses->get($dumper);
62+
$dumper = new $dumperClass;
63+
$results = collect($this->results->all());
64+
65+
return $results->map(function ($result) use ($dumper) {
66+
return $dumper->dump($result);
67+
});
68+
}
69+
70+
/**
71+
* @param string
72+
* @return ProviderAndDumperAggregator
73+
*/
74+
public function geocode($value)
75+
{
76+
$this->results = parent::geocode($value);
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* @return ProviderAndDumperAggregator
83+
*/
84+
public function get()
85+
{
86+
return $this->results;
87+
}
88+
89+
/**
90+
* @param float
91+
* @param float
92+
* @return ProviderAndDumperAggregator
93+
*/
94+
public function reverse($latitude, $longitude)
95+
{
96+
$this->results = parent::reverse($latitude, $longitude);
97+
98+
return $this;
99+
}
100+
}

tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Toin0u\GeocoderLaravel\Tests\Laravel5_3\Providers;
22

33
use Toin0u\GeocoderLaravel\Tests\Laravel5_3\TestCase;
4+
use Toin0u\Geocoder\Exceptions\InvalidDumperException;
45

56
class GeocoderServiceProviderTest extends TestCase
67
{
@@ -48,7 +49,6 @@ public function testItCanUseMaxMindBinaryWithoutProvider()
4849

4950
public function testItCanUseASpecificProvider()
5051
{
51-
// dd(config('geocoder.providers'));
5252
$result = app('geocoder')
5353
->using('google_maps')
5454
->geocode('1600 Pennsylvania Ave., Washington, DC USA')
@@ -58,4 +58,27 @@ public function testItCanUseASpecificProvider()
5858
$this->assertEquals('Washington', $result[0]->getLocality());
5959
$this->assertEquals('20003', $result[0]->getPostalCode());
6060
}
61+
62+
public function testItDumpsAndAddress()
63+
{
64+
$result = app('geocoder')
65+
->using('google_maps')
66+
->geocode('1600 Pennsylvania Ave., Washington, DC USA')
67+
->dump('geojson');
68+
$jsonAddress = json_decode($result->first());
69+
70+
$this->assertEquals('1600', $jsonAddress->properties->streetNumber);
71+
}
72+
73+
public function testItThrowsAnExceptionForInvalidDumper()
74+
{
75+
$this->expectException(InvalidDumperException::class);
76+
$result = app('geocoder')
77+
->using('google_maps')
78+
->geocode('1600 Pennsylvania Ave., Washington, DC USA')
79+
->dump('test');
80+
$jsonAddress = json_decode($result->first());
81+
82+
$this->assertEquals('1600', $jsonAddress->properties->streetNumber);
83+
}
6184
}

0 commit comments

Comments
 (0)