Files
Leantime/app/Core/Http/Responses/JsonRpcResponse.php
wangruiguo d647428529
Some checks failed
Security Audit / Composer Security Audit (push) Has been cancelled
Security Audit / NPM Security Audit (push) Has been cancelled
Security Audit / CodeQL Analysis (push) Has been cancelled
OneBot: Leantime 改造版源码(BOM/Univer 表格/AI 接管/品牌替换等)
2026-09-03 18:49:20 +08:00

46 lines
1.4 KiB
PHP

<?php
namespace Leantime\Core\Http\Responses;
use Illuminate\Http\JsonResponse;
use Leantime\Core\Http\Responses\Contracts\LeantimeResponseInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* The JSON-RPC 2.0 success envelope as a first-class response type.
*
* Centralizes the `{jsonrpc, result, id}` wire format (previously inlined in the Jsonrpc
* controller) in the HTTP layer behind LeantimeResponseInterface, alongside ImageResponse.
*
* @see https://www.jsonrpc.org/specification#response_object
*/
class JsonRpcResponse implements LeantimeResponseInterface
{
/**
* @param mixed $result The service return value (any JSON value per spec §5).
* @param int|string|null $id The request id; null indicates a notification.
*/
public function __construct(
private mixed $result,
private int|string|null $id = null,
) {}
/**
* @param \Illuminate\Http\Request $request
*/
public function toResponse($request): Response
{
// A request without an id is a JSON-RPC notification and MUST NOT be responded to.
// @see https://www.jsonrpc.org/specification#notification
if ($this->id === null) {
return new Response('', Response::HTTP_OK);
}
return new JsonResponse([
'jsonrpc' => '2.0',
'result' => $this->result,
'id' => $this->id,
]);
}
}