mirror of
https://github.com/friendica/friendica
synced 2025-04-19 07:10:11 +00:00
Inherit ApiResponse
from Response
This commit is contained in:
parent
561aba18e3
commit
537b74f307
46 changed files with 326 additions and 277 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue