php-json-rpc/src/Server/JsonRpcMethod.php

52 lines
1.1 KiB
PHP
Raw Normal View History

2019-01-05 03:25:30 +00:00
<?php
namespace JsonRpc\Server;
2019-01-05 04:47:51 +00:00
use Illuminate\Http\JsonResponse;
2019-01-22 08:01:23 +00:00
use JsonRpc\Exception\RpcServerException;
2019-01-30 08:06:41 +00:00
use JsonRpc\JsonRpc;
2019-01-05 04:47:51 +00:00
2019-01-30 08:06:41 +00:00
class JsonRpcMethod extends JsonRpc
2019-01-05 03:25:30 +00:00
{
protected $id;
protected $request;
final public function __construct($id, $request)
{
$this->id = $id;
$this->request = $request;
}
public function response($result)
{
2019-01-19 09:10:30 +00:00
return [
2019-01-05 03:25:30 +00:00
'jsonrpc' => '2.0',
'result' => $result,
'id' => $this->id
2019-01-19 09:10:30 +00:00
];
2019-01-05 03:25:30 +00:00
}
public function error($code, $msg)
{
2019-01-22 08:01:23 +00:00
2019-01-30 06:48:41 +00:00
return is_string($msg)
? [
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
'message' => $msg,
],
'id' => $this->id
]
: [
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
'message' => self::ErrorMsg[$code],
'data' => $msg
],
'id' => $this->id
];
2019-01-05 03:25:30 +00:00
}
}