コード例 #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
ファイル: MapStatic.php プロジェクト: cawaphp/widget
 /**
  * @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
ファイル: Column.php プロジェクト: cawaphp/bootstrap
 /**
  * {@inheritdoc}
  */
 public function __construct($id, $name = null)
 {
     parent::__construct($id, $name);
     $this->argsCallback = function (self $caller, string $sort = null) {
         if ($sort) {
             return Uri::parse()->addQuery(self::QUERY_SORT, $sort)->get();
         } else {
             return Uri::parse()->getQuery(self::QUERY_SORT);
         }
     };
 }
コード例 #4
0
ファイル: Pagination.php プロジェクト: cawaphp/bootstrap
 /**
  * @param int $page
  * @param callable $argsCallback
  */
 public function __construct(int $page, callable $argsCallback = null)
 {
     parent::__construct('<nav>');
     $this->page = $page;
     if ($argsCallback) {
         $this->argsCallback = $argsCallback;
     } else {
         $this->argsCallback = function (self $caller, int $page = null) {
             if ($page) {
                 return Uri::parse()->addQuery(self::QUERY_PAGE, (string) $page)->get();
             } else {
                 return (int) Uri::parse()->getQuery(self::QUERY_PAGE);
             }
         };
     }
     $this->current = call_user_func($this->argsCallback, $this) ?: 1;
     $this->ul = new HtmlContainer('<ul>');
     $this->ul->addClass('pagination');
 }
コード例 #5
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);
 }
コード例 #6
0
ファイル: Request.php プロジェクト: cawaphp/cawa
 /**
  * @return array
  */
 public function getQueries() : array
 {
     return $this->uri->getQueries();
 }
コード例 #7
0
ファイル: UriTest.php プロジェクト: cawaphp/cawa
 /**
  * Test the fragment extract
  *
  * @param string $uriString
  * @param array $parts
  * @dataProvider validUriStringProviderWithPart
  */
 public function testFragment(string $uriString, array $parts)
 {
     $uri = new Uri($uriString);
     if (isset($parts['fragment'])) {
         $this->assertEquals($parts['fragment'], $uri->getFragment());
     } else {
         $this->assertNull($uri->getFragment());
     }
 }
コード例 #8
0
ファイル: MailerFactory.php プロジェクト: cawaphp/email
 /**
  * @param string $name
  *
  * @return \Swift_Mailer
  */
 private static function mailer(string $name = null) : \Swift_Mailer
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $config = DI::config()->getIfExists('email/' . ($name ?: 'default'));
     if (!$config) {
         $transport = \Swift_MailTransport::newInstance();
         $return = \Swift_Mailer::newInstance($transport);
     } elseif (is_callable($config)) {
         $return = $config();
     } else {
         $uri = new Uri($config);
         switch ($uri->getScheme()) {
             case 'smtp':
                 $transport = \Swift_SmtpTransport::newInstance($uri->getHost(), $uri->getPort());
                 if ($uri->getUser()) {
                     $transport->setUsername($uri->getUser());
                 }
                 if ($uri->getPassword()) {
                     $transport->setPassword($uri->getPassword());
                 }
                 if ($uri->getQuery('auth')) {
                     $transport->setAuthMode($uri->getQuery('auth'));
                 }
                 if ($uri->getQuery('encryption')) {
                     $transport->setEncryption($uri->getQuery('encryption'));
                 }
                 break;
             case 'echo':
                 $transport = EchoTransport::newInstance();
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf("Undefined email mailer type '%s'", $uri->getScheme()));
                 break;
         }
         $return = \Swift_Mailer::newInstance($transport);
         if ($uri->getQuery('plugins')) {
             foreach ($uri->getQuery('plugins') as $plugin) {
                 $return->registerPlugin(new $plugin());
             }
         }
     }
     return DI::set(__METHOD__, $name, $return);
 }
