Passed
Push — master ( 342a74...4ed09e )
by Eugene
02:09
created

UnpackOptions::isBigIntAsFloatMode()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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_FLOAT = 0b001;
19
    public const BIGINT_AS_STR   = 0b010;
20
    public const BIGINT_AS_GMP   = 0b100;
21
22
    private $bigIntMode;
23
24 333
    private function __construct()
25
    {
26 333
    }
27
28 319
    public static function fromDefaults() : self
29
    {
30 319
        $self = new self();
31 319
        $self->bigIntMode = self::BIGINT_AS_FLOAT;
32
33 319
        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_FLOAT |
42 21
            self::BIGINT_AS_STR |
43 21
            self::BIGINT_AS_GMP
44 3
        ) ?: self::BIGINT_AS_FLOAT;
45
46 16
        return $self;
47
    }
48
49 4
    public function isBigIntAsFloatMode() : bool
50
    {
51 4
        return self::BIGINT_AS_FLOAT === $this->bigIntMode;
52
    }
53
54 324
    public function isBigIntAsStrMode() : bool
55
    {
56 324
        return self::BIGINT_AS_STR === $this->bigIntMode;
57
    }
58
59 324
    public function isBigIntAsGmpMode() : bool
60
    {
61 324
        return self::BIGINT_AS_GMP === $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_FLOAT => 'BIGINT_AS_FLOAT',
73
            self::BIGINT_AS_STR => 'BIGINT_AS_STR',
74
            self::BIGINT_AS_GMP => 'BIGINT_AS_GMP',
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