2021-11-21 14:06:36 -05:00
|
|
|
<?php
|
2022-01-02 04:49:50 -05:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2021-11-21 14:06:36 -05:00
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
2021-11-21 15:52:36 -05:00
|
|
|
use Friendica\Capabilities\ICanCreateResponses;
|
2021-11-21 14:06:36 -05:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2021-11-21 17:37:17 -05:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2021-11-21 14:06:36 -05:00
|
|
|
|
2021-11-21 15:52:36 -05:00
|
|
|
class Response implements ICanCreateResponses
|
2021-11-21 14:06:36 -05:00
|
|
|
{
|
|
|
|
/**
|
2021-11-21 15:52:36 -05:00
|
|
|
* @var string[]
|
2021-11-21 14:06:36 -05:00
|
|
|
*/
|
|
|
|
protected $headers = [];
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $content = '';
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-11-21 18:07:09 -05:00
|
|
|
protected $type = self::TYPE_HTML;
|
2021-11-21 14:06:36 -05:00
|
|
|
|
2022-01-02 15:28:28 -05:00
|
|
|
protected $status = 200;
|
|
|
|
|
|
|
|
protected $reason = null;
|
|
|
|
|
2021-11-21 14:06:36 -05:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 15:52:36 -05:00
|
|
|
public function setHeader(?string $header = null, ?string $key = null): void
|
2021-11-21 14:06:36 -05:00
|
|
|
{
|
2021-11-21 15:52:36 -05: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 14:06:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 15:52:36 -05:00
|
|
|
public function addContent($content): void
|
2021-11-21 14:06:36 -05:00
|
|
|
{
|
|
|
|
$this->content .= $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getHeaders(): array
|
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 15:52:36 -05:00
|
|
|
public function getContent()
|
2021-11-21 14:06:36 -05:00
|
|
|
{
|
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 15:52:36 -05:00
|
|
|
public function setType(string $type, ?string $content_type = null): void
|
2021-11-21 14:06:36 -05:00
|
|
|
{
|
2021-11-21 18:07:09 -05:00
|
|
|
if (!in_array($type, static::ALLOWED_TYPES)) {
|
2021-11-21 14:06:36 -05:00
|
|
|
throw new InternalServerErrorException('wrong type');
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:52:36 -05:00
|
|
|
switch ($type) {
|
2022-01-03 14:18:43 -05:00
|
|
|
case static::TYPE_HTML:
|
|
|
|
$content_type = $content_type ?? 'text/html';
|
|
|
|
break;
|
2021-11-21 15:52:36 -05:00
|
|
|
case static::TYPE_JSON:
|
|
|
|
$content_type = $content_type ?? 'application/json';
|
|
|
|
break;
|
|
|
|
case static::TYPE_XML:
|
|
|
|
$content_type = $content_type ?? 'text/xml';
|
|
|
|
break;
|
2021-11-21 18:07:09 -05:00
|
|
|
case static::TYPE_RSS:
|
|
|
|
$content_type = $content_type ?? 'application/rss+xml';
|
|
|
|
break;
|
|
|
|
case static::TYPE_ATOM:
|
|
|
|
$content_type = $content_type ?? 'application/atom+xml';
|
|
|
|
break;
|
2021-11-21 15:52:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->setHeader($content_type, 'Content-type');
|
|
|
|
|
2021-11-21 14:06:36 -05:00
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
2022-01-02 15:28:28 -05:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function setStatus(int $status = 200, ?string $reason = null): void
|
|
|
|
{
|
|
|
|
$this->status = $status;
|
|
|
|
$this->reason = $reason;
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:06:36 -05:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2021-11-21 15:52:36 -05:00
|
|
|
public function getType(): string
|
2021-11-21 14:06:36 -05:00
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
2021-11-21 17:37:17 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function generate(): ResponseInterface
|
|
|
|
{
|
|
|
|
// Setting the response type as an X-header for direct usage
|
2021-11-21 18:07:09 -05:00
|
|
|
$this->headers[static::X_HEADER] = $this->type;
|
2021-11-21 17:37:17 -05:00
|
|
|
|
2022-01-02 16:17:04 -05:00
|
|
|
return new \GuzzleHttp\Psr7\Response($this->status, $this->headers, $this->content, '1.1', $this->reason);
|
2021-11-21 17:37:17 -05:00
|
|
|
}
|
2021-11-21 14:06:36 -05:00
|
|
|
}
|