|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\JsonRpc\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Bankiru\Api\JsonRpc\BankiruJsonRpcServerBundle; |
|
6
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcErrorInterface; |
|
7
|
|
|
use ScayTrase\Api\JsonRpc\JsonRpcResponseInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
9
|
|
|
|
|
10
|
|
|
final class JsonRpcHttpResponse extends JsonResponse |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* JsonRpcHttpResponse constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param JsonRpcResponseInterface|JsonRpcResponseInterface[] $jsonRpc |
|
16
|
|
|
* @param int $status |
|
17
|
|
|
* @param array $headers |
|
18
|
|
|
*/ |
|
19
|
19 |
|
public function __construct($jsonRpc = null, $status = 200, array $headers = []) |
|
20
|
|
|
{ |
|
21
|
19 |
|
if (null === $jsonRpc) { |
|
22
|
1 |
|
parent::__construct(null, $status, $headers); |
|
23
|
|
|
|
|
24
|
1 |
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
18 |
|
if (is_array($jsonRpc)) { |
|
28
|
7 |
|
parent::__construct(array_map([$this, 'formatJsonRpcResponse'], $jsonRpc), $status, $headers); |
|
29
|
|
|
|
|
30
|
7 |
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
11 |
|
parent::__construct($this->formatJsonRpcResponse($jsonRpc), $status, $headers); |
|
34
|
11 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $jsonRpc |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
16 |
|
private function formatJsonRpcResponse(JsonRpcResponseInterface $jsonRpc) |
|
42
|
|
|
{ |
|
43
|
|
|
$data = [ |
|
44
|
16 |
|
'jsonrpc' => BankiruJsonRpcServerBundle::JSONRPC_VERSION, |
|
45
|
16 |
|
'id' => $jsonRpc->getId(), |
|
46
|
16 |
|
]; |
|
47
|
|
|
|
|
48
|
16 |
|
if ($jsonRpc->isSuccessful()) { |
|
49
|
13 |
|
$data['result'] = $jsonRpc->getBody(); |
|
50
|
|
|
|
|
51
|
13 |
|
return $data; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
$error = $jsonRpc->getError(); |
|
55
|
6 |
|
$data['error']['code'] = $error->getCode(); |
|
56
|
6 |
|
$data['error']['message'] = $error->getMessage(); |
|
57
|
6 |
|
$data['error']['data'] = $error instanceof JsonRpcErrorInterface ? $error->getData() : null; |
|
58
|
|
|
|
|
59
|
6 |
|
return $data; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|