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

33 lines
805 B
PHP
Raw Normal View History

2023-10-17 14:50:46 +08:00
<?php
namespace Kiri\Router\Format;
2024-08-29 17:01:08 +08:00
use Kiri;
2023-10-17 14:50:46 +08:00
use Kiri\Router\Constrict\Stream;
use Psr\Http\Message\ResponseInterface;
class MixedFormat implements IFormat
{
/**
* @param mixed $result
* @return ResponseInterface
*/
public function call(mixed $result): ResponseInterface
{
2023-11-07 14:28:57 +08:00
if ($result instanceof ResponseInterface) {
return $result;
}
2024-08-29 17:01:08 +08:00
$response = Kiri::getDi()->get(ResponseInterface::class);
2023-10-17 14:50:46 +08:00
if (is_object($result)) {
2024-08-29 17:01:08 +08:00
return $response->withBody(new Stream('[object]'));
2023-10-17 14:50:46 +08:00
}
if (is_array($result)) {
2024-08-29 17:01:08 +08:00
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
2023-10-17 14:50:46 +08:00
} else {
2024-08-29 17:01:08 +08:00
return $response->withBody(new Stream((string)$result));
2023-10-17 14:50:46 +08:00
}
}
}