1
|
|
|
<?php namespace Geocoder\Laravel; |
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\Dumper\GeoJson; |
12
|
|
|
use Geocoder\Dumper\Gpx; |
13
|
|
|
use Geocoder\Dumper\Kml; |
14
|
|
|
use Geocoder\Dumper\Wkb; |
15
|
|
|
use Geocoder\Dumper\Wkt; |
16
|
|
|
use Geocoder\Geocoder; |
17
|
|
|
use Geocoder\Laravel\Exceptions\InvalidDumperException; |
18
|
|
|
use Geocoder\Laravel\ProviderAndDumperAggregator; |
19
|
|
|
use Geocoder\ProviderAggregator; |
20
|
|
|
use Geocoder\Model\AddressCollection; |
21
|
|
|
use Illuminate\Support\Collection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Mike Bronner <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ProviderAndDumperAggregator |
27
|
|
|
{ |
28
|
|
|
protected $results; |
29
|
|
|
protected $aggregator; |
30
|
|
|
|
31
|
|
|
public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT) |
32
|
|
|
{ |
33
|
|
|
$this->aggregator = new ProviderAggregator($limit); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function all() : array |
37
|
|
|
{ |
38
|
|
|
return $this->results->all(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function get() : AddressCollection |
42
|
|
|
{ |
43
|
|
|
return $this->results; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function dump($dumper) : Collection |
47
|
|
|
{ |
48
|
|
|
$dumperClasses = collect([ |
49
|
|
|
'geojson' => GeoJson::class, |
50
|
|
|
'gpx' => Gpx::class, |
51
|
|
|
'kml' => Kml::class, |
52
|
|
|
'wkb' => Wkb::class, |
53
|
|
|
'wkt' => Wkt::class, |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
if (!$dumperClasses->has($dumper)) { |
57
|
|
|
$errorMessage = implode('', [ |
58
|
|
|
"The dumper specified ('{$dumper}') is invalid. Valid dumpers ", |
59
|
|
|
"are: geojson, gpx, kml, wkb, wkt.", |
60
|
|
|
]); |
61
|
|
|
throw new InvalidDumperException($errorMessage); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$dumperClass = $dumperClasses->get($dumper); |
65
|
|
|
$dumper = new $dumperClass; |
66
|
|
|
$results = collect($this->results->all()); |
67
|
|
|
|
68
|
|
|
return $results->map(function ($result) use ($dumper) { |
69
|
|
|
return $dumper->dump($result); |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function geocodeQuery($query) |
74
|
|
|
{ |
75
|
|
|
return $this->aggregator->geocodeQuery($query); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function reverseQuery($query) |
79
|
|
|
{ |
80
|
|
|
return $this->aggregator->reverseQuery($query); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getName() |
84
|
|
|
{ |
85
|
|
|
return $this->aggregator->getName(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function geocode(string $value) : self |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$cacheId = str_slug($value); |
91
|
|
|
$this->results = cache()->remember( |
92
|
|
|
"geocoder-{$cacheId}", |
93
|
|
|
config('geocoder.cache-duraction', 0), |
94
|
|
|
function () use ($value) { |
95
|
|
|
return $this->aggregator->geocode($value); |
96
|
|
|
} |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function reverse(float $latitude, float $longitude) : self |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$cacheId = str_slug("{$latitude}-{$longitude}"); |
105
|
|
|
$this->results = cache()->remember( |
106
|
|
|
"geocoder-{$cacheId}", |
107
|
|
|
config('geocoder.cache-duraction', 0), |
108
|
|
|
function () use ($latitude, $longitude) { |
109
|
|
|
return $this->aggregator->reverse($latitude, $longitude); |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function limit($limit) |
117
|
|
|
{ |
118
|
|
|
$this->aggregator->limit($limit); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getLimit() |
124
|
|
|
{ |
125
|
|
|
return $this->aggregator->getLimit(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function registerProvider($provider) |
129
|
|
|
{ |
130
|
|
|
$this->aggregator->registerProvider($provider); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function registerProviders($providers = []) |
136
|
|
|
{ |
137
|
|
|
$this->aggregator->registerProviders($providers); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function using($name) |
143
|
|
|
{ |
144
|
|
|
$this->aggregator->using($name); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getProviders() |
150
|
|
|
{ |
151
|
|
|
return $this->aggregator->getProviders(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function getProvider() |
155
|
|
|
{ |
156
|
|
|
return $this->aggregator->getProvider(); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.