UnpackOptions::isBigIntAsStrMode()   A
last analyzed

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 1
Bugs 0 Features 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 1
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_STR = 0b001;
19
    public const BIGINT_AS_GMP = 0b010;
20
    public const BIGINT_AS_DEC = 0b100;
21
22
    /** @var int */
23
    private $bigIntMode;
24
25
    /**
26
     * @param int $bigIntMode
27
     */
28 358
    private function __construct($bigIntMode)
29
    {
30 358
        $this->bigIntMode = $bigIntMode;
31
    }
32
33 345
    public static function fromDefaults() : self
34
    {
35 345
        return new self(self::BIGINT_AS_STR);
36
    }
37
38 23
    public static function fromBitmask(int $bitmask) : self
39
    {
40 23
        return new self(
41 23
            self::getSingleOption('bigint', $bitmask,
42 23
                self::BIGINT_AS_STR | self::BIGINT_AS_GMP | self::BIGINT_AS_DEC
43 23
            ) ?: self::BIGINT_AS_STR
44 23
        );
45
    }
46
47 4
    public function isBigIntAsStrMode() : bool
48
    {
49 4
        return self::BIGINT_AS_STR === $this->bigIntMode;
50
    }
51
52 350
    public function isBigIntAsGmpMode() : bool
53
    {
54 350
        return self::BIGINT_AS_GMP === $this->bigIntMode;
55
    }
56
57 350
    public function isBigIntAsDecMode() : bool
58
    {
59 350
        return self::BIGINT_AS_DEC === $this->bigIntMode;
60
    }
61
62 23
    private static function getSingleOption(string $name, int $bitmask, int $validBitmask) : int
63
    {
64 23
        $option = $bitmask & $validBitmask;
65 23
        if ($option === ($option & -$option)) {
66 18
            return $option;
67
        }
68
69 5
        static $map = [
70 5
            self::BIGINT_AS_STR => 'BIGINT_AS_STR',
71 5
            self::BIGINT_AS_GMP => 'BIGINT_AS_GMP',
72 5
            self::BIGINT_AS_DEC => 'BIGINT_AS_DEC',
73 5
        ];
74
75 5
        $validOptions = [];
76 5
        for ($i = $validBitmask & -$validBitmask; $i <= $validBitmask; $i <<= 1) {
77 5
            $validOptions[] = __CLASS__.'::'.$map[$i];
78
        }
79
80 5
        throw InvalidOptionException::outOfRange($name, $validOptions);
81
    }
82
}
83