|
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
|
|
|
const BIGINT_AS_STR = 0b001; |
|
19
|
|
|
const BIGINT_AS_GMP = 0b010; |
|
20
|
|
|
const BIGINT_AS_EXCEPTION = 0b100; |
|
21
|
|
|
|
|
22
|
|
|
private $bigIntMode; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @codeCoverageIgnore |
|
26
|
|
|
*/ |
|
27
|
|
|
private function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
131 |
|
public static function fromBitmask($options) |
|
32
|
|
|
{ |
|
33
|
131 |
|
$self = new self(); |
|
34
|
|
|
|
|
35
|
131 |
|
$self->bigIntMode = self::getSingleOption('bigint', $options, |
|
36
|
131 |
|
self::BIGINT_AS_STR | |
|
37
|
131 |
|
self::BIGINT_AS_GMP | |
|
38
|
131 |
|
self::BIGINT_AS_EXCEPTION |
|
39
|
124 |
|
) ?: self::BIGINT_AS_STR; |
|
40
|
|
|
|
|
41
|
131 |
|
return $self; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
127 |
|
public function isBigIntAsStrMode() |
|
45
|
|
|
{ |
|
46
|
127 |
|
return self::BIGINT_AS_STR === $this->bigIntMode; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
127 |
|
public function isBigIntAsGmpMode() |
|
50
|
|
|
{ |
|
51
|
127 |
|
return self::BIGINT_AS_GMP === $this->bigIntMode; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
131 |
|
private static function getSingleOption($name, $options, $mask) |
|
55
|
|
|
{ |
|
56
|
131 |
|
$option = $options & $mask; |
|
57
|
131 |
|
if ($option === ($option & -$option)) { |
|
58
|
131 |
|
return $option; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
static $map = [ |
|
62
|
|
|
self::BIGINT_AS_STR => 'BIGINT_AS_STR', |
|
63
|
|
|
self::BIGINT_AS_GMP => 'BIGINT_AS_GMP', |
|
64
|
|
|
self::BIGINT_AS_EXCEPTION => 'BIGINT_AS_EXCEPTION', |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
4 |
|
$validOptions = []; |
|
68
|
4 |
View Code Duplication |
for ($i = 1; $i <= $mask; $i <<= 1) { |
|
|
|
|
|
|
69
|
4 |
|
$validOptions[] = __CLASS__.'::'.$map[$i]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
throw InvalidOptionException::fromValidOptions($name, $validOptions); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.