|
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 JsonObjectSerializer implements MessageSerializerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
private int $encodeOptions; |
|
23
|
|
|
|
|
24
|
|
|
private int $decodeOptions; |
|
25
|
|
|
|
|
26
|
|
|
private int $depth; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(int $encodeOptions = 0, int $decodeOptions = 0, int $depth = 512) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->encodeOptions = $encodeOptions; |
|
31
|
61 |
|
$this->decodeOptions = $decodeOptions; |
|
32
|
|
|
$this->depth = $depth; |
|
33
|
61 |
|
} |
|
34
|
61 |
|
|
|
35
|
61 |
|
public function serialize($request): string |
|
36
|
61 |
|
{ |
|
37
|
|
|
return json_encode($request, $this->encodeOptions, $this->depth); |
|
38
|
40 |
|
} |
|
39
|
|
|
|
|
40
|
40 |
|
public function deserialize(string $response, array $context) |
|
41
|
|
|
{ |
|
42
|
|
|
if ('' === trim($response)) { |
|
43
|
39 |
|
$result = null; |
|
44
|
|
|
} else { |
|
45
|
39 |
|
$decodedResponse = json_decode($response, false, $this->depth, $this->decodeOptions); |
|
46
|
5 |
|
$result = $this->deserializeResponse($decodedResponse, $response); |
|
47
|
|
|
} |
|
48
|
34 |
|
|
|
49
|
34 |
|
return $result; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
38 |
|
private function deserializeResponse($decodedResponse, string $response) |
|
53
|
|
|
{ |
|
54
|
|
|
$result = []; |
|
55
|
34 |
|
|
|
56
|
|
|
if (is_array($decodedResponse)) { |
|
57
|
34 |
|
foreach ($decodedResponse as $decodedObject) { |
|
58
|
|
|
$result[] = $this->deserializeResponseObject($decodedObject, $response); |
|
59
|
34 |
|
} |
|
60
|
8 |
|
} else { |
|
61
|
8 |
|
$result = $this->deserializeResponseObject($decodedResponse, $response); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
26 |
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
33 |
|
private function deserializeResponseObject($decodedObject, string $response): ResponseObject |
|
68
|
|
|
{ |
|
69
|
|
|
if (!is_object($decodedObject)) { |
|
70
|
34 |
|
throw new InvalidResponseException($response); |
|
71
|
|
|
} |
|
72
|
34 |
|
|
|
73
|
1 |
|
$responseObject = new ResponseObject( |
|
74
|
|
|
$decodedObject->jsonrpc ?? '', |
|
75
|
|
|
$decodedObject->result ?? null, |
|
76
|
33 |
|
$decodedObject->id ?? null |
|
77
|
33 |
|
); |
|
78
|
33 |
|
|
|
79
|
33 |
|
if (!empty($decodedObject->error)) { |
|
80
|
|
|
$decodedError = $decodedObject->error; |
|
81
|
|
|
|
|
82
|
33 |
|
$errorObject = new ErrorObject( |
|
83
|
9 |
|
$decodedError->code ?? 0, |
|
84
|
|
|
$decodedError->message ?? 'unknown error', |
|
85
|
9 |
|
$decodedError->data ?? null |
|
86
|
9 |
|
); |
|
87
|
9 |
|
|
|
88
|
9 |
|
$responseObject->setError($errorObject); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
return $responseObject; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|