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\Request\RequestObjectInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Igor Lazarev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ContextGenerator |
19
|
|
|
{ |
20
|
|
|
/** @var string[] */ |
21
|
|
|
private array $resultTypesByMethods; |
22
|
|
|
|
23
|
|
|
private ?string $defaultErrorType; |
24
|
|
|
|
25
|
|
|
/** @var string[] */ |
26
|
|
|
private array $errorTypesByMethods; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
54 |
|
array $resultTypesByMethods = [], |
30
|
|
|
string $errorType = null, |
31
|
|
|
array $errorTypesByMethods = [] |
32
|
|
|
) { |
33
|
|
|
$this->resultTypesByMethods = $resultTypesByMethods; |
34
|
54 |
|
$this->defaultErrorType = $errorType; |
35
|
54 |
|
$this->errorTypesByMethods = $errorTypesByMethods; |
36
|
54 |
|
} |
37
|
54 |
|
|
38
|
|
|
public function createSerializationContext($request): array |
39
|
39 |
|
{ |
40
|
|
|
$context = [ |
41
|
|
|
'result_types_by_methods' => $this->resultTypesByMethods, |
42
|
39 |
|
'default_error_type' => $this->defaultErrorType, |
43
|
39 |
|
'error_types_by_methods' => $this->errorTypesByMethods, |
44
|
39 |
|
]; |
45
|
|
|
|
46
|
|
|
if ($request instanceof RequestObjectInterface) { |
47
|
39 |
|
$context['request'] = $request; |
48
|
29 |
|
} else { |
49
|
|
|
$requests = []; |
50
|
10 |
|
|
51
|
|
|
/** @var RequestObjectInterface $singleRequest */ |
52
|
|
|
foreach ($request as $singleRequest) { |
53
|
10 |
|
$requests[$singleRequest->getId()] = $singleRequest; |
54
|
10 |
|
} |
55
|
|
|
|
56
|
|
|
$context['requests'] = $requests; |
57
|
10 |
|
} |
58
|
|
|
|
59
|
|
|
return ['json_rpc' => $context]; |
60
|
39 |
|
} |
61
|
|
|
} |
62
|
|
|
|