2021-11-21 19:06:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
2021-11-21 20:52:36 +00:00
|
|
|
use Friendica\Capabilities\ICanCreateResponses;
|
2021-11-21 19:06:36 +00:00
|
|
|
use Friendica\Capabilities\IRespondToRequests;
|
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
|
|
|
|
2021-11-21 20:52:36 +00:00
|
|
|
class Response implements ICanCreateResponses
|
2021-11-21 19:06:36 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-11-21 20:52:36 +00:00
|
|
|
* @var string[]
|
2021-11-21 19:06:36 +00:00
|
|
|
*/
|
|
|
|
protected $headers = [];
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $content = '';
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-11-21 20:52:36 +00:00
|
|
|
protected $type = IRespondToRequests::TYPE_HTML;
|
2021-11-21 19:06:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 20:52:36 +00:00
|
|
|
public function setHeader(?string $header = null, ?string $key = null): void
|
2021-11-21 19:06:36 +00:00
|
|
|
{
|
2021-11-21 20:52:36 +00:00
|
|
|
if (!isset($header) && !empty($key)) {
|
|
|
|
unset($this->headers[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($header)) {
|
|
|
|
if (empty($key)) {
|
|
|
|
$this->headers[] = $header;
|
|
|
|
} else {
|
|
|
|
$this->headers[$key] = $header;
|
|
|
|
}
|
|
|
|
}
|
2021-11-21 19:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 20:52:36 +00:00
|
|
|
public function addContent($content): void
|
2021-11-21 19:06:36 +00:00
|
|
|
{
|
|
|
|
$this->content .= $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getHeaders(): array
|
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 20:52:36 +00:00
|
|
|
public function getContent()
|
2021-11-21 19:06:36 +00:00
|
|
|
{
|
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 20:52:36 +00:00
|
|
|
public function setType(string $type, ?string $content_type = null): void
|
2021-11-21 19:06:36 +00:00
|
|
|
{
|
|
|
|
if (!in_array($type, IRespondToRequests::ALLOWED_TYPES)) {
|
|
|
|
throw new InternalServerErrorException('wrong type');
|
|
|
|
}
|
|
|
|
|
2021-11-21 20:52:36 +00:00
|
|
|
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');
|
|
|
|
|
2021-11-21 19:06:36 +00:00
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 20:52:36 +00:00
|
|
|
public function getType(): string
|
2021-11-21 19:06:36 +00:00
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
}
|