Test Failed
Pull Request — master (#8)
by Igor
13:01
created

SerializationOptions::getSerializerType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 $serializerType;
33
34
    /**
35
     * Used to deserialize successful server response to defined class or type.
36
     *
37
     * Example
38
     *
39
     * [
40 43
     *     'createProduct' => CreateProductResponse::class,
41
     * ]
42
     *
43
     * Works only with Symfony serializer.
44
     *
45 43
     * @var string[]
46
     */
47 42
    private $resultTypesByMethods;
48 42
49 42
    /**
50 42
     * Used to deserialize error data from server response to defined class or type. It can be used
51
     * when all error data has the same structure or as fallback type for errors. If server can respond
52 27
     * with specific error data on method you can use errorTypesByMethods option.
53
     *
54 27
     * @var string|null
55
     */
56
    private $defaultErrorType;
57 24
58
    /**
59 24
     * Used to deserialize error data from server response after call to specific method.
60
     *
61
     * Example
62 24
     *
63
     * [
64 24
     *     'createProduct' => CreateProductErrors::class,
65
     * ]
66
     *
67 29
     * @var string[]
68
     */
69 29
    private $errorTypesByMethods;
70 29
71 29
    public function __construct(
72 29
        string $serializerType = self::DEFAULT_SERIALIZER,
73
        array $resultTypesByMethods = [],
74
        string $errorType = null,
75
        array $errorTypesByMethods = []
76 43
    ) {
77
        $this->validateSerializerType($serializerType);
78 43
79 1
        $this->serializerType = $serializerType;
80 1
        $this->resultTypesByMethods = $resultTypesByMethods;
81 1
        $this->defaultErrorType = $errorType;
82 1
        $this->errorTypesByMethods = $errorTypesByMethods;
83 1
    }
84 1
85
    public function getSerializerType(): string
86 1
    {
87 1
        return $this->serializerType;
88 1
    }
89
90
    public function getResultTypesByMethods(): array
91
    {
92
        return $this->resultTypesByMethods;
93
    }
94 42
95
    public function getDefaultErrorType(): ?string
96
    {
97
        return $this->defaultErrorType;
98
    }
99
100
    public function getErrorTypesByMethods(): array
101
    {
102
        return $this->errorTypesByMethods;
103
    }
104
105
    public static function createFromArray(array $options): self
106
    {
107
        return new self(
108
            $options['serializer_type'] ?? self::DEFAULT_SERIALIZER,
109
            $options['result_types_by_methods'] ?? [],
110
            $options['default_error_type'] ?? null,
111
            $options['error_types_by_methods'] ?? []
112
        );
113
    }
114
115
    private function validateSerializerType(string $serializer): void
116
    {
117
        if (!in_array($serializer, self::SUPPORTED_SERIALIZERS, true)) {
118
            throw new InvalidConfigException(
119
                sprintf(
120
                    'Serializer type option must be equal to one of: %s.',
121
                    implode(
122
                        ', ',
123
                        array_map(
124
                            static function (string $s): string {
125
                                return '"'.$s.'"';
126
                            },
127
                            self::SUPPORTED_SERIALIZERS
128
                        )
129
                    )
130
                )
131
            );
132
        }
133
    }
134
}
135