-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUUID.php
78 lines (72 loc) · 2.44 KB
/
UUID.php
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
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_UUID
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* A representation of a version 1 UUID, based on code orginally by Wing Lian.
*
* @category Zend
* @package Zend_UUID
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version 0.1
*/
class Zend_UUID
{
protected $_binary;
protected $_hex;
/**
* Represents a UUID based upon a given hexadecimal string, binary data, or generated
* using the current time, node (MAC Address), and clock sequence.
*
* @param string hex
* @param binary binary
* @param binary $node
* @param binary $clockSeq
*/
public function __construct($hex=NULL, $binary=NULL, $node=NULL, $clockSeq=NULL) {
if(!is_null($hex))
{
$this->_hex = $hex;
$this->_binary = pack("H*", $this->_hex);
}
elseif(!is_null($binary))
{
$this->_binary = $binary;
$unpacked = unpack("H*hex", $this->_binary);
$this->_hex = $unpacked['hex'];
}
else
{
// 0x01b21dd213814000 is the number of 100-ns intervals between the
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
$timestamp = microtime(true)*10000000 + 0x01b21dd213814000;
$clockSeq = (is_null($clockSeq)) ? mt_rand(0, 0x3FFF) : ($clockSeq & 0x3FFF);
$this->_binary = pack("NnnnnN", $timestamp & 0xFFFFFFFF, ($timestamp >> 32) & 0xFFFF, (($timestamp >> 48) & 0x0FFF) | 0x1000, ($clockSeq & 0x3FFF) | 0x8000, (is_null($node) ? mt_rand(0, 0xFFFF) : $node >> 32), (is_null($node) ? mt_rand(0, 0xFFFFFFFF) : ($node & 0xFFFFFFFF)) );
$this->_hex = unpack("H*hex", $this->_binary);
}
}
public function getBinary()
{
return $this->_binary;
}
public function getHex()
{
return $this->_hex;
}
}