-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUUID.php
59 lines (48 loc) · 1.21 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
<?php
declare(strict_types=1);
namespace axios\tools;
class UUID
{
private $salt;
public function __construct($salt = '')
{
$this->salt = $salt;
}
public function v0(): string
{
return uniqid((string) (microtime(true)));
}
public function v1(): string
{
return uniqid(md5((string) (microtime(true))));
}
public function v2(): string
{
return md5($this->salt . uniqid(md5((string) (microtime(true))), true));
}
public function v3($cut = 8, $flavour = '-'): string
{
$str = $this->v2();
$length = 32;
$tmp = [];
while ($length > 0) {
$part = substr($str, 32 - $length, $cut);
$tmp[] = $part;
$length -= $cut;
}
return implode($flavour, $tmp);
}
public function v4($cut = [6, 7, 9, 10], $flavour = '-'): string
{
$str = $this->v2();
$length = 32;
$tmp = [];
while ($length > 0) {
$cut_val = array_rand($cut);
$part = substr($str, 32 - $length, $cut_val);
$tmp[] = $part;
$length -= $cut_val;
}
return implode($flavour, $tmp);
}
}