-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRpcResponse.php
More file actions
61 lines (46 loc) · 1.41 KB
/
RpcResponse.php
File metadata and controls
61 lines (46 loc) · 1.41 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
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Casper\Rpc;
class RpcResponse
{
private ?string $jsonrpc;
private ?int $id;
private ?array $result;
private ?RpcError $error = null;
private function __construct() {}
public static function fromArray(array $data): self
{
$response = new self();
$response->jsonrpc = $data['jsonrpc'] ?? null;
$response->id = $data['id'] ?? null;
$response->result = $data['result'] ?? null;
if ($response->result === null || isset($data['error'])) {
$errorMessage = $data['error']['message'] ?? $data['message'] ?? 'Empty response';
$errorCode = $data['error']['code'] ?? 0;
if (isset($data['error']['data'])) {
$errorData = $data['error']['data'];
if (is_array($errorData) || is_object($errorData)) {
$errorData = json_encode($errorData);
}
$errorMessage .= '. Data: ' . $errorData;
}
$response->error = new RpcError($errorMessage, $errorCode);
}
return $response;
}
public function getJsonrpc(): string
{
return $this->jsonrpc;
}
public function getId(): int
{
return $this->id;
}
public function getResult(): ?array
{
return $this->result;
}
public function getError(): ?RpcError
{
return $this->error;
}
}