mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-23 09:02:41 +00:00
1b970cc7c5
- Improves #48, #57, and #61
47 lines
No EOL
1.3 KiB
PHP
47 lines
No EOL
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace JKingWeb\Arsse\REST;
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
class Response {
|
|
const T_JSON = "application/json";
|
|
const T_XML = "application/xml";
|
|
const T_TEXT = "text/plain";
|
|
|
|
public $code;
|
|
public $payload;
|
|
public $type;
|
|
public $fields;
|
|
|
|
|
|
function __construct(int $code, $payload = null, string $type = self::T_JSON, array $extraFields = []) {
|
|
$this->code = $code;
|
|
$this->payload = $payload;
|
|
$this->type = $type;
|
|
$this->fields = $extraFields;
|
|
}
|
|
|
|
function output() {
|
|
if(!headers_sent()) {
|
|
header("Status: ".$this->code." ".Arsse::$lang->msg("HTTP.Status.".$this->code));
|
|
$body = "";
|
|
if(!is_null($this->payload)) {
|
|
header("Content-Type: ".$this->type);
|
|
switch($this->type) {
|
|
case self::T_JSON:
|
|
$body = json_encode($this->payload,\JSON_PRETTY_PRINT);
|
|
break;
|
|
default:
|
|
$body = (string) $this->payload;
|
|
break;
|
|
}
|
|
}
|
|
foreach($this->fields as $field) {
|
|
header($field);
|
|
}
|
|
echo $body;
|
|
} else {
|
|
throw new REST\Exception("headersSent");
|
|
}
|
|
}
|
|
} |