Completed
Pull Request — master (#7)
by Igor
04:25
created

SerializationOptions::validateSerializer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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