1
1
<?php namespace Geocoder \Laravel ;
2
2
3
3
/**
4
- * This file is part of the Geocoder package.
4
+ * This file is part of the Geocoder Laravel package.
5
5
* For the full copyright and license information, please view the LICENSE
6
6
* file that was distributed with this source code.
7
7
*
8
+ * @author Mike Bronner <[email protected] >
8
9
* @license MIT License
9
10
*/
10
11
14
15
use Geocoder \Dumper \Wkb ;
15
16
use Geocoder \Dumper \Wkt ;
16
17
use Geocoder \Geocoder ;
18
+ use Geocoder \Query \GeocodeQuery ;
19
+ use Geocoder \Query \ReverseQuery ;
17
20
use Geocoder \Laravel \Exceptions \InvalidDumperException ;
18
- use Geocoder \Laravel \ProviderAndDumperAggregator ;
19
21
use Geocoder \ProviderAggregator ;
20
- use Geocoder \Model \AddressCollection ;
21
22
use Illuminate \Support \Collection ;
22
23
23
- /**
24
- * @author Mike Bronner <[email protected] >
25
- */
26
24
class ProviderAndDumperAggregator
27
25
{
28
- protected $ results ;
29
26
protected $ aggregator ;
27
+ protected $ results ;
30
28
31
29
public function __construct (int $ limit = Geocoder::DEFAULT_RESULT_LIMIT )
32
30
{
33
31
$ this ->aggregator = new ProviderAggregator ($ limit );
32
+ $ this ->results = collect ();
34
33
}
35
34
35
+ /**
36
+ * @deprecated Use `get()` instead.
37
+ */
36
38
public function all () : array
37
39
{
38
40
return $ this ->results ->all ();
39
41
}
40
42
41
- public function get () : AddressCollection
43
+ public function get () : Collection
42
44
{
43
45
return $ this ->results ;
44
46
}
45
47
46
- public function dump ($ dumper ) : Collection
48
+ public function dump (string $ dumper ) : Collection
47
49
{
48
50
$ dumperClasses = collect ([
49
51
'geojson ' => GeoJson::class,
@@ -70,29 +72,69 @@ public function dump($dumper) : Collection
70
72
});
71
73
}
72
74
73
- public function geocodeQuery ($ query )
75
+ public function geocodeQuery (GeocodeQuery $ query ) : self
74
76
{
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 ;
76
94
}
77
95
78
- public function reverseQuery ($ query )
96
+ public function reverseQuery (ReverseQuery $ query ) : self
79
97
{
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 ;
81
115
}
82
116
83
- public function getName ()
117
+ public function getName () : string
84
118
{
85
119
return $ this ->aggregator ->getName ();
86
120
}
87
121
88
122
public function geocode (string $ value ) : self
89
123
{
90
- $ cacheId = str_slug ($ value );
124
+ $ cacheKey = str_slug ($ value );
91
125
$ this ->results = cache ()->remember (
92
- "geocoder- {$ cacheId }" ,
126
+ "geocoder- {$ cacheKey }" ,
93
127
config ('geocoder.cache-duraction ' , 0 ),
94
128
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 ;
96
138
}
97
139
);
98
140
@@ -106,47 +148,54 @@ public function reverse(float $latitude, float $longitude) : self
106
148
"geocoder- {$ cacheId }" ,
107
149
config ('geocoder.cache-duraction ' , 0 ),
108
150
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 ;
110
159
}
111
160
);
112
161
113
162
return $ this ;
114
163
}
115
164
116
- public function limit ($ limit )
165
+ public function limit (int $ limit ) : self
117
166
{
118
167
$ this ->aggregator ->limit ($ limit );
119
168
120
169
return $ this ;
121
170
}
122
171
123
- public function getLimit ()
172
+ public function getLimit () : int
124
173
{
125
174
return $ this ->aggregator ->getLimit ();
126
175
}
127
176
128
- public function registerProvider ($ provider )
177
+ public function registerProvider (string $ provider ) : self
129
178
{
130
179
$ this ->aggregator ->registerProvider ($ provider );
131
180
132
181
return $ this ;
133
182
}
134
183
135
- public function registerProviders ($ providers = [])
184
+ public function registerProviders (array $ providers = []) : self
136
185
{
137
186
$ this ->aggregator ->registerProviders ($ providers );
138
187
139
188
return $ this ;
140
189
}
141
190
142
- public function using ($ name )
191
+ public function using (string $ name ) : self
143
192
{
144
193
$ this ->aggregator ->using ($ name );
145
194
146
195
return $ this ;
147
196
}
148
197
149
- public function getProviders ()
198
+ public function getProviders () : array
150
199
{
151
200
return $ this ->aggregator ->getProviders ();
152
201
}
0 commit comments