/** * Get address from requested param * * @param string $param * @throws \Sonar\Exceptions\GeoServiceException * @return array */ public function location($param) { try { $result = $this->geocoder->geocode($param); // parse data from geo locator return (new Geo($result))->toArray(); } catch (\Exception $e) { throw new GeoServiceException($e->getMessage()); } }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, ClientIp::KEY)) { throw new RuntimeException('Geolocate middleware needs ClientIp executed before'); } $ip = ClientIp::getIp($request); if ($ip !== null) { $request = Middleware::setAttribute($request, self::KEY, $this->geocoder->geocode($ip)); } return $next($request, $response); }
/** * {@inheritdoc} */ public function geocode($request) { if ($this->getProvider() instanceof GeocoderProvider) { return $this->getProvider()->getGeocodedData($request); } return parent::geocode($request); }
/** * {@inheritdoc} */ public function geocode($value) { if (null === $this->logger) { return parent::geocode($value); } $startTime = microtime(true); $results = parent::geocode($value); $duration = (microtime(true) - $startTime) * 1000; $this->logger->logRequest(sprintf("[Geocoding] %s", $value), $duration, $this->getProviderClass(), $results); return $results; }
protected function tryToGetAddressFromGeocode($address, $attempts = 0) { if ($attempts >= 3) { return; } $result = null; /** * @var \Geocoder\Model\Address */ try { $result = $this->geocoder->geocode($address)->first(); return $result; } catch (\Geocoder\Exception\NoResult $no_result) { $this->writeString('x'); return; } catch (\ErrorException $response_error) { $this->tryToGetAddressFromGeocode($address, ++$attempts); } return $result; }
protected function execute(InputInterface $input, OutputInterface $output) { $geocoder = new Geocoder(); $adapter = $this->getAdapter($input->getOption('adapter')); $provider = $this->getProvider($input->getOption('provider')); if ($input->getOption('args')) { $args = is_array($input->getOption('args')) ? implode(',', $input->getOption('args')) : $input->getOption('args'); $geocoder->registerProvider(new $provider(new $adapter(), $args)); } else { $geocoder->registerProvider(new $provider(new $adapter())); } $geocoded = $geocoder->geocode($input->getArgument('value')); if ($input->getOption('raw')) { $result = array(); $result[] = sprintf('<label>Adapter</label>: <value>%s</value>', $adapter); $result[] = sprintf('<label>Provider</label>: <value>%s</value>', $provider); if ($input->getOption('args')) { $result[] = sprintf('<label>Arguments</label>: <value>%s</value>', $args); } $result[] = '---'; $result[] = sprintf('<label>Latitude</label>: <value>%s</value>', $geocoded->getLatitude()); $result[] = sprintf('<label>Longitude</label>: <value>%s</value>', $geocoded->getLongitude()); if (null !== ($bounds = $geocoded->getBounds())) { $result[] = '<label>Bounds</label>'; $result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']); $result[] = sprintf(' - <label>West</label>: <value>%s</value>', $bounds['west']); $result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']); $result[] = sprintf(' - <label>East</label>: <value>%s</value>', $bounds['east']); } $result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $geocoded->getStreetNumber()); $result[] = sprintf('<label>Street Name</label>: <value>%s</value>', $geocoded->getStreetName()); $result[] = sprintf('<label>Zipcode</label>: <value>%s</value>', $geocoded->getZipcode()); $result[] = sprintf('<label>City</label>: <value>%s</value>', $geocoded->getCity()); $result[] = sprintf('<label>City District</label>: <value>%s</value>', $geocoded->getCityDistrict()); $result[] = sprintf('<label>County</label>: <value>%s</value>', $geocoded->getCounty()); $result[] = sprintf('<label>County Code</label>: <value>%s</value>', $geocoded->getCountyCode()); $result[] = sprintf('<label>Region</label>: <value>%s</value>', $geocoded->getRegion()); $result[] = sprintf('<label>Region Code</label>: <value>%s</value>', $geocoded->getRegionCode()); $result[] = sprintf('<label>Country</label>: <value>%s</value>', $geocoded->getCountry()); $result[] = sprintf('<label>Country Code</label>: <value>%s</value>', $geocoded->getCountryCode()); $result[] = sprintf('<label>Timezone</label>: <value>%s</value>', $geocoded->getTimezone()); } elseif ($input->getOption('json')) { $result = sprintf('<value>%s</value>', json_encode($geocoded->toArray())); } elseif ($input->getOption('dumper')) { $dumper = $this->getDumper($input->getOption('dumper')); $dumper = new $dumper(); $result = sprintf('<value>%s</value>', $dumper->dump($geocoded)); } else { $result = sprintf('<value>%s, %s</value>', $geocoded->getLatitude(), $geocoded->getLongitude()); } $output->writeln($result); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!self::hasAttribute($request, ClientIp::KEY)) { throw new RuntimeException('Geolocate middleware needs ClientIp executed before'); } $ip = ClientIp::getIp($request); if ($ip !== null) { if ($this->saveInSession) { $ips =& self::getStorage($request, self::KEY); if (isset($ips[$ip])) { $address = new AddressCollection($ips[$ip]); } else { $address = $this->geocoder->geocode($ip); $ips[$ip] = $address->all(); } } else { $address = $this->geocoder->geocode($ip); } $request = self::setAttribute($request, self::KEY, $address); } return $next($request, $response); }
/** * {@inheritDoc} * * @static */ public static function geocode($value) { return \Geocoder\Geocoder::geocode($value); }
protected function geocodeAddress() { $this->geocoderResult = $this->geocoder->geocode($this->address); return true; }
/** * Get geo mockup data * * @return \Geocoder\Model\AddressCollection */ public function getMockData() { return $this->geocoder->geocode(self::IP_ADDRESS); }
function it_throws_exception_if_country_is_not_geolocated(Geocoder $geocoder, Money $money) { $geocoder->geocode(Argument::any())->shouldBeCalled()->willReturn(new AddressCollection()); $this->shouldThrow('Locurro\\Exception\\GeolocationException')->duringConvert($money, 'umpirsky'); }