|
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
|
342 |
|
private function __construct($bigIntMode) |
|
29
|
|
|
{ |
|
30
|
342 |
|
$this->bigIntMode = $bigIntMode; |
|
31
|
342 |
|
} |
|
32
|
|
|
|
|
33
|
329 |
|
public static function fromDefaults() : self |
|
34
|
|
|
{ |
|
35
|
329 |
|
return new self(self::BIGINT_AS_STR); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
22 |
|
public static function fromBitmask(int $bitmask) : self |
|
39
|
|
|
{ |
|
40
|
22 |
|
return new self( |
|
41
|
22 |
|
self::getSingleOption('bigint', $bitmask, |
|
42
|
22 |
|
self::BIGINT_AS_STR | self::BIGINT_AS_GMP | self::BIGINT_AS_DEC |
|
43
|
17 |
|
) ?: self::BIGINT_AS_STR |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
public function isBigIntAsStrMode() : bool |
|
48
|
|
|
{ |
|
49
|
4 |
|
return self::BIGINT_AS_STR === $this->bigIntMode; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
334 |
|
public function isBigIntAsGmpMode() : bool |
|
53
|
|
|
{ |
|
54
|
334 |
|
return self::BIGINT_AS_GMP === $this->bigIntMode; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
334 |
|
public function isBigIntAsDecMode() : bool |
|
58
|
|
|
{ |
|
59
|
334 |
|
return self::BIGINT_AS_DEC === $this->bigIntMode; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
22 |
|
private static function getSingleOption(string $name, int $bitmask, int $validBitmask) : int |
|
63
|
|
|
{ |
|
64
|
22 |
|
$option = $bitmask & $validBitmask; |
|
65
|
22 |
|
if ($option === ($option & -$option)) { |
|
66
|
17 |
|
return $option; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
static $map = [ |
|
70
|
|
|
self::BIGINT_AS_STR => 'BIGINT_AS_STR', |
|
71
|
|
|
self::BIGINT_AS_GMP => 'BIGINT_AS_GMP', |
|
72
|
|
|
self::BIGINT_AS_DEC => 'BIGINT_AS_DEC', |
|
73
|
|
|
]; |
|
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
|
|
|
|