|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MysqlUuid; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Variant |
|
9
|
|
|
* |
|
10
|
|
|
* The variant field should be more accurately called the UUID 'type': it |
|
11
|
|
|
* determines the interpretation of all other parts of the UUID. It's the 'outer |
|
12
|
|
|
* value' of the version (i.e. the UUID variant has many versions: versions DO NOT |
|
13
|
|
|
* have variants). |
|
14
|
|
|
*/ |
|
15
|
|
|
class Variant |
|
16
|
|
|
{ |
|
17
|
|
|
const NCS = 0; |
|
18
|
|
|
const RFC4122 = 1; |
|
19
|
|
|
const MICROSOFT = 2; |
|
20
|
|
|
const FUTURE = 3; |
|
21
|
|
|
const OTHER = 4; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Named variants |
|
25
|
|
|
* |
|
26
|
|
|
* 'other' not named, because only used as fallback value |
|
27
|
|
|
* |
|
28
|
|
|
* @var array<int,string> |
|
29
|
|
|
*/ |
|
30
|
|
|
public static $variants = [ |
|
31
|
|
|
self::NCS => 'ncs', |
|
32
|
|
|
self::RFC4122 => 'rfc4122', |
|
33
|
|
|
self::MICROSOFT => 'microsoft', |
|
34
|
|
|
self::FUTURE => 'future' |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The mask for the variant is established as a single byte, and an int |
|
39
|
|
|
* from 1-8 that says how many bits of the byte value are considered |
|
40
|
|
|
* insignificant (the RFC says the multiplexing differs between variants) |
|
41
|
|
|
* |
|
42
|
|
|
* That is, if the variant uses the first 3 bits of the multiplexed value, |
|
43
|
|
|
* the int value will be 8 - 3 = 5. This means shifting the multiplexed value |
|
44
|
|
|
* left by five bits will retain only the variant information. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array<int,array> |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $mask = [ |
|
49
|
|
|
self::NCS => [0x00, 7], |
|
50
|
|
|
self::RFC4122 => [0x80, 6], |
|
51
|
|
|
self::MICROSOFT => [0xC0, 5], |
|
52
|
|
|
self::FUTURE => [0xE0, 5] |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var Uuid |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $uuid; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Uuid $uuid |
|
62
|
|
|
*/ |
|
63
|
4 |
|
public function __construct(Uuid $uuid) |
|
64
|
|
|
{ |
|
65
|
4 |
|
$this->uuid = $uuid; |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return int |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function get() |
|
72
|
|
|
{ |
|
73
|
3 |
|
$value = ord($this->getVariantByte()); |
|
74
|
|
|
|
|
75
|
3 |
|
foreach ($this->mask as $variant => $mask) { |
|
76
|
2 |
|
list($pattern, $bits) = $mask; |
|
77
|
|
|
|
|
78
|
2 |
|
$masked = $value >> $bits << $bits; |
|
79
|
|
|
|
|
80
|
2 |
|
if ($masked === $pattern) { |
|
81
|
2 |
|
return $variant; |
|
82
|
|
|
} |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
return self::OTHER; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param int $variant See self::* formats |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function set($variant) |
|
92
|
|
|
{ |
|
93
|
2 |
|
if (empty($this->mask[$variant])) { |
|
94
|
1 |
|
throw new InvalidArgumentException('Invalid variant scheme; cannot find mask'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
$byte = ord($this->getVariantByte()); |
|
98
|
1 |
|
list($pattern, $bits) = $this->mask[$variant]; |
|
99
|
|
|
|
|
100
|
1 |
|
$mask = 0xFF >> $bits << $bits; |
|
101
|
1 |
|
$value = ($byte & ~$mask) | ($pattern & $mask); |
|
102
|
|
|
|
|
103
|
1 |
|
$this->setVariantByte(chr($value)); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get a single byte from the UUID containing the variant in the high bits |
|
108
|
|
|
* |
|
109
|
|
|
* We're assuming here that the variant will only ever be multiplexed into |
|
110
|
|
|
* a single byte of the clock_seq field. (True of all currently known |
|
111
|
|
|
* variants) |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
3 |
|
protected function getVariantByte() |
|
116
|
|
|
{ |
|
117
|
3 |
|
$bin = hex2bin($this->uuid->getField('clock_seq')); |
|
118
|
3 |
|
return $bin[0]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $byte A single byte |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
1 |
|
protected function setVariantByte($byte) |
|
126
|
|
|
{ |
|
127
|
1 |
|
$bin = hex2bin($this->uuid->getField('clock_seq')); |
|
128
|
1 |
|
$bin[0] = $byte; |
|
129
|
1 |
|
$this->uuid->setField('clock_seq', bin2hex($bin)); |
|
130
|
1 |
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|