| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Geocoder package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license    MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Geocoder\Provider\Chain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Geocoder\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Geocoder\Model\AddressCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Geocoder\Query\GeocodeQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Geocoder\Query\ReverseQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Geocoder\Provider\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Psr\Log\LoggerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Psr\Log\LoggerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Psr\Log\LogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author Markus Bachmann <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | final class Chain implements Provider, LoggerAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     use LoggerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $geocodeQueryLogLevel = LogLevel::ALERT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $reverseQueryLogLevel = LogLevel::ALERT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var Provider[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $providers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param Provider[] $providers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |     public function __construct(array $providers = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 4 |  |         $this->providers = $providers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function setGeocodeQueryLogLevel(string $level) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->geocodeQueryLogLevel = $level; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function setReverseQueryLogLevel(string $level) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->reverseQueryLogLevel = $level; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |     public function geocodeQuery(GeocodeQuery $query): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         foreach ($this->providers as $provider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |                 $result = $provider->geocodeQuery($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |                 if (!$result->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |                     return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |                 $this->log( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                     $this->geocodeQueryLogLevel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |                     'Provider "{providerName}" could not geocode address: "{address}".', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |                         'exception' => $e, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |                         'providerName' => $provider->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |                         'address' => $query->getText(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return new AddressCollection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     public function reverseQuery(ReverseQuery $query): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         foreach ($this->providers as $provider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |                 $result = $provider->reverseQuery($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |                 if (!$result->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |                     return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |             } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |                 $coordinates = $query->getCoordinates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |                 $this->log( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |                     $this->reverseQueryLogLevel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |                     sprintf('Provider "%s" could reverse coordinates: %f, %f.', $provider->getName(), $coordinates->getLatitude(), $coordinates->getLongitude()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |                     ['exception' => $e] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return new AddressCollection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |     public function getName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |         return 'chain'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Adds a provider. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param Provider $provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @return Chain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |     public function add(Provider $provider): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |         $this->providers[] = $provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param $level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param array $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  |     private function log($level, $message, array $context = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  |         if ($this->logger) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $this->logger->log($level, $message, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  |