X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FResponse.php;h=ba7d4320271d275ed108a1a7d1167fa85139418a;hb=99284222c1d7fb4adca9077e3057faf3b36f7180;hp=87fbbf07cc81e4579e0d99ea2d57f6ab0b87a5fd;hpb=561aba18e3a230c0912ad9483c6df43cc40e09d6;p=friendica.git diff --git a/src/Module/Response.php b/src/Module/Response.php index 87fbbf07cc..ba7d432027 100644 --- a/src/Module/Response.php +++ b/src/Module/Response.php @@ -1,15 +1,34 @@ . + * + */ namespace Friendica\Module; -use Friendica\Capabilities\ICanReadAndWriteToResponds; -use Friendica\Capabilities\IRespondToRequests; +use Friendica\Capabilities\ICanCreateResponses; use Friendica\Network\HTTPException\InternalServerErrorException; +use Psr\Http\Message\ResponseInterface; -class Response implements ICanReadAndWriteToResponds +class Response implements ICanCreateResponses { /** - * @var string[][] + * @var string[] */ protected $headers = []; /** @@ -19,20 +38,34 @@ class Response implements ICanReadAndWriteToResponds /** * @var string */ - protected $type = IRespondToRequests::TYPE_CONTENT; + protected $type = self::TYPE_HTML; + + protected $status = 200; + + protected $reason = null; /** * {@inheritDoc} */ - public function addHeader(string $key, string $value) + public function setHeader(?string $header = null, ?string $key = null): void { - $this->headers[$key][] = $value; + if (!isset($header) && !empty($key)) { + unset($this->headers[$key]); + } + + if (isset($header)) { + if (empty($key)) { + $this->headers[] = $header; + } else { + $this->headers[$key] = $header; + } + } } /** * {@inheritDoc} */ - public function addContent(string $content) + public function addContent($content): void { $this->content .= $content; } @@ -48,7 +81,7 @@ class Response implements ICanReadAndWriteToResponds /** * {@inheritDoc} */ - public function getContent(): string + public function getContent() { return $this->content; } @@ -56,20 +89,60 @@ class Response implements ICanReadAndWriteToResponds /** * {@inheritDoc} */ - public function setType(string $type) + public function setType(string $type, ?string $content_type = null): void { - if (!in_array($type, IRespondToRequests::ALLOWED_TYPES)) { + if (!in_array($type, static::ALLOWED_TYPES)) { throw new InternalServerErrorException('wrong type'); } + switch ($type) { + case static::TYPE_HTML: + $content_type = $content_type ?? 'text/html; charset=utf-8'; + break; + case static::TYPE_JSON: + $content_type = $content_type ?? 'application/json'; + break; + case static::TYPE_XML: + $content_type = $content_type ?? 'text/xml'; + break; + case static::TYPE_RSS: + $content_type = $content_type ?? 'application/rss+xml'; + break; + case static::TYPE_ATOM: + $content_type = $content_type ?? 'application/atom+xml'; + break; + } + + $this->setHeader($content_type, 'Content-type'); + $this->type = $type; } /** * {@inheritDoc} */ - public function getTyp(): string + public function setStatus(int $status = 200, ?string $reason = null): void + { + $this->status = $status; + $this->reason = $reason; + } + + /** + * {@inheritDoc} + */ + public function getType(): string { return $this->type; } + + /** + * {@inheritDoc} + */ + public function generate(): ResponseInterface + { + // Setting the response type as an X-header for direct usage + $this->headers[static::X_HEADER] = $this->type; + + return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, '1.1', $this->reason); + } }