예제 #1
0
 /**
  * @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;
 }
예제 #2
0
 /**
  * @param bool $relative
  *
  * @return string
  */
 public function generateUrl(bool $relative = false) : string
 {
     if (!$this->hmacAdded && !$relative && ($sign = DI::config()->getIfExists('googleMaps/secret'))) {
         $binarySign = base64_decode(str_replace(['-', '_'], ['+', '/'], $sign));
         $hmac = hash_hmac('sha1', $this->generateUrl(true), $binarySign, true);
         $this->queries['signature'] = str_replace(['+', '/'], ['-', '_'], base64_encode($hmac));
         $this->hmacAdded = true;
     }
     $uri = new Uri('https://maps.googleapis.com/maps/api/staticmap');
     $uri->addQueries($this->queries);
     $url = $uri->get($relative);
     // google maps expect markers=...&markers=...
     $url = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $url);
     $url = str_replace('%2C', ',', $url);
     return $url;
 }
예제 #3
0
파일: Uri.php 프로젝트: cawaphp/cawa
 /**
  * @param string $uri
  *
  * @return string
  */
 public static function getAbsoluteUri(string $uri = null) : string
 {
     $uri = new Uri($uri);
     return $uri->get(false);
 }
 /**
  * @param string $method
  *
  * @return string
  */
 public function getUri(string $method) : string
 {
     $serviceUri = (string) self::uri('swagger/request', ['renderer' => 'Json', 'version' => $this->getVersion(), 'namespace' => $this->getNamespace(), 'service' => $this->getName(), 'method' => $method]);
     $uri = new Uri();
     $uri->setPath($serviceUri);
     $uri->setQuerystring();
     $uri->setFragment();
     return $uri->get(false);
 }
예제 #5
0
파일: LinkTrait.php 프로젝트: cawaphp/html
 /**
  * @param Uri $uri
  *
  * @return Link
  */
 public function setUri(Uri $uri) : self
 {
     return $this->setHref($uri->get());
 }