Skip to content

Commit b38a86d

Browse files
committed
Implemented compatibility with Geocoder 4.0
1 parent e35aebf commit b38a86d

File tree

7 files changed

+179
-128
lines changed

7 files changed

+179
-128
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [2.0.0-RC1] - 18 Jun 2017
6+
### Added
7+
- compatibility with Geocoder 4.0-dev.
8+
- caching to `geocodeQuery()` and `reverseQuery()` methods.
9+
10+
### Updated
11+
- the geocoder `all()` method to be deprecated. Use `get()`.
12+
513
## [1.1.0] - 17 Jun 2017
614
### Added
715
- caching functionality for `geocode()` and `reverse()` methods.

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
> If you still use **Laravel 4**, please check out the `0.4.x` branch
99
[here](https://github.com/geocoder-php/GeocoderLaravel/tree/0.4.x).
1010

11-
**Version 1.0.0 is a backwards-compatibility-breaking update. Please review
11+
**Version 2.0.0 is a backwards-compatibility-breaking update. Please review
1212
this documentation, especially the _Usage_ section before installing.**
1313

1414
This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/)
1515
in [**Laravel 5**](http://laravel.com/).
1616

17+
## Requirements
18+
- PHP >= 7.0.0
19+
- Laravel >= 5.0
20+
1721
## Installation
1822
1. Install the package via composer:
1923
```sh
2024
composer require toin0u/geocoder-laravel
2125
```
22-
_Once 1.0.0 is stable, we will update this command to reflect that. In the interest of getting it out and into your hands, a temporary RC build is best._
2326

2427
2. Find the `providers` array key in `config/app.php` and register the **Geocoder Service Provider**:
2528
```php
@@ -29,6 +32,31 @@ composer require toin0u/geocoder-laravel
2932
```
3033

3134
## Upgrading
35+
### 1.x to 2.x
36+
The one change to keep in mind here is that the results returned from
37+
`Geocoder for Laravel` are now using the Laravel-native Collections class
38+
instead of returning and instance of `AddressCollection`. This should provide
39+
greater versatility in manipulation of the results, and be inline with
40+
expectations for working with Laravel. The existing `AddressCollection`
41+
methods should map strait over to Laravel's `Collection` methods. But be sure
42+
to double-check your results, if you have been using `getIterator()`, `count()`,
43+
`first()`, `isEmpty()`, `slice()`, `has()`, `get()`, or `all()` on your results.
44+
45+
**Alert:** if you have been using the `getIterator()` method, it is no longer
46+
needed. Simply iterate over your results as you would any other Laravel
47+
collection.
48+
49+
**Deprecated:** the `all()` method on the geocoder is being deprecated in favor
50+
of using `get()`, which will return a Laravel Collection. You can then run
51+
`all()` on that. This method will be removed in version 3.0.0.
52+
53+
**Added:** this version introduces a new way to create more complex queries:
54+
- geocodeQuery()
55+
- reverseQuery()
56+
Please see the [Geocoder documentation](https://github.com/geocoder-php/Geocoder)
57+
for more details.
58+
59+
### 0.x to 1.x
3260
If you are upgrading from a pre-1.x version of this package, please keep the
3361
following things in mind:
3462

@@ -128,11 +156,6 @@ The service provider initializes the `geocoder` service, accessible via the
128156
app('geocoder')->geocode('Los Angeles, CA')->get();
129157
```
130158

131-
#### Get Array of Addresses
132-
```php
133-
app('geocoder')->geocode('Los Angeles, CA')->all();
134-
```
135-
136159
#### Reverse-Geocoding
137160
```php
138161
app('geocoder')->reverse(43.882587,-103.454067)->get();

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
],
2525
"require": {
2626
"php": ">=5.4",
27+
"geocoder-php/bing-maps-provider": "dev-master@dev",
28+
"geocoder-php/chain-provider": "dev-master@dev",
2729
"geocoder-php/common-http": "dev-master@dev",
30+
"geocoder-php/free-geoip-provider": "dev-master@dev",
2831
"geocoder-php/google-maps-provider": "dev-master@dev",
32+
"geocoder-php/maxmind-binary-provider": "dev-master@dev",
2933
"guzzlehttp/guzzle": "^6.2",
3034
"illuminate/cache": "~5.0",
3135
"illuminate/support": "~5.0",
@@ -61,7 +65,7 @@
6165
},
6266
"extra": {
6367
"branch-alias": {
64-
"dev-master": "1.2.0-dev"
68+
"dev-master": "2.0.0-RC1"
6569
}
6670
},
6771
"config": {

config/geocoder.php

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,32 @@
22

33
/**
44
* This file is part of the GeocoderLaravel library.
5-
*
6-
* (c) Antoine Corcy <[email protected]>
7-
*
85
* For the full copyright and license information, please view the LICENSE
96
* file that was distributed with this source code.
107
*/
118

129
use Http\Adapter\Guzzle6\Client;
13-
// use Geocoder\Provider\Chain;
14-
// use Geocoder\Provider\BingMaps;
15-
// use Geocoder\Provider\FreeGeoIp;
16-
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress;
17-
// use Geocoder\Provider\MaxMindBinary;
10+
use Geocoder\Provider\BingMaps\BingMaps;
11+
use Geocoder\Provider\Chain\Chain;
12+
use Geocoder\Provider\FreeGeoIp\FreeGeoIp;
13+
use Geocoder\Provider\GoogleMaps\GoogleMaps;
1814

1915
return [
2016
'cache-duraction' => 999999999,
2117
'providers' => [
22-
// Chain::class => [
23-
// GoogleMaps::class => [
24-
// 'en',
25-
// 'us',
26-
// true,
27-
// env('GOOGLE_MAPS_API_KEY'),
28-
// ],
29-
// FreeGeoIp::class => [],
30-
// ],
31-
// BingMaps::class => [
32-
// 'en-US',
33-
// env('BING_MAPS_API_KEY'),
34-
// ],
35-
GoogleAddress::class => [
36-
'en',
18+
Chain::class => [
19+
GoogleMaps::class => [
20+
'en-US',
21+
env('GOOGLE_MAPS_API_KEY'),
22+
],
23+
FreeGeoIp::class => [],
24+
],
25+
BingMaps::class => [
26+
'en-US',
27+
env('BING_MAPS_API_KEY'),
28+
],
29+
GoogleMaps::class => [
3730
'us',
38-
true,
3931
env('GOOGLE_MAPS_API_KEY'),
4032
],
4133
],

src/ProviderAndDumperAggregator.php

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php namespace Geocoder\Laravel;
22

33
/**
4-
* This file is part of the Geocoder package.
4+
* This file is part of the Geocoder Laravel package.
55
* For the full copyright and license information, please view the LICENSE
66
* file that was distributed with this source code.
77
*
8+
* @author Mike Bronner <[email protected]>
89
* @license MIT License
910
*/
1011

@@ -14,36 +15,37 @@
1415
use Geocoder\Dumper\Wkb;
1516
use Geocoder\Dumper\Wkt;
1617
use Geocoder\Geocoder;
18+
use Geocoder\Query\GeocodeQuery;
19+
use Geocoder\Query\ReverseQuery;
1720
use Geocoder\Laravel\Exceptions\InvalidDumperException;
18-
use Geocoder\Laravel\ProviderAndDumperAggregator;
1921
use Geocoder\ProviderAggregator;
20-
use Geocoder\Model\AddressCollection;
2122
use Illuminate\Support\Collection;
2223

23-
/**
24-
* @author Mike Bronner <[email protected]>
25-
*/
2624
class ProviderAndDumperAggregator
2725
{
28-
protected $results;
2926
protected $aggregator;
27+
protected $results;
3028

3129
public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
3230
{
3331
$this->aggregator = new ProviderAggregator($limit);
32+
$this->results = collect();
3433
}
3534

35+
/**
36+
* @deprecated Use `get()` instead.
37+
*/
3638
public function all() : array
3739
{
3840
return $this->results->all();
3941
}
4042

41-
public function get() : AddressCollection
43+
public function get() : Collection
4244
{
4345
return $this->results;
4446
}
4547

46-
public function dump($dumper) : Collection
48+
public function dump(string $dumper) : Collection
4749
{
4850
$dumperClasses = collect([
4951
'geojson' => GeoJson::class,
@@ -70,29 +72,69 @@ public function dump($dumper) : Collection
7072
});
7173
}
7274

73-
public function geocodeQuery($query)
75+
public function geocodeQuery(GeocodeQuery $query) : self
7476
{
75-
return $this->aggregator->geocodeQuery($query);
77+
$cacheKey = serialize($query);
78+
$this->results = cache()->remember(
79+
"geocoder-{$cacheKey}",
80+
config('geocoder.cache-duraction', 0),
81+
function () use ($query) {
82+
$addresses = collect();
83+
$addressCollection = $this->aggregator->geocodeQuery($query);
84+
85+
foreach ($addressCollection as $address) {
86+
$addresses->push($address);
87+
}
88+
89+
return $addresses;
90+
}
91+
);
92+
93+
return $this;
7694
}
7795

78-
public function reverseQuery($query)
96+
public function reverseQuery(ReverseQuery $query) : self
7997
{
80-
return $this->aggregator->reverseQuery($query);
98+
$cacheKey = serialize($query);
99+
$this->results = cache()->remember(
100+
"geocoder-{$cacheKey}",
101+
config('geocoder.cache-duraction', 0),
102+
function () use ($query) {
103+
$addresses = collect();
104+
$addressCollection = $this->aggregator->reverseQuery($query);
105+
106+
foreach ($addressCollection as $address) {
107+
$addresses->push($address);
108+
}
109+
110+
return $addresses;
111+
}
112+
);
113+
114+
return $this;
81115
}
82116

83-
public function getName()
117+
public function getName() : string
84118
{
85119
return $this->aggregator->getName();
86120
}
87121

88122
public function geocode(string $value) : self
89123
{
90-
$cacheId = str_slug($value);
124+
$cacheKey = str_slug($value);
91125
$this->results = cache()->remember(
92-
"geocoder-{$cacheId}",
126+
"geocoder-{$cacheKey}",
93127
config('geocoder.cache-duraction', 0),
94128
function () use ($value) {
95-
return $this->aggregator->geocode($value);
129+
130+
$addresses = collect();
131+
$addressCollection = $this->aggregator->geocode($value);
132+
133+
foreach ($addressCollection as $address) {
134+
$addresses->push($address);
135+
}
136+
137+
return $addresses;
96138
}
97139
);
98140

@@ -106,47 +148,54 @@ public function reverse(float $latitude, float $longitude) : self
106148
"geocoder-{$cacheId}",
107149
config('geocoder.cache-duraction', 0),
108150
function () use ($latitude, $longitude) {
109-
return $this->aggregator->reverse($latitude, $longitude);
151+
$addresses = collect();
152+
$addressCollection = $this->aggregator->reverse($latitude, $longitude);
153+
154+
foreach ($addressCollection as $address) {
155+
$addresses->push($address);
156+
}
157+
158+
return $addresses;
110159
}
111160
);
112161

113162
return $this;
114163
}
115164

116-
public function limit($limit)
165+
public function limit(int $limit) : self
117166
{
118167
$this->aggregator->limit($limit);
119168

120169
return $this;
121170
}
122171

123-
public function getLimit()
172+
public function getLimit() : int
124173
{
125174
return $this->aggregator->getLimit();
126175
}
127176

128-
public function registerProvider($provider)
177+
public function registerProvider(string $provider) : self
129178
{
130179
$this->aggregator->registerProvider($provider);
131180

132181
return $this;
133182
}
134183

135-
public function registerProviders($providers = [])
184+
public function registerProviders(array $providers = []) : self
136185
{
137186
$this->aggregator->registerProviders($providers);
138187

139188
return $this;
140189
}
141190

142-
public function using($name)
191+
public function using(string $name) : self
143192
{
144193
$this->aggregator->using($name);
145194

146195
return $this;
147196
}
148197

149-
public function getProviders()
198+
public function getProviders() : array
150199
{
151200
return $this->aggregator->getProviders();
152201
}

0 commit comments

Comments
 (0)