2020use Geocoder \Laravel \Exceptions \InvalidDumperException ;
2121use Geocoder \ProviderAggregator ;
2222use Illuminate \Support \Collection ;
23+ use ReflectionClass ;
2324
2425/**
2526 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
2627 */
2728class ProviderAndDumperAggregator
2829{
2930 protected $ aggregator ;
31+ protected $ limit ;
3032 protected $ results ;
3133
32- public function __construct (int $ limit = Geocoder:: DEFAULT_RESULT_LIMIT )
34+ public function __construct (int $ limit = null )
3335 {
3436 $ this ->aggregator = new ProviderAggregator ($ limit );
37+ $ this ->limit = $ limit ;
3538 $ this ->results = collect ();
3639 }
3740
@@ -78,7 +81,7 @@ public function dump(string $dumper) : Collection
7881 public function geocodeQuery (GeocodeQuery $ query ) : self
7982 {
8083 $ cacheKey = serialize ($ query );
81- $ this ->results = cache ( )->remember (
84+ $ this ->results = app ( ' cache ' )->remember (
8285 "geocoder- {$ cacheKey }" ,
8386 config ('geocoder.cache-duraction ' , 0 ),
8487 function () use ($ query ) {
@@ -92,7 +95,7 @@ function () use ($query) {
9295 public function reverseQuery (ReverseQuery $ query ) : self
9396 {
9497 $ cacheKey = serialize ($ query );
95- $ this ->results = cache ( )->remember (
98+ $ this ->results = app ( ' cache ' )->remember (
9699 "geocoder- {$ cacheKey }" ,
97100 config ('geocoder.cache-duraction ' , 0 ),
98101 function () use ($ query ) {
@@ -111,7 +114,7 @@ public function getName() : string
111114 public function geocode (string $ value ) : self
112115 {
113116 $ cacheKey = str_slug (strtolower (urlencode ($ value )));
114- $ this ->results = cache ( )->remember (
117+ $ this ->results = app ( ' cache ' )->remember (
115118 "geocoder- {$ cacheKey }" ,
116119 config ('geocoder.cache-duraction ' , 0 ),
117120 function () use ($ value ) {
@@ -125,7 +128,7 @@ function () use ($value) {
125128 public function reverse (float $ latitude , float $ longitude ) : self
126129 {
127130 $ cacheKey = str_slug (strtolower (urlencode ("{$ latitude }- {$ longitude }" )));
128- $ this ->results = cache ( )->remember (
131+ $ this ->results = app ( ' cache ' )->remember (
129132 "geocoder- {$ cacheKey }" ,
130133 config ('geocoder.cache-duraction ' , 0 ),
131134 function () use ($ latitude , $ longitude ) {
@@ -138,14 +141,16 @@ function () use ($latitude, $longitude) {
138141
139142 public function limit (int $ limit ) : self
140143 {
141- $ this ->aggregator ->limit ($ limit );
144+ $ this ->aggregator = new ProviderAggregator (null , $ limit );
145+ $ this ->registerProvidersFromConfig (collect (config ('geocoder.providers ' )));
146+ $ this ->limit = $ limit ;
142147
143148 return $ this ;
144149 }
145150
146151 public function getLimit () : int
147152 {
148- return $ this ->aggregator -> getLimit () ;
153+ return $ this ->limit ;
149154 }
150155
151156 public function registerProvider ($ provider ) : self
@@ -164,7 +169,7 @@ public function registerProviders(array $providers = []) : self
164169
165170 public function using (string $ name ) : self
166171 {
167- $ this ->aggregator ->using ($ name );
172+ $ this ->aggregator = $ this -> aggregator ->using ($ name );
168173
169174 return $ this ;
170175 }
@@ -178,4 +183,58 @@ protected function getProvider()
178183 {
179184 return $ this ->aggregator ->getProvider ();
180185 }
186+
187+ public function registerProvidersFromConfig (Collection $ providers ) : self
188+ {
189+ $ this ->registerProviders ($ this ->getProvidersFromConfiguration ($ providers ));
190+
191+ return $ this ;
192+ }
193+
194+ protected function getProvidersFromConfiguration (Collection $ providers ) : array
195+ {
196+ $ providers = $ providers ->map (function ($ arguments , $ provider ) {
197+ $ arguments = $ this ->getArguments ($ arguments , $ provider );
198+ $ reflection = new ReflectionClass ($ provider );
199+
200+ if ($ provider === 'Geocoder\Provider\Chain\Chain ' ) {
201+ return $ reflection ->newInstance ($ arguments );
202+ }
203+
204+ return $ reflection ->newInstanceArgs ($ arguments );
205+ });
206+
207+ return $ providers ->toArray ();
208+ }
209+
210+ protected function getArguments (array $ arguments , string $ provider ) : array
211+ {
212+ if ($ provider === 'Geocoder\Provider\Chain\Chain ' ) {
213+ return $ this ->getProvidersFromConfiguration (
214+ collect (config ('geocoder.providers.Geocoder\Provider\Chain\Chain ' ))
215+ );
216+ }
217+
218+ $ adapter = $ this ->getAdapterClass ($ provider );
219+
220+ if ($ adapter ) {
221+ array_unshift ($ arguments , (new $ adapter ));
222+ }
223+
224+ return $ arguments ;
225+ }
226+
227+ protected function getAdapterClass (string $ provider ) : string
228+ {
229+ $ specificAdapters = collect ([
230+ 'Geocoder\Provider\GeoIP2 ' => 'Geocoder\Adapter\GeoIP2Adapter ' ,
231+ 'Geocoder\Provider\MaxMindBinary ' => null ,
232+ ]);
233+
234+ if ($ specificAdapters ->has ($ provider )) {
235+ return $ specificAdapters ->get ($ provider );
236+ }
237+
238+ return config ('geocoder.adapter ' );
239+ }
181240}
0 commit comments