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
|
|
|
/** @var string */ |
32
|
|
|
private $serializer; |
33
|
|
|
|
34
|
|
|
/** @var string[] */ |
35
|
|
|
private $resultTypesByMethods; |
36
|
|
|
|
37
|
|
|
/** @var string|null */ |
38
|
|
|
private $errorType; |
39
|
|
|
|
40
|
43 |
|
public function __construct( |
41
|
|
|
string $serializer = self::DEFAULT_SERIALIZER, |
42
|
|
|
array $resultTypesByMethods = [], |
43
|
|
|
string $errorType = null |
44
|
|
|
) { |
45
|
43 |
|
$this->validateSerializer($serializer); |
46
|
|
|
|
47
|
42 |
|
$this->serializer = $serializer; |
48
|
42 |
|
$this->resultTypesByMethods = $resultTypesByMethods; |
49
|
42 |
|
$this->errorType = $errorType; |
50
|
42 |
|
} |
51
|
|
|
|
52
|
27 |
|
public function getSerializer(): string |
53
|
|
|
{ |
54
|
27 |
|
return $this->serializer; |
55
|
|
|
} |
56
|
|
|
|
57
|
24 |
|
public function getResultTypesByMethods(): array |
58
|
|
|
{ |
59
|
24 |
|
return $this->resultTypesByMethods; |
60
|
|
|
} |
61
|
|
|
|
62
|
24 |
|
public function getErrorType(): ?string |
63
|
|
|
{ |
64
|
24 |
|
return $this->errorType; |
65
|
|
|
} |
66
|
|
|
|
67
|
29 |
|
public static function createFromArray(array $options): self |
68
|
|
|
{ |
69
|
29 |
|
return new self( |
70
|
29 |
|
$options['serializer'] ?? self::DEFAULT_SERIALIZER, |
71
|
29 |
|
$options['result_types_by_methods'] ?? [], |
72
|
29 |
|
$options['error_type'] ?? null |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
43 |
|
private function validateSerializer(string $serializer): void |
77
|
|
|
{ |
78
|
43 |
|
if (!in_array($serializer, self::SUPPORTED_SERIALIZERS, true)) { |
79
|
1 |
|
throw new InvalidConfigException( |
80
|
1 |
|
sprintf( |
81
|
1 |
|
'Serializer option must be equal to one of: %s.', |
82
|
1 |
|
implode( |
83
|
1 |
|
', ', |
84
|
1 |
|
array_map( |
85
|
|
|
static function (string $s): string { |
86
|
1 |
|
return '"'.$s.'"'; |
87
|
1 |
|
}, |
88
|
1 |
|
self::SUPPORTED_SERIALIZERS |
89
|
|
|
) |
90
|
|
|
) |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
} |
94
|
42 |
|
} |
95
|
|
|
} |
96
|
|
|
|