Completed
Branch coverage (797ea6)
by Eugene
01:35
created

UnpackOptions   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 64
ccs 27
cts 27
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isBigIntAsGmpMode() 0 4 1
A fromDefaults() 0 7 1
A fromBitmask() 0 12 2
A isBigIntAsStrMode() 0 4 1
A getSingleOption() 0 20 3
1
<?php
2
3
/*
4
 * This file is part of the rybakit/msgpack.php package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace MessagePack;
13
14
use MessagePack\Exception\InvalidOptionException;
15
16
final class UnpackOptions
17
{
18
    public const BIGINT_AS_STR       = 0b001;
19
    public const BIGINT_AS_GMP       = 0b010;
20
    public const BIGINT_AS_EXCEPTION = 0b100;
21
22
    private $bigIntMode;
23
24 242
    private function __construct()
25
    {
26 242
    }
27
28 232
    public static function fromDefaults() : self
29
    {
30 232
        $self = new self();
31 232
        $self->bigIntMode = self::BIGINT_AS_STR;
32
33 232
        return $self;
34
    }
35
36 17
    public static function fromBitmask(int $bitmask) : self
37
    {
38 17
        $self = new self();
39
40 17
        $self->bigIntMode = self::getSingleOption('bigint', $bitmask,
41 17
            self::BIGINT_AS_STR |
42 17
            self::BIGINT_AS_GMP |
43 17
            self::BIGINT_AS_EXCEPTION
44 2
        ) ?: self::BIGINT_AS_STR;
45
46 12
        return $self;
47
    }
48
49 237
    public function isBigIntAsStrMode() : bool
50
    {
51 237
        return self::BIGINT_AS_STR === $this->bigIntMode;
52
    }
53
54 237
    public function isBigIntAsGmpMode() : bool
55
    {
56 237
        return self::BIGINT_AS_GMP === $this->bigIntMode;
57
    }
58
59 17
    private static function getSingleOption(string $name, int $bitmask, int $validBitmask) : int
60
    {
61 17
        $option = $bitmask & $validBitmask;
62 17
        if ($option === ($option & -$option)) {
63 12
            return $option;
64
        }
65
66 5
        static $map = [
67
            self::BIGINT_AS_STR => 'BIGINT_AS_STR',
68
            self::BIGINT_AS_GMP => 'BIGINT_AS_GMP',
69
            self::BIGINT_AS_EXCEPTION => 'BIGINT_AS_EXCEPTION',
70
        ];
71
72 5
        $validOptions = [];
73 5
        for ($i = $validBitmask & -$validBitmask; $i <= $validBitmask; $i <<= 1) {
74 5
            $validOptions[] = __CLASS__.'::'.$map[$i];
75
        }
76
77 5
        throw InvalidOptionException::fromValidOptions($name, $validOptions);
78
    }
79
}
80