1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 2/14/15 |
5
|
|
|
* Time: 11:09 AM |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Uuid\Versions; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractUuid |
15
|
|
|
* @package NilPortugues\Uuid\Versions |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractUuid |
18
|
|
|
{ |
19
|
|
|
const TIME_LOW = 'time_low'; |
20
|
|
|
const TIME_MID = 'time_mid'; |
21
|
|
|
const TIME_HI_AND_VERSION = 'time_hi_and_version'; |
22
|
|
|
const CLOCK_SEQ_HI_AND_RESERVED = 'clock_seq_hi_and_reserved'; |
23
|
|
|
const CLOCK_SEQ_LOW = 'clock_seq_low'; |
24
|
|
|
const NODE = 'node'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a version 3 or 5 UUID based on the hash (md5 or sha1) of a |
28
|
|
|
* namespace identifier (which is a UUID) and a name (which is a string) |
29
|
|
|
* |
30
|
|
|
* @param string $hash The hash to use when creating the UUID |
31
|
|
|
* @param int $version The UUID version to be generated |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
protected static function uuidFromHashedName($hash, $version) |
36
|
|
|
{ |
37
|
|
|
// Set the version number |
38
|
|
|
$timeHi = \hexdec(\substr($hash, 12, 4)) & 0x0fff; |
39
|
|
|
$timeHi &= ~(0xf000); |
40
|
|
|
$timeHi |= $version << 12; |
41
|
|
|
|
42
|
|
|
// Set the variant to RFC 4122 |
43
|
|
|
$clockSeqHi = \hexdec(\substr($hash, 16, 2)) & 0x3f; |
44
|
|
|
$clockSeqHi &= ~(0xc0); |
45
|
|
|
$clockSeqHi |= 0x80; |
46
|
|
|
|
47
|
|
|
return [ |
48
|
|
|
self::TIME_LOW => \substr($hash, 0, 8), |
49
|
|
|
self::TIME_MID => \substr($hash, 8, 4), |
50
|
|
|
self::TIME_HI_AND_VERSION => \sprintf('%04x', $timeHi), |
51
|
|
|
self::CLOCK_SEQ_HI_AND_RESERVED => \sprintf('%02x', $clockSeqHi), |
52
|
|
|
self::CLOCK_SEQ_LOW => \substr($hash, 18, 2), |
53
|
|
|
self::NODE => \substr($hash, 20, 12), |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Converts this UUID into a string representation |
59
|
|
|
* |
60
|
|
|
* @param array $fields |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
protected static function toString(array $fields) |
65
|
|
|
{ |
66
|
|
|
return \vsprintf( |
67
|
|
|
'%08s-%04s-%04s-%02s%02s-%012s', |
68
|
|
|
$fields |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|