2023-10-17 14:50:46 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Router\Format;
|
|
|
|
|
|
2023-11-03 17:44:38 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2023-10-17 14:50:46 +08:00
|
|
|
use Kiri\Router\Constrict\Stream;
|
|
|
|
|
use Kiri\Router\ContentType;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
|
|
class MixedFormat implements IFormat
|
|
|
|
|
{
|
|
|
|
|
|
2023-11-03 17:44:38 +08:00
|
|
|
/**
|
|
|
|
|
* @var ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
#[Container(ResponseInterface::class)]
|
|
|
|
|
public ResponseInterface $response;
|
|
|
|
|
|
|
|
|
|
|
2023-10-17 14:50:46 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $result
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function call(mixed $result): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
if (is_object($result)) {
|
2023-11-03 17:44:38 +08:00
|
|
|
return $this->response->withBody(new Stream('[object]'));
|
2023-10-17 14:50:46 +08:00
|
|
|
}
|
|
|
|
|
if (is_array($result)) {
|
2023-11-03 17:44:38 +08:00
|
|
|
return $this->response->withContentType(ContentType::JSON)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
2023-10-17 14:50:46 +08:00
|
|
|
} else {
|
2023-11-03 17:44:38 +08:00
|
|
|
return $this->response->withBody(new Stream((string)$result));
|
2023-10-17 14:50:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|