|
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\Configuration; |
|
12
|
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\Exception\InvalidConfigException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Igor Lazarev <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class SerializationOptions |
|
19
|
|
|
{ |
|
20
|
|
|
public const OBJECT_SERIALIZER = 'object'; |
|
21
|
|
|
public const ARRAY_SERIALIZER = 'array'; |
|
22
|
|
|
public const SYMFONY_SERIALIZER = 'symfony'; |
|
23
|
|
|
public const DEFAULT_SERIALIZER = self::OBJECT_SERIALIZER; |
|
24
|
|
|
|
|
25
|
|
|
private const SUPPORTED_SERIALIZERS = [ |
|
26
|
|
|
self::OBJECT_SERIALIZER, |
|
27
|
|
|
self::ARRAY_SERIALIZER, |
|
28
|
|
|
self::SYMFONY_SERIALIZER, |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
private string $serializerType; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Used to deserialize successful server response to defined class or type. |
|
35
|
|
|
* |
|
36
|
|
|
* Example |
|
37
|
|
|
* |
|
38
|
|
|
* [ |
|
39
|
|
|
* 'createProduct' => CreateProductResponse::class, |
|
40
|
|
|
* ] |
|
41
|
|
|
* |
|
42
|
|
|
* Works only with Symfony serializer. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string[] |
|
45
|
|
|
*/ |
|
46
|
|
|
private array $resultTypesByMethods; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Used to deserialize error data from server response to defined class or type. It can be used |
|
50
|
|
|
* when all error data has the same structure or as fallback type for errors. If server can respond |
|
51
|
|
|
* with specific error data on method you can use errorTypesByMethods option. |
|
52
|
|
|
*/ |
|
53
|
|
|
private ?string $defaultErrorType; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Used to deserialize error data from server response after call to specific method. |
|
57
|
|
|
* |
|
58
|
|
|
* Example |
|
59
|
|
|
* |
|
60
|
|
|
* [ |
|
61
|
|
|
* 'createProduct' => CreateProductErrors::class, |
|
62
|
|
|
* ] |
|
63
|
|
|
* |
|
64
|
|
|
* @var string[] |
|
65
|
|
|
*/ |
|
66
|
|
|
private array $errorTypesByMethods; |
|
67
|
|
|
|
|
68
|
|
|
public function __construct( |
|
69
|
|
|
string $serializerType = self::DEFAULT_SERIALIZER, |
|
70
|
|
|
array $resultTypesByMethods = [], |
|
71
|
49 |
|
string $errorType = null, |
|
72
|
|
|
array $errorTypesByMethods = [] |
|
73
|
|
|
) { |
|
74
|
|
|
$this->validateSerializerType($serializerType); |
|
75
|
|
|
|
|
76
|
|
|
$this->serializerType = $serializerType; |
|
77
|
49 |
|
$this->resultTypesByMethods = $resultTypesByMethods; |
|
78
|
|
|
$this->defaultErrorType = $errorType; |
|
79
|
48 |
|
$this->errorTypesByMethods = $errorTypesByMethods; |
|
80
|
48 |
|
} |
|
81
|
48 |
|
|
|
82
|
48 |
|
public function getSerializerType(): string |
|
83
|
48 |
|
{ |
|
84
|
|
|
return $this->serializerType; |
|
85
|
33 |
|
} |
|
86
|
|
|
|
|
87
|
33 |
|
public function getResultTypesByMethods(): array |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->resultTypesByMethods; |
|
90
|
30 |
|
} |
|
91
|
|
|
|
|
92
|
30 |
|
public function getDefaultErrorType(): ?string |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->defaultErrorType; |
|
95
|
30 |
|
} |
|
96
|
|
|
|
|
97
|
30 |
|
public function getErrorTypesByMethods(): array |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->errorTypesByMethods; |
|
100
|
3 |
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
public static function createFromArray(array $options): self |
|
103
|
|
|
{ |
|
104
|
|
|
return new self( |
|
105
|
35 |
|
$options['serializer_type'] ?? self::DEFAULT_SERIALIZER, |
|
106
|
|
|
$options['result_types_by_methods'] ?? [], |
|
107
|
35 |
|
$options['default_error_type'] ?? null, |
|
108
|
35 |
|
$options['error_types_by_methods'] ?? [] |
|
109
|
35 |
|
); |
|
110
|
35 |
|
} |
|
111
|
35 |
|
|
|
112
|
|
|
private function validateSerializerType(string $serializer): void |
|
113
|
|
|
{ |
|
114
|
|
|
if (!in_array($serializer, self::SUPPORTED_SERIALIZERS, true)) { |
|
115
|
49 |
|
throw new InvalidConfigException( |
|
116
|
|
|
sprintf( |
|
117
|
49 |
|
'Serializer type option must be equal to one of: %s.', |
|
118
|
1 |
|
implode( |
|
119
|
1 |
|
', ', |
|
120
|
1 |
|
array_map( |
|
121
|
1 |
|
static function (string $s): string { |
|
122
|
1 |
|
return '"'.$s.'"'; |
|
123
|
1 |
|
}, |
|
124
|
|
|
self::SUPPORTED_SERIALIZERS |
|
125
|
1 |
|
) |
|
126
|
1 |
|
) |
|
127
|
1 |
|
) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|