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 <hello@genealabs.com>
89 * @license MIT License
910 */
1011
1415use Geocoder \Dumper \Wkb ;
1516use Geocoder \Dumper \Wkt ;
1617use Geocoder \Geocoder ;
18+ use Geocoder \Query \GeocodeQuery ;
19+ use Geocoder \Query \ReverseQuery ;
1720use Geocoder \Laravel \Exceptions \InvalidDumperException ;
18- use Geocoder \Laravel \ProviderAndDumperAggregator ;
1921use Geocoder \ProviderAggregator ;
20- use Geocoder \Model \AddressCollection ;
2122use Illuminate \Support \Collection ;
2223
23- /**
24- * @author Mike Bronner <hello@genealabs.com>
25- */
2624class 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