Files
kiri-router/src/Format/NoBody.php
T

44 lines
900 B
PHP
Raw Normal View History

2023-11-22 10:19:33 +08:00
<?php
namespace Kiri\Router\Format;
2024-08-29 17:01:08 +08:00
use Kiri;
2023-11-22 10:19:33 +08:00
use Kiri\Router\Constrict\Stream;
use Psr\Http\Message\ResponseInterface;
class NoBody implements IFormat
{
2024-12-16 16:29:35 +08:00
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/**
* @param $result
*
* @return ResponseInterface
*/
public function call($result): ResponseInterface
{
// TODO: Implement call() method.
if (request()->getMethod() === 'HEAD') {
return $this->response->withBody(new Stream());
}
if ($result instanceof ResponseInterface) {
return $result;
}
if (is_object($result)) {
return $this->response->withBody(new Stream('[object]'));
}
if (is_array($result)) {
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else {
return $this->response->withBody(new Stream((string)$result));
}
}
2023-11-22 10:19:33 +08:00
}