1
|
|
|
<?php namespace Geocoder\Laravel; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Geocoder Laravel package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Mike Bronner <[email protected]> |
9
|
|
|
* @license MIT License |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Geocoder\Dumper\GeoJson; |
13
|
|
|
use Geocoder\Dumper\Gpx; |
14
|
|
|
use Geocoder\Dumper\Kml; |
15
|
|
|
use Geocoder\Dumper\Wkb; |
16
|
|
|
use Geocoder\Dumper\Wkt; |
17
|
|
|
use Geocoder\Geocoder; |
18
|
|
|
use Geocoder\Query\GeocodeQuery; |
19
|
|
|
use Geocoder\Query\ReverseQuery; |
20
|
|
|
use Geocoder\Laravel\Exceptions\InvalidDumperException; |
21
|
|
|
use Geocoder\ProviderAggregator; |
22
|
|
|
use Illuminate\Support\Collection; |
23
|
|
|
use ReflectionClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
27
|
|
|
*/ |
28
|
|
|
class ProviderAndDumperAggregator |
29
|
|
|
{ |
30
|
|
|
protected $aggregator; |
31
|
|
|
protected $limit; |
32
|
|
|
protected $results; |
33
|
|
|
|
34
|
24 |
|
public function __construct() |
35
|
|
|
{ |
36
|
24 |
|
$this->aggregator = new ProviderAggregator(); |
37
|
24 |
|
$this->results = collect(); |
38
|
24 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @deprecated Use `get()` instead. |
42
|
|
|
*/ |
43
|
1 |
|
public function all() : array |
44
|
|
|
{ |
45
|
1 |
|
return $this->results->all(); |
46
|
|
|
} |
47
|
|
|
|
48
|
17 |
|
public function get() : Collection |
49
|
|
|
{ |
50
|
17 |
|
return $this->results; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function dump(string $dumper) : Collection |
54
|
|
|
{ |
55
|
2 |
|
$dumperClasses = collect([ |
56
|
2 |
|
'geojson' => GeoJson::class, |
57
|
|
|
'gpx' => Gpx::class, |
58
|
|
|
'kml' => Kml::class, |
59
|
|
|
'wkb' => Wkb::class, |
60
|
|
|
'wkt' => Wkt::class, |
61
|
|
|
]); |
62
|
|
|
|
63
|
2 |
|
if (!$dumperClasses->has($dumper)) { |
64
|
1 |
|
$errorMessage = implode('', [ |
65
|
1 |
|
"The dumper specified ('{$dumper}') is invalid. Valid dumpers ", |
66
|
1 |
|
"are: geojson, gpx, kml, wkb, wkt.", |
67
|
|
|
]); |
68
|
1 |
|
throw new InvalidDumperException($errorMessage); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$dumperClass = $dumperClasses->get($dumper); |
72
|
1 |
|
$dumper = new $dumperClass; |
73
|
1 |
|
$results = collect($this->results->all()); |
74
|
|
|
|
75
|
1 |
|
return $results->map(function ($result) use ($dumper) { |
76
|
1 |
|
return $dumper->dump($result); |
77
|
1 |
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function geocodeQuery(GeocodeQuery $query) : self |
81
|
|
|
{ |
82
|
1 |
|
$cacheKey = serialize($query); |
83
|
1 |
|
$this->results = app('cache')->remember( |
84
|
1 |
|
"geocoder-{$cacheKey}", |
85
|
1 |
|
config('geocoder.cache-duration', 0), |
86
|
1 |
|
function () use ($query) { |
87
|
1 |
|
return collect($this->aggregator->geocodeQuery($query)); |
88
|
1 |
|
} |
89
|
|
|
); |
90
|
|
|
|
91
|
1 |
|
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function reverseQuery(ReverseQuery $query) : self |
97
|
|
|
{ |
98
|
1 |
|
$cacheKey = serialize($query); |
99
|
1 |
|
$this->results = app('cache')->remember( |
100
|
1 |
|
"geocoder-{$cacheKey}", |
101
|
1 |
|
config('geocoder.cache-duration', 0), |
102
|
1 |
|
function () use ($query) { |
103
|
1 |
|
return collect($this->aggregator->reverseQuery($query)); |
104
|
1 |
|
} |
105
|
|
|
); |
106
|
|
|
|
107
|
1 |
|
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); |
108
|
|
|
|
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public function getName() : string |
113
|
|
|
{ |
114
|
1 |
|
return $this->aggregator->getName(); |
115
|
|
|
} |
116
|
|
|
|
117
|
16 |
View Code Duplication |
public function geocode(string $value) : self |
118
|
|
|
{ |
119
|
16 |
|
$cacheKey = str_slug(strtolower(urlencode($value))); |
120
|
16 |
|
$this->results = app('cache')->remember( |
121
|
16 |
|
"geocoder-{$cacheKey}", |
122
|
16 |
|
config('geocoder.cache-duration', 0), |
123
|
16 |
|
function () use ($value) { |
124
|
16 |
|
return collect($this->aggregator->geocode($value)); |
125
|
16 |
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
16 |
|
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); |
129
|
|
|
|
130
|
16 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
View Code Duplication |
public function reverse(float $latitude, float $longitude) : self |
134
|
|
|
{ |
135
|
1 |
|
$cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}"))); |
136
|
1 |
|
$this->results = app('cache')->remember( |
137
|
1 |
|
"geocoder-{$cacheKey}", |
138
|
1 |
|
config('geocoder.cache-duration', 0), |
139
|
1 |
|
function () use ($latitude, $longitude) { |
140
|
1 |
|
return collect($this->aggregator->reverse($latitude, $longitude)); |
141
|
1 |
|
} |
142
|
|
|
); |
143
|
|
|
|
144
|
1 |
|
$this->removeEmptyCacheEntry("geocoder-{$cacheKey}"); |
145
|
|
|
|
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
public function limit(int $limit) : self |
150
|
|
|
{ |
151
|
1 |
|
$this->aggregator = new ProviderAggregator(null, $limit); |
152
|
1 |
|
$this->registerProvidersFromConfig(collect(config('geocoder.providers'))); |
153
|
1 |
|
$this->limit = $limit; |
154
|
|
|
|
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
public function getLimit() : int |
159
|
|
|
{ |
160
|
1 |
|
return $this->limit; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
public function registerProvider($provider) : self |
164
|
|
|
{ |
165
|
1 |
|
$this->aggregator->registerProvider($provider); |
166
|
|
|
|
167
|
1 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
24 |
|
public function registerProviders(array $providers = []) : self |
171
|
|
|
{ |
172
|
24 |
|
$this->aggregator->registerProviders($providers); |
173
|
|
|
|
174
|
24 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
5 |
|
public function using(string $name) : self |
178
|
|
|
{ |
179
|
5 |
|
$this->aggregator = $this->aggregator->using($name); |
180
|
|
|
|
181
|
5 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
public function getProviders() : Collection |
185
|
|
|
{ |
186
|
2 |
|
return collect($this->aggregator->getProviders()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @deprecated Use `getProviders()` instead. |
191
|
|
|
*/ |
192
|
1 |
|
public function getProvider() |
193
|
|
|
{ |
194
|
1 |
|
return $this->getProviders()->first(); |
195
|
|
|
} |
196
|
|
|
|
197
|
24 |
|
public function registerProvidersFromConfig(Collection $providers) : self |
198
|
|
|
{ |
199
|
24 |
|
$this->registerProviders($this->getProvidersFromConfiguration($providers)); |
200
|
|
|
|
201
|
24 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function getProvidersFromConfiguration(Collection $providers) : array |
205
|
|
|
{ |
206
|
24 |
|
$providers = $providers->map(function ($arguments, $provider) { |
207
|
24 |
|
$arguments = $this->getArguments($arguments, $provider); |
208
|
24 |
|
$reflection = new ReflectionClass($provider); |
209
|
|
|
|
210
|
24 |
|
if ($provider === 'Geocoder\Provider\Chain\Chain') { |
211
|
24 |
|
return $reflection->newInstance($arguments); |
212
|
|
|
} |
213
|
|
|
|
214
|
24 |
|
return $reflection->newInstanceArgs($arguments); |
215
|
24 |
|
}); |
216
|
|
|
|
217
|
24 |
|
return $providers->toArray(); |
218
|
|
|
} |
219
|
|
|
|
220
|
24 |
|
protected function getArguments(array $arguments, string $provider) : array |
221
|
|
|
{ |
222
|
24 |
|
if ($provider === 'Geocoder\Provider\Chain\Chain') { |
223
|
24 |
|
return $this->getProvidersFromConfiguration( |
224
|
24 |
|
collect(config('geocoder.providers.Geocoder\Provider\Chain\Chain')) |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
24 |
|
$adapter = $this->getAdapterClass($provider); |
229
|
|
|
|
230
|
24 |
|
if ($adapter) { |
231
|
24 |
|
array_unshift($arguments, (new $adapter)); |
232
|
|
|
} |
233
|
|
|
|
234
|
24 |
|
return $arguments; |
235
|
|
|
} |
236
|
|
|
|
237
|
24 |
|
protected function getAdapterClass(string $provider) : string |
238
|
|
|
{ |
239
|
24 |
|
$specificAdapters = collect([ |
240
|
24 |
|
'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter', |
241
|
|
|
'Geocoder\Provider\MaxMindBinary' => null, |
242
|
|
|
]); |
243
|
|
|
|
244
|
24 |
|
if ($specificAdapters->has($provider)) { |
245
|
|
|
return $specificAdapters->get($provider); |
246
|
|
|
} |
247
|
|
|
|
248
|
24 |
|
return config('geocoder.adapter'); |
249
|
|
|
} |
250
|
|
|
|
251
|
19 |
|
protected function removeEmptyCacheEntry(string $cacheKey) |
252
|
|
|
{ |
253
|
19 |
|
$result = app('cache')->get($cacheKey); |
254
|
|
|
|
255
|
19 |
|
if ($result && $result->isEmpty()) { |
256
|
1 |
|
app('cache')->forget($cacheKey); |
257
|
|
|
} |
258
|
19 |
|
} |
259
|
|
|
} |
260
|
|
|
|