|
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\Bridge\Symfony\Serialization; |
|
12
|
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\Request\RequestObject; |
|
14
|
|
|
use Strider2038\JsonRpcClient\Response\ErrorObject; |
|
15
|
|
|
use Strider2038\JsonRpcClient\Response\ResponseObject; |
|
16
|
|
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException; |
|
17
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
|
18
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
|
19
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
|
20
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Igor Lazarev <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class ResponseObjectDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use DenormalizerAwareTrait; |
|
28
|
|
|
|
|
29
|
4 |
|
public function hasCacheableSupportsMethod(): bool |
|
30
|
|
|
{ |
|
31
|
4 |
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
public function supportsDenormalization($data, $type, $format = null): bool |
|
35
|
|
|
{ |
|
36
|
4 |
|
return ResponseObject::class === $type; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
9 |
|
public function denormalize($data, $type, $format = null, array $context = []): ResponseObject |
|
40
|
|
|
{ |
|
41
|
9 |
|
if (!is_array($data)) { |
|
42
|
1 |
|
throw new UnexpectedValueException('Denormalization data is expected to be an array'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
$response = new ResponseObject( |
|
46
|
8 |
|
$data['jsonrpc'] ?? '', |
|
47
|
8 |
|
$this->denormalizeResult($data, $format, $context), |
|
48
|
8 |
|
$data['id'] ?? null |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
8 |
|
if (array_key_exists('error', $data)) { |
|
52
|
4 |
|
$response->setError($this->denormalizeError($data, $format, $context)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
return $response; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
8 |
|
private function denormalizeResult($data, $format, array $context) |
|
59
|
|
|
{ |
|
60
|
8 |
|
$result = $data['result'] ?? null; |
|
61
|
|
|
|
|
62
|
8 |
|
$request = $context['json_rpc']['request'] ?? null; |
|
63
|
|
|
|
|
64
|
8 |
|
if ($request instanceof RequestObject) { |
|
65
|
4 |
|
$method = $request->getMethod(); |
|
66
|
|
|
|
|
67
|
4 |
|
$resultTypesByMethods = $context['json_rpc']['result_types_by_methods'] ?? []; |
|
68
|
|
|
|
|
69
|
4 |
|
if (null !== $result && array_key_exists($method, $resultTypesByMethods)) { |
|
70
|
3 |
|
$resultType = $resultTypesByMethods[$method]; |
|
71
|
|
|
|
|
72
|
3 |
|
$result = $this->denormalizer->denormalize($result, $resultType, $format, $context); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
8 |
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
private function denormalizeError($data, $format, array $context): ErrorObject |
|
80
|
|
|
{ |
|
81
|
4 |
|
return new ErrorObject( |
|
82
|
4 |
|
$data['error']['code'] ?? 0, |
|
83
|
4 |
|
$data['error']['message'] ?? 'unknown error', |
|
84
|
4 |
|
$this->denormalizeErrorData($data['error']['data'] ?? null, $format, $context) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
private function denormalizeErrorData($errorData, $format, array $context) |
|
89
|
|
|
{ |
|
90
|
4 |
|
$errorType = $context['json_rpc']['error_type'] ?? null; |
|
91
|
|
|
|
|
92
|
4 |
|
if (null !== $errorData && null !== $errorType) { |
|
93
|
2 |
|
$errorData = $this->denormalizer->denormalize($errorData, $errorType, $format, $context); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
return $errorData; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|