|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of JSON RPC Client. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Serialization; |
|
12
|
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\Exception\InvalidResponseException; |
|
14
|
|
|
use Strider2038\JsonRpcClient\Response\ErrorObject; |
|
15
|
|
|
use Strider2038\JsonRpcClient\Response\ResponseObject; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Igor Lazarev <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class JsonArraySerializer implements MessageSerializerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
private $encodeOptions; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int */ |
|
26
|
|
|
private $decodeOptions; |
|
27
|
|
|
|
|
28
|
|
|
/** @var int */ |
|
29
|
|
|
private $depth; |
|
30
|
|
|
|
|
31
|
11 |
|
public function __construct(int $encodeOptions = 0, int $decodeOptions = 0, int $depth = 512) |
|
32
|
|
|
{ |
|
33
|
11 |
|
$this->encodeOptions = $encodeOptions; |
|
34
|
11 |
|
$this->decodeOptions = $decodeOptions; |
|
35
|
11 |
|
$this->depth = $depth; |
|
36
|
11 |
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
public function serialize($request): string |
|
39
|
|
|
{ |
|
40
|
3 |
|
return json_encode($request, $this->encodeOptions, $this->depth); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
7 |
|
public function deserialize(string $response) |
|
44
|
|
|
{ |
|
45
|
7 |
|
if ('' === trim($response)) { |
|
46
|
1 |
|
$result = null; |
|
47
|
|
|
} else { |
|
48
|
6 |
|
$decodedResponse = json_decode($response, true, $this->depth, $this->decodeOptions); |
|
49
|
6 |
|
$result = $this->deserializeResponse($decodedResponse, $response); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
return $result; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
private function deserializeResponse($decodedResponse, string $response) |
|
56
|
|
|
{ |
|
57
|
6 |
|
if (!is_array($decodedResponse)) { |
|
58
|
1 |
|
throw new InvalidResponseException($response); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
$result = []; |
|
62
|
|
|
|
|
63
|
5 |
|
if (array_key_exists('jsonrpc', $decodedResponse) || empty($decodedResponse)) { |
|
64
|
4 |
|
$result = $this->deserializeResponseObject($decodedResponse); |
|
65
|
|
|
} else { |
|
66
|
1 |
|
foreach ($decodedResponse as $decodedObject) { |
|
67
|
1 |
|
$result[] = $this->deserializeResponseObject($decodedObject); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
private function deserializeResponseObject(array $decodedObject): ResponseObject |
|
75
|
|
|
{ |
|
76
|
5 |
|
$responseObject = new ResponseObject(); |
|
77
|
5 |
|
$responseObject->jsonrpc = $decodedObject['jsonrpc'] ?? ''; |
|
78
|
5 |
|
$responseObject->result = $decodedObject['result'] ?? null; |
|
79
|
5 |
|
$responseObject->id = $decodedObject['id'] ?? null; |
|
80
|
|
|
|
|
81
|
5 |
|
if (array_key_exists('error', $decodedObject)) { |
|
82
|
1 |
|
$decodedError = $decodedObject['error']; |
|
83
|
|
|
|
|
84
|
1 |
|
$errorObject = new ErrorObject(); |
|
85
|
1 |
|
$errorObject->code = $decodedError['code'] ?? null; |
|
86
|
1 |
|
$errorObject->message = $decodedError['message'] ?? null; |
|
87
|
1 |
|
$errorObject->data = $decodedError['data'] ?? null; |
|
88
|
|
|
|
|
89
|
1 |
|
$responseObject->error = $errorObject; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
return $responseObject; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|