コード例 #9
0
ファイル: Grid.php プロジェクト: cawaphp/bootstrap
 /**
  * @param $filter
  *
  * @return $this|self
  */
 public function addFilter(AbstractField $filter) : self
 {
     if (!$this->filtersForm) {
         $this->filtersForm = new Form();
         $this->filtersForm->setMethod('GET')->setName($this->stateId ? $this->stateId : 'grid')->setAction(self::request()->getUri()->get());
         $this->navbar->add($this->filtersForm);
     }
     if ($this->stateId) {
         $filter->setName($this->stateId . '_' . $filter->getName());
     }
     $this->filtersForm->add($filter);
     $this->filtersForm->setAction(Uri::parse($this->filtersForm->getAction())->removeQuery($filter->getName())->get());
     if ($filter->getLabel()) {
         $filter->getLabel()->addClass('sr-only');
     }
     return $this;
 }
コード例 #10
0
 /**
  * @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);
 }
コード例 #11
0
ファイル: Router.php プロジェクト: cawaphp/cawa
 /**
  * @param string $name
  * @param array $data
  * @param bool $warnData
  *
  * @return Uri
  */
 public function getUri(string $name, array $data = [], $warnData = true) : Uri
 {
     if (!isset($this->routes[$name])) {
         throw new \InvalidArgumentException(sprintf("Invalid route name '%s'", $name));
     }
     $route = $this->routes[$name];
     $uri = new Uri();
     $uri->removeAllQueries()->setFragment(null)->setPath($this->routeRegexp($route, $data));
     // $cloneData = $data;
     // append querystring
     if ($route->getUserInputs()) {
         $queryToAdd = [];
         foreach ($route->getUserInputs() as $querystring) {
             if (!isset($data[$querystring->getName()]) && $querystring->isMandatory() && $warnData && $route->getMethod() != 'POST' && $route->getMethod() != 'PUT' && $route->getMethod() != 'DELETE') {
                 throw new \InvalidArgumentException(sprintf("Missing querystring '%s' to generate route '%s'", $querystring->getName(), $route->getName()));
             }
             if (isset($data[$querystring->getName()])) {
                 // unset($cloneData[$querystring->getName()]);
                 $queryToAdd[$querystring->getName()] = $data[$querystring->getName()];
             }
         }
         $uri->addQueries($queryToAdd);
     }
     /*
     // Args can be consumed by routeRegexp and not remove from cloneData
     if (sizeof($cloneData)) {
         throw new \InvalidArgumentException(sprintf(
             "Too many data to generate route '%s' with keys %s",
             $route->getName(),
             "'" . implode("', '", array_keys($cloneData)) . "'"
         ));
     }
     */
     return $uri;
 }
コード例 #12
0
ファイル: LinkTrait.php プロジェクト: cawaphp/html
 /**
  * @param Uri $uri
  *
  * @return Link
  */
 public function setUri(Uri $uri) : self
 {
     return $this->setHref($uri->get());
 }
コード例 #13
0
ファイル: Photo.php プロジェクト: cawaphp/google-maps
 /**
  * @param int $maxWidth
  * @param int $maxHeight
  *
  * @return Uri
  */
 public function getUrl(int $maxWidth = null, int $maxHeight = null) : Uri
 {
     if ($maxWidth && $maxHeight) {
         throw new \InvalidArgumentException("You can't specify maxWidth AND maxHeight");
     }
     $queries = ['photoreference' => $this->reference, 'key' => DI::config()->get('googleMaps/apikey')];
     if ($maxWidth) {
         $queries['maxwidth'] = (string) $maxWidth;
     }
     if ($maxHeight) {
         $queries['maxheight'] = (string) $maxHeight;
     }
     $uri = new Uri('https://maps.googleapis.com/maps/api/place/photo');
     $uri->addQueries($queries);
     return $uri;
 }
コード例 #14
0
ファイル: Form.php プロジェクト: cawaphp/html
 /**
  *
  */
 protected function alterBeforeRender()
 {
     if ($this->csrf) {
         $this->addCsrf();
     }
     // remove all questring of current form
     if ($this->getMethod() == 'GET') {
         $this->add(new Hidden($this->getName(), '1'));
         $uri = new Uri($this->getAction());
         if ($uri->getQueries()) {
             $remove = [];
             foreach ($uri->getQueries() as $key => $value) {
                 if (isset($this->values[$key]) || isset($this->values[$key . '[]'])) {
                     $remove[] = $key;
                 }
             }
             $this->setAction((string) $uri->removeQueries($remove));
         }
     }
 }