This commit is contained in:
2024-12-16 16:29:35 +08:00
parent daa02a6408
commit dc561cec9b
7 changed files with 89 additions and 49 deletions
+32 -23
View File
@@ -9,27 +9,36 @@ use Psr\Http\Message\ResponseInterface;
class NoBody implements IFormat
{
/**
* @param $result
* @return ResponseInterface
*/
public function call($result): ResponseInterface
{
// TODO: Implement call() method.
$response = Kiri::getDi()->get(ResponseInterface::class);
if (request()->getMethod() === 'HEAD') {
return $response->withBody(new Stream());
}
if ($result instanceof ResponseInterface) {
return $result;
}
if (is_object($result)) {
return $response->withBody(new Stream('[object]'));
}
if (is_array($result)) {
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else {
return $response->withBody(new Stream((string)$result));
}
}
/**
* @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));
}
}
}