Пример #1
0
 /**
  * @return \Geocoder\ProviderAggregator
  * @throws RuntimeException
  */
 public static function getGeocoder()
 {
     if (!self::$addressGeocoder) {
         $adapter = self::config()->adapter;
         if (!class_exists($adapter)) {
             throw new RuntimeException("Adapter class {$adapter} is not defined");
         }
         $adaperOptions = self::config()->adapter_options;
         $configuration = new \Ivory\HttpAdapter\Configuration();
         foreach ($adaperOptions as $adapterParam => $adapterValue) {
             $method = 'set' . ucfirst($adapterParam);
             $configuration->{$method}($adapterValue);
         }
         $geocoder = new \Geocoder\ProviderAggregator();
         $adapterInstance = new $adapter($configuration);
         $providers = self::config()->providers;
         foreach ($providers as $provider => $params) {
             if (isset($params['locale'])) {
                 $params['locale'] = i18n::get_locale();
             }
             array_unshift($params, $adapterInstance);
             //put the adapter as the first param
             $class = '\\Geocoder\\Provider\\' . $provider;
             if (!class_exists($class)) {
                 throw new RuntimeException("Provider class {$class} is not defined");
             }
             $reflectionClass = new ReflectionClass($class);
             $providerInstance = $reflectionClass->newInstanceArgs($params);
             $geocoder->registerProvider($providerInstance);
         }
         self::$addressGeocoder = $geocoder;
     }
     return self::$addressGeocoder;
 }
 public function get()
 {
     $context = Context::factory();
     // get default parameters from config
     $config = $context->ns('geocodit');
     $defaultProfile = $config->getValue('trust', 'geocodit', $this->VALID_PROFILE());
     $defaultAddress = $config->getValue('defaultAddress', 'Via Montefiori 13, Esino Lario', FILTER_SANITIZE_STRING);
     $penality = $config->getValue('penality', 2, $this->VALID_PENALITY());
     // get input patrameters from URL quesry string
     $input = $context->ns(INPUT_GET);
     $query = $input->getValue('q', $defaultAddress, FILTER_SANITIZE_STRING);
     $profile = $input->getValue('trust', $defaultProfile, self::VALID_PROFILE());
     $geocoder = new \Geocoder\ProviderAggregator();
     $adapter = new \Ivory\HttpAdapter\CurlHttpAdapter();
     // chain all supported providers
     $chain = new \Geocoder\Provider\Chain(array());
     foreach ($this->PROFILES[$profile] as $providerName) {
         $provider = $this->geocoderFactory($adapter, trim($providerName));
         $chain->add($provider);
     }
     $geocoder->registerProvider($chain);
     // Call toponym resolution providers
     $address = $geocoder->limit(1)->geocode($query)->first();
     // apply penality (just to avoid abuse, set to 0 in config file to disable)
     if ($penality > 0) {
         usleep($penality * 1000000);
     }
     return $this->stateTransfer($address);
 }