3 namespace Friendica\Module;
5 use Friendica\Capabilities\ICanCreateResponses;
6 use Friendica\Network\HTTPException\InternalServerErrorException;
7 use Psr\Http\Message\ResponseInterface;
9 class Response implements ICanCreateResponses
14 protected $headers = [];
18 protected $content = '';
22 protected $type = self::TYPE_HTML;
27 public function setHeader(?string $header = null, ?string $key = null): void
29 if (!isset($header) && !empty($key)) {
30 unset($this->headers[$key]);
35 $this->headers[] = $header;
37 $this->headers[$key] = $header;
45 public function addContent($content): void
47 $this->content .= $content;
53 public function getHeaders(): array
55 return $this->headers;
61 public function getContent()
63 return $this->content;
69 public function setType(string $type, ?string $content_type = null): void
71 if (!in_array($type, static::ALLOWED_TYPES)) {
72 throw new InternalServerErrorException('wrong type');
76 case static::TYPE_JSON:
77 $content_type = $content_type ?? 'application/json';
79 case static::TYPE_XML:
80 $content_type = $content_type ?? 'text/xml';
82 case static::TYPE_RSS:
83 $content_type = $content_type ?? 'application/rss+xml';
85 case static::TYPE_ATOM:
86 $content_type = $content_type ?? 'application/atom+xml';
90 $this->setHeader($content_type, 'Content-type');
98 public function getType(): string
106 public function generate(): ResponseInterface
108 // Setting the response type as an X-header for direct usage
109 $this->headers[static::X_HEADER] = $this->type;
111 return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);