-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseBuilder.php
More file actions
executable file
·49 lines (41 loc) · 1.07 KB
/
ResponseBuilder.php
File metadata and controls
executable file
·49 lines (41 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace Devim\Component\RpcServer;
use Devim\Component\RpcServer\Exception\RpcException;
/**
* Class ResponseBuilder
* @package Devim\Component\RpcServer
*/
class ResponseBuilder
{
/**
* @param $id|null
* @param $data
* @return array
*/
public static function build($id, $data) : array
{
$response = ['jsonrpc' => RpcServer::JSON_RPC_VERSION];
if ($data instanceof \Throwable) {
$response['error'] = self::buildError($data);
} else {
$response['result'] = $data;
}
$response['id'] = $id;
return $response;
}
/**
* @param \Throwable $exception
* @return array
*/
private static function buildError(\Throwable $exception) : array
{
$response = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
];
if ($exception instanceof RpcException && null !== $exception->getData()) {
$response['data'] = $exception->getData();
}
return $response;
}
}