Files
kiri-core/http-message/Response.php
T

223 lines
3.9 KiB
PHP
Raw Normal View History

2021-09-10 03:33:45 +08:00
<?php
2021-09-16 14:53:36 +08:00
namespace Http\Message;
2021-09-10 03:33:45 +08:00
2021-09-10 10:45:24 +08:00
use Exception;
2021-09-10 03:33:45 +08:00
use JetBrains\PhpStorm\Pure;
2021-09-10 10:45:24 +08:00
use Kiri\Core\Help;
2021-09-12 01:16:20 +08:00
use Server\Constrict\ResponseInterface;
2021-09-24 02:23:24 +08:00
use Server\SInterface\OnDownloadInterface;
2021-09-10 03:33:45 +08:00
2021-09-16 11:51:20 +08:00
/**
*
*/
2021-09-12 01:16:20 +08:00
class Response implements ResponseInterface
2021-09-10 03:33:45 +08:00
{
2021-09-10 10:24:11 +08:00
use Message;
2021-09-10 03:33:45 +08:00
2021-09-21 02:46:21 +08:00
const CONTENT_TYPE_HTML = 'text/html';
protected string $charset = 'utf8';
2021-09-10 10:45:24 +08:00
2021-09-10 10:24:11 +08:00
protected int $statusCode = 200;
2021-09-10 03:33:45 +08:00
2021-09-10 10:24:11 +08:00
protected string $reasonPhrase = '';
2021-09-10 03:33:45 +08:00
2021-09-10 11:10:49 +08:00
/**
* __construct
*/
public function __construct()
{
$this->stream = new Stream();
}
2021-09-10 10:24:11 +08:00
/**
* @return int
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
2021-09-10 03:33:45 +08:00
2021-09-10 10:24:11 +08:00
/**
* @param int $code
* @param string $reasonPhrase
* @return $this|Response
*/
public function withStatus($code, $reasonPhrase = ''): static
{
$this->statusCode = $code;
$this->reasonPhrase = $reasonPhrase;
return $this;
}
2021-09-10 03:33:45 +08:00
2021-09-10 10:24:11 +08:00
/**
* @return string
*/
public function getReasonPhrase(): string
{
return $this->reasonPhrase;
}
2021-09-10 03:33:45 +08:00
2021-09-10 10:24:11 +08:00
/**
* @return string|null
*/
#[Pure] public function getAccessControlAllowOrigin(): ?string
{
return $this->getHeaderLine('Access-Control-Allow-Origin');
}
2021-09-10 03:33:45 +08:00
2021-09-10 10:24:11 +08:00
/**
* @return string|null
*/
#[Pure] public function getAccessControlAllowHeaders(): ?string
{
return $this->getHeaderLine('Access-Control-Allow-Headers');
}
2021-09-10 03:33:45 +08:00
2021-09-10 10:24:11 +08:00
/**
* @return string|null
*/
#[Pure] public function getAccessControlRequestMethod(): ?string
{
return $this->getHeaderLine('Access-Control-Request-Method');
}
2021-09-10 10:45:24 +08:00
/**
* @param string $type
* @return Response
*/
public function withContentType(string $type): static
{
return $this->withHeader('Content-Type', $type);
}
/**
* @return bool
*/
#[Pure] public function hasContentType(): bool
{
return $this->hasHeader('Content-Type');
}
/**
* @param string|null $value
* @return Response
*/
public function withAccessControlAllowHeaders(?string $value): static
{
return $this->withHeader('Access-Control-Allow-Headers', $value);
}
/**
* @param string|null $value
* @return Response
*/
public function withAccessControlRequestMethod(?string $value): static
{
return $this->withHeader('Access-Control-Request-Method', $value);
}
/**
* @param string|null $value
* @return Response
*/
public function withAccessControlAllowOrigin(?string $value): static
{
return $this->withHeader('Access-Control-Allow-Origin', $value);
}
/**
* @param $data
2021-09-10 10:57:48 +08:00
* @param string $contentType
2021-09-10 10:45:24 +08:00
* @return static
*/
2021-09-21 02:46:21 +08:00
public function json($data, string $contentType = 'application/json'): static
2021-09-10 10:45:24 +08:00
{
$this->stream->write(json_encode($data));
2021-09-10 10:57:48 +08:00
return $this->withContentType($contentType);
2021-09-10 10:45:24 +08:00
}
/**
* @param $data
2021-09-10 10:57:48 +08:00
* @param string $contentType
2021-09-10 10:45:24 +08:00
* @return static
*/
2021-09-21 02:46:21 +08:00
public function html($data, string $contentType = 'text/html'): static
2021-09-10 10:45:24 +08:00
{
if (!is_string($data)) {
$data = json_encode($data);
}
$this->stream->write((string)$data);
2021-09-10 10:57:48 +08:00
return $this->withContentType($contentType);
2021-09-10 10:45:24 +08:00
}
/**
* @param $data
2021-09-10 10:57:48 +08:00
* @param string $contentType
2021-09-10 10:45:24 +08:00
* @return static
*/
2021-09-21 02:46:21 +08:00
public function xml($data, string $contentType = 'application/xml'): static
2021-09-10 10:45:24 +08:00
{
$this->stream->write(Help::toXml($data));
2021-09-10 10:57:48 +08:00
return $this->withContentType($contentType);
2021-09-10 10:45:24 +08:00
}
2021-09-21 02:46:21 +08:00
/**
* @param string $charset
* @return $this
*/
public function withCharset(string $charset): static
{
$type = explode('charset', $this->getContentType())[0];
$this->withContentType(
rtrim($type,';') . ';charset=' . $charset
);
return $this;
}
2021-09-10 10:45:24 +08:00
/**
* @param $path
* @param bool $isChunk
* @param int $size
* @param int $offset
2021-09-24 02:23:24 +08:00
* @return OnDownloadInterface
2021-09-10 10:45:24 +08:00
* @throws Exception
*/
2021-09-24 02:23:24 +08:00
public function file($path, bool $isChunk = false, int $size = -1, int $offset = 0): OnDownloadInterface
2021-09-10 10:45:24 +08:00
{
$path = realpath($path);
if (!file_exists($path) || !is_readable($path)) {
throw new Exception('Cannot read file "' . $path . '", no permission');
}
2021-09-24 02:23:24 +08:00
return (new OnDownload())->path($path, $isChunk, $size, $offset);
2021-09-10 10:45:24 +08:00
}
2021-09-10 03:33:45 +08:00
}