/** * @param string $url * @param array $queries * * @throws InvalidRequest * @throws OverQueryLimit * @throws RequestDenied * @throws Unknown * * @return array */ protected static function query(string $url, array $queries = []) : array { if (!self::$client) { self::$client = new HttpClient(); $base = new Uri('https://maps.googleapis.com/maps/api'); self::$client->setBaseUri($base); } if (!isset($queries['key'])) { $queries['key'] = DI::config()->get('googleMaps/apikey'); } $uri = new Uri($url); $uri->addQueries($queries); $response = self::$client->get($uri->get()); $data = json_decode($response->getBody(), true); if (!($data['status'] == 'OK' || $data['status'] == 'ZERO_RESULTS')) { switch ($data['status']) { case 'OVER_QUERY_LIMIT': throw new OverQueryLimit($data['error_message'] ?? $data['status']); case 'REQUEST_DENIED': throw new RequestDenied($data['error_message'] ?? $data['status']); case 'INVALID_REQUEST': throw new InvalidRequest($data['error_message'] ?? $data['status']); default: throw new Unknown($data['error_message'] ?? $data['status']); } } return $data; }
/** * @param string $name * * @return Cache */ private static function httpClient(string $name = null) : Cache { if ($return = DI::get(__METHOD__, $name)) { return $return; } $config = DI::config()->get('httpclient/' . $name); if (is_callable($config)) { $item = $config(); } else { $item = new HttpClient(); $item->setBaseUri($config); } return DI::set(__METHOD__, $name, $item); }