-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathUnpackOptions.php
More file actions
82 lines (67 loc) · 2.04 KB
/
UnpackOptions.php
File metadata and controls
82 lines (67 loc) · 2.04 KB
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
<?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_DEC = 0b100;
/** @var int */
private $bigIntMode;
/**
* @param int $bigIntMode
*/
private function __construct($bigIntMode)
{
$this->bigIntMode = $bigIntMode;
}
public static function fromDefaults() : self
{
return new self(self::BIGINT_AS_STR);
}
public static function fromBitmask(int $bitmask) : self
{
return new self(
self::getSingleOption('bigint', $bitmask,
self::BIGINT_AS_STR | self::BIGINT_AS_GMP | self::BIGINT_AS_DEC
) ?: self::BIGINT_AS_STR
);
}
public function isBigIntAsStrMode() : bool
{
return self::BIGINT_AS_STR === $this->bigIntMode;
}
public function isBigIntAsGmpMode() : bool
{
return self::BIGINT_AS_GMP === $this->bigIntMode;
}
public function isBigIntAsDecMode() : bool
{
return self::BIGINT_AS_DEC === $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_DEC => 'BIGINT_AS_DEC',
];
$validOptions = [];
for ($i = $validBitmask & -$validBitmask; $i <= $validBitmask; $i <<= 1) {
$validOptions[] = __CLASS__.'::'.$map[$i];
}
throw InvalidOptionException::outOfRange($name, $validOptions);
}
}