Bimba.git

ref: ae5068cf00a897920984922872282af92bd9e15c

converter/server/vendor/rybakit/msgpack/src/UnpackOptions.php


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

/*
 * This file is part of the rybakit/msgpack.php package.
 *
 * (c) Eugene Leonovich <gen.work@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace MessagePack;

use MessagePack\Exception\InvalidOptionException;

final class UnpackOptions
{
    public const BIGINT_AS_STR       = 0b001;
    public const BIGINT_AS_GMP       = 0b010;
    public const BIGINT_AS_EXCEPTION = 0b100;

    private $bigIntMode;

    private function __construct()
    {
    }

    public static function fromDefaults() : self
    {
        $self = new self();
        $self->bigIntMode = self::BIGINT_AS_STR;

        return $self;
    }

    public static function fromBitmask(int $bitmask) : self
    {
        $self = new self();

        $self->bigIntMode = self::getSingleOption('bigint', $bitmask,
            self::BIGINT_AS_STR |
            self::BIGINT_AS_GMP |
            self::BIGINT_AS_EXCEPTION
        ) ?: self::BIGINT_AS_STR;

        return $self;
    }

    public function isBigIntAsStrMode() : bool
    {
        return self::BIGINT_AS_STR === $this->bigIntMode;
    }

    public function isBigIntAsGmpMode() : bool
    {
        return self::BIGINT_AS_GMP === $this->bigIntMode;
    }

    public function isBigIntAsExceptionMode() : bool
    {
        return self::BIGINT_AS_EXCEPTION === $this->bigIntMode;
    }

    private static function getSingleOption(string $name, int $bitmask, int $validBitmask) : int
    {
        $option = $bitmask & $validBitmask;
        if ($option === ($option & -$option)) {
            return $option;
        }

        static $map = [
            self::BIGINT_AS_STR => 'BIGINT_AS_STR',
            self::BIGINT_AS_GMP => 'BIGINT_AS_GMP',
            self::BIGINT_AS_EXCEPTION => 'BIGINT_AS_EXCEPTION',
        ];

        $validOptions = [];
        for ($i = $validBitmask & -$validBitmask; $i <= $validBitmask; $i <<= 1) {
            $validOptions[] = __CLASS__.'::'.$map[$i];
        }

        throw InvalidOptionException::outOfRange($name, $validOptions);
    }
}