Completed
Push — master ( 03152b...3acf41 )
by Eugene
04:43
created

UnpackOptions::isBigIntAsExceptionMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 246
    private function __construct()
25
    {
26 246
    }
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 21
    public static function fromBitmask(int $bitmask) : self
37
    {
38 21
        $self = new self();
39
40 21
        $self->bigIntMode = self::getSingleOption('bigint', $bitmask,
41 21
            self::BIGINT_AS_STR |
42 21
            self::BIGINT_AS_GMP |
43 21
            self::BIGINT_AS_EXCEPTION
44 3
        ) ?: self::BIGINT_AS_STR;
45
46 16
        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 4
    public function isBigIntAsExceptionMode() : bool
60
    {
61 4
        return self::BIGINT_AS_EXCEPTION === $this->bigIntMode;
62
    }
63
64 21
    private static function getSingleOption(string $name, int $bitmask, int $validBitmask) : int
65
    {
66 21
        $option = $bitmask & $validBitmask;
67 21
        if ($option === ($option & -$option)) {
68 16
            return $option;
69
        }
70
71 5
        static $map = [
72
            self::BIGINT_AS_STR => 'BIGINT_AS_STR',
73
            self::BIGINT_AS_GMP => 'BIGINT_AS_GMP',
74
            self::BIGINT_AS_EXCEPTION => 'BIGINT_AS_EXCEPTION',
75
        ];
76
77 5
        $validOptions = [];
78 5
        for ($i = $validBitmask & -$validBitmask; $i <= $validBitmask; $i <<= 1) {
79 5
            $validOptions[] = __CLASS__.'::'.$map[$i];
80
        }
81
82 5
        throw InvalidOptionException::outOfRange($name, $validOptions);
83
    }
84
}
85