コード例 #1
0
ファイル: Response.php プロジェクト: froq/froq-beta-archive
 /**
  * Set body.
  *
  * @param  mixed $body
  * @return self
  */
 public final function setBody($body) : self
 {
     switch ($this->contentType) {
         // handle xml @todo
         case ContentType::XML:
             break;
             // handle json
         // handle json
         case ContentType::JSON:
             $json = new Json($body);
             // simply check for pretty print
             $app = app();
             if (is_in($app->request->params->get['pp'], ['1', 'true'])) {
                 $body = $json->encode(JSON_PRETTY_PRINT);
             } else {
                 $body = $json->encode();
             }
             if ($json->hasError()) {
                 throw new JsonException($json->getErrorMessage(), $json->getErrorCode());
             }
             break;
             // handle html
         // handle html
         case ContentType::HTML:
             // check for page title
             if ($pageTitle = get_global('page.title')) {
                 $body = preg_replace('~<title>(.*?)</title>~s', '<title>' . html_encode($pageTitle) . '</title>', $body, 1);
             }
             // check page description
             if ($pageDescription = get_global('page.description')) {
                 $body = preg_replace('~<meta\\s+name="description"\\s+content="(.*?)">~', '<meta\\s+name="description"\\s+content="' . html_encode($pageDescription) . '">', $body, 1);
             }
             break;
     }
     // can gzip?
     if (!empty($this->gzipOptions)) {
         $this->gzip->setData($body);
         if ($this->gzip->isDataMinlenOK()) {
             $body = $this->gzip->encode();
             $this->setHeader('Vary', 'Accept-Encoding');
             $this->setHeader('Content-Encoding', 'gzip');
         }
     }
     // content length
     $this->setContentLength(strlen($body));
     $this->body = $body;
     return $this;
 }
コード例 #2
0
ファイル: Response.php プロジェクト: froq/froq-http
 /**
  * Set body.
  * @param  any $body
  * @return self
  */
 public final function setBody($body) : self
 {
     switch ($this->body->content->getType()) {
         case BodyContent::TYPE_XML:
             // @todo
             break;
         case BodyContent::TYPE_JSON:
             $json = new Json($body);
             $body = $json->encode();
             if ($json->hasError()) {
                 throw new JsonException($json->getErrorMessage(), $json->getErrorCode());
             }
             break;
     }
     // gzip
     if (!empty($this->gzipOptions)) {
         $this->gzip->setData($body);
         if ($this->gzip->checkDataMinlen()) {
             $body = $this->gzip->encode();
             $this->setHeader('Vary', 'Accept-Encoding');
             $this->setHeader('Content-Encoding', 'gzip');
         }
     }
     $this->body->content->setData($body);
     $this->body->content->setLength(strlen($body));
     return $this;
 }