Passed
Push — master ( 461fcb...e43cf9 )
by Igor
03:06
created

SerializationOptions::createFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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