Bimba.git

ref: 5ec6b8b626f241c6a4b55f98f0f06c45d1d12e8c

converter/server/vendor/rybakit/msgpack/src/PackOptions.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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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 PackOptions
{
    public const FORCE_STR         = 0b00000001;
    public const FORCE_BIN         = 0b00000010;
    public const DETECT_STR_BIN    = 0b00000100;
    public const FORCE_ARR         = 0b00001000;
    public const FORCE_MAP         = 0b00010000;
    public const DETECT_ARR_MAP    = 0b00100000;
    public const FORCE_FLOAT32     = 0b01000000;
    public const FORCE_FLOAT64     = 0b10000000;

    private $strBinMode;
    private $arrMapMode;
    private $floatMode;

    private function __construct()
    {
    }

    public static function fromDefaults() : self
    {
        $self = new self();
        $self->strBinMode = self::DETECT_STR_BIN;
        $self->arrMapMode = self::DETECT_ARR_MAP;
        $self->floatMode = self::FORCE_FLOAT64;

        return $self;
    }

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

        $self->strBinMode = self::getSingleOption('str/bin', $bitmask,
            self::FORCE_STR |
            self::FORCE_BIN |
            self::DETECT_STR_BIN
        ) ?: self::DETECT_STR_BIN;

        $self->arrMapMode = self::getSingleOption('arr/map', $bitmask,
            self::FORCE_ARR |
            self::FORCE_MAP |
            self::DETECT_ARR_MAP
        ) ?: self::DETECT_ARR_MAP;

        $self->floatMode = self::getSingleOption('float', $bitmask,
            self::FORCE_FLOAT32 |
            self::FORCE_FLOAT64
        ) ?: self::FORCE_FLOAT64;

        return $self;
    }

    public function isDetectStrBinMode() : bool
    {
        return self::DETECT_STR_BIN === $this->strBinMode;
    }

    public function isForceStrMode() : bool
    {
        return self::FORCE_STR === $this->strBinMode;
    }

    public function isForceBinMode() : bool
    {
        return self::FORCE_BIN === $this->strBinMode;
    }

    public function isDetectArrMapMode() : bool
    {
        return self::DETECT_ARR_MAP === $this->arrMapMode;
    }

    public function isForceArrMode() : bool
    {
        return self::FORCE_ARR === $this->arrMapMode;
    }

    public function isForceMapMode() : bool
    {
        return self::FORCE_MAP === $this->arrMapMode;
    }

    public function isForceFloat32Mode() : bool
    {
        return self::FORCE_FLOAT32 === $this->floatMode;
    }

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

        static $map = [
            self::FORCE_STR => 'FORCE_STR',
            self::FORCE_BIN => 'FORCE_BIN',
            self::DETECT_STR_BIN => 'DETECT_STR_BIN',
            self::FORCE_ARR => 'FORCE_ARR',
            self::FORCE_MAP => 'FORCE_MAP',
            self::DETECT_ARR_MAP => 'DETECT_ARR_MAP',
            self::FORCE_FLOAT32 => 'FORCE_FLOAT32',
            self::FORCE_FLOAT64 => 'FORCE_FLOAT64',
        ];

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

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