Inherit ApiResponse from Response

This commit is contained in:
Philipp 2021-11-21 21:52:36 +01:00
parent 561aba18e3
commit 537b74f307
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
46 changed files with 326 additions and 277 deletions

View file

@ -2,14 +2,14 @@
namespace Friendica\Module;
use Friendica\Capabilities\ICanReadAndWriteToResponds;
use Friendica\Capabilities\ICanCreateResponses;
use Friendica\Capabilities\IRespondToRequests;
use Friendica\Network\HTTPException\InternalServerErrorException;
class Response implements ICanReadAndWriteToResponds
class Response implements ICanCreateResponses
{
/**
* @var string[][]
* @var string[]
*/
protected $headers = [];
/**
@ -19,20 +19,30 @@ class Response implements ICanReadAndWriteToResponds
/**
* @var string
*/
protected $type = IRespondToRequests::TYPE_CONTENT;
protected $type = IRespondToRequests::TYPE_HTML;
/**
* {@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 +58,7 @@ class Response implements ICanReadAndWriteToResponds
/**
* {@inheritDoc}
*/
public function getContent(): string
public function getContent()
{
return $this->content;
}
@ -56,19 +66,31 @@ 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)) {
throw new InternalServerErrorException('wrong type');
}
switch ($type) {
case static::TYPE_JSON:
$content_type = $content_type ?? 'application/json';
break;
case static::TYPE_XML:
$content_type = $content_type ?? 'text/xml';
break;
}
$this->setHeader($content_type, 'Content-type');
$this->type = $type;
}
/**
* {@inheritDoc}
*/
public function getTyp(): string
public function getType(): string
{
return $this->type;
}