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

45 lines
851 B
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-05 03:25:30 +00:00
class JsonRpcMethod
{
protected $id;
protected $request;
final public function __construct($id, $request)
{
$this->id = $id;
$this->request = $request;
}
public function response($result)
{
return JsonResponse::create([
'jsonrpc' => '2.0',
'result' => $result,
'id' => $this->id
]);
}
2019-01-17 06:10:18 +00:00
/**
*
* @param $code
* @param $msg
* @return static
*/
2019-01-05 03:25:30 +00:00
public function error($code, $msg)
{
return JsonResponse::create([
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
2019-01-17 06:10:18 +00:00
'message' => is_array($msg) ? json_encode($msg) : $msg,
2019-01-05 03:25:30 +00:00
],
'id' => $this->id
]);
}
}