2021-08-04 02:43:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2021-08-04 14:05:00 +08:00
|
|
|
namespace Server\Constrict;
|
2021-08-04 02:43:28 +08:00
|
|
|
|
|
|
|
|
|
2021-08-17 16:52:50 +08:00
|
|
|
use Http\Context\Context;
|
2021-09-09 16:38:54 +08:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
2021-08-28 01:21:23 +08:00
|
|
|
use Server\Message\Response as Psr7Response;
|
2021-09-06 17:46:03 +08:00
|
|
|
use Server\ResponseInterface;
|
2021-09-09 16:38:54 +08:00
|
|
|
use Server\SInterface\DownloadInterface;
|
2021-08-04 02:43:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Response
|
|
|
|
|
* @package Server
|
|
|
|
|
*/
|
2021-08-04 17:13:26 +08:00
|
|
|
class Response implements ResponseInterface
|
2021-08-04 02:43:28 +08:00
|
|
|
{
|
|
|
|
|
|
2021-08-04 10:16:03 +08:00
|
|
|
const JSON = 'json';
|
|
|
|
|
const XML = 'xml';
|
|
|
|
|
const HTML = 'html';
|
2021-08-10 16:40:01 +08:00
|
|
|
const FILE = 'file';
|
2021-08-04 10:16:03 +08:00
|
|
|
|
2021-09-06 17:46:03 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function __get(string $name)
|
|
|
|
|
{
|
|
|
|
|
return $this->__call__()->{$name};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Psr7Response
|
|
|
|
|
*/
|
|
|
|
|
public function __call__(): Psr7Response
|
|
|
|
|
{
|
2021-09-06 17:59:12 +08:00
|
|
|
return Context::getContext(ResponseInterface::class, new Psr7Response());
|
2021-09-06 17:46:03 +08:00
|
|
|
}
|
2021-09-09 16:38:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getProtocolVersion(): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $version
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withProtocolVersion($version): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($version);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getHeaders(): array
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasHeader($name): bool
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHeader($name): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHeaderLine($name): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|string[] $value
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withHeader($name, $value): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name, $value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|string[] $value
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withAddedHeader($name, $value): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name, $value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withoutHeader($name): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return StreamInterface
|
|
|
|
|
*/
|
|
|
|
|
public function getBody(): StreamInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param StreamInterface $body
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withBody(StreamInterface $body): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($body);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getStatusCode(): int
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $code
|
|
|
|
|
* @param string $reasonPhrase
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($code, $reasonPhrase);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getReasonPhrase(): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return DownloadInterface
|
|
|
|
|
*/
|
|
|
|
|
public function file(string $path): DownloadInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($path);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $responseData
|
|
|
|
|
* @return string|array|bool|int|null
|
|
|
|
|
*/
|
|
|
|
|
public function _toArray($responseData): string|array|null|bool|int
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($responseData);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function xml($data): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($data);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function html($data): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($data);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function json($data): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($data);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getContentType(): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasContentType(): bool
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withContentType(string $type): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($type);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-09 16:50:52 +08:00
|
|
|
* @param string $value
|
2021-09-09 16:38:54 +08:00
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
2021-09-09 16:50:52 +08:00
|
|
|
public function withAccessControlAllowOrigin(string $value): ResponseInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-09 16:50:52 +08:00
|
|
|
* @param string $value
|
2021-09-09 16:38:54 +08:00
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
2021-09-09 16:50:52 +08:00
|
|
|
public function withAccessControlRequestMethod(string $value): ResponseInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-09 16:50:52 +08:00
|
|
|
* @param string $value
|
2021-09-09 16:38:54 +08:00
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
2021-09-09 16:50:52 +08:00
|
|
|
public function withAccessControlAllowHeaders(string $value): ResponseInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
2021-08-04 02:43:28 +08:00
|
|
|
}
|