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