-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathShortUuid.php
More file actions
178 lines (161 loc) · 4.91 KB
/
Copy pathShortUuid.php
File metadata and controls
178 lines (161 loc) · 4.91 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace PascalDeVink\ShortUuid;
use Brick\Math\BigInteger;
use Brick\Math\RoundingMode;
use Brick\Math\Exception\MathException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* Can encode a given UUID to a shorter string and decode it back to the original UUID.
*/
final class ShortUuid
{
/**
* @var array
*/
private array $alphabet = [
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
];
/**
* @var int
*/
private int $alphabetLength = 57;
/**
* @param array|null $alphabet
*/
public function __construct(array $alphabet = null)
{
if (null !== $alphabet) {
$this->setAlphabet($alphabet);
}
}
/**
* Generate a version 1 UUID from a host ID, sequence number, and the current time and shorten it.
*
* @param int|string $node A 48-bit number representing the hardware address
* This number may be represented as an integer or a hexadecimal string.
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
* could arise when the clock is set backwards in time or if the node ID
* changes.
*
* @return string
*/
public static function uuid1($node = null, ?int $clockSeq = null) : string
{
$uuid = Uuid::uuid1($node, $clockSeq);
$shortUuid = new self();
return $shortUuid->encode($uuid);
}
/**
* Generate a version 4 (random) UUID and shorten it.
*
* @return string
*/
public static function uuid4() : string
{
$uuid = Uuid::uuid4();
$shortUuid = new self();
return $shortUuid->encode($uuid);
}
/**
* Generate a version 5 UUID based on the SHA-1 hash of a namespace
* identifier (which is a UUID) and a name (which is a string) and shorten it.
*
* @param string $ns The UUID namespace in which to create the named UUID
* @param string $name The name to create a UUID for
*
* @return string
*/
public static function uuid5(string $ns, string $name) : string
{
$uuid = Uuid::uuid5($ns, $name);
$shortUuid = new self();
return $shortUuid->encode($uuid);
}
/**
* Encodes the given UUID to a shorter version.
* For example:
* - 4e52c919-513e-4562-9248-7dd612c6c1ca becomes fpfyRTmt6XeE9ehEKZ5LwF
* - 59a3e9ab-6b99-4936-928a-d8b465dd41e0 becomes BnxtX5wGumMUWXmnbey6xH
*
* @param UuidInterface $uuid
*
* @return string
*
* @throws MathException
*/
public function encode(UuidInterface $uuid) : string
{
$uuidInteger = BigInteger::of((string) $uuid->getInteger());
return $this->numToString($uuidInteger);
}
/**
* Decodes the given short UUID to the original version.
* For example:
* - fpfyRTmt6XeE9ehEKZ5LwF becomes 4e52c919-513e-4562-9248-7dd612c6c1ca
* - BnxtX5wGumMUWXmnbey6xH becomes 59a3e9ab-6b99-4936-928a-d8b465dd41e0
*
* @param string $shortUuid
*
* @return UuidInterface
*/
public function decode(string $shortUuid) : UuidInterface
{
return Uuid::fromInteger($this->stringToNum($shortUuid));
}
/**
* Transforms a given (big) number to a string value, based on the set alphabet.
*
* @param BigInteger $number
*
* @return string
*
* @throws MathException
*/
private function numToString(BigInteger $number) : string
{
$output = '';
while ($number->isPositive()) {
$previousNumber = clone $number;
$number = $number->dividedBy($this->alphabetLength, RoundingMode::DOWN);
$digit = $previousNumber->mod($this->alphabetLength);
$output .= $this->alphabet[(int)$digit->toInt()];
}
return $output;
}
/**
* Transforms a given string to a (big) number, based on the set alphabet.
*
* @param string $string
*
* @return BigInteger
*/
private function stringToNum(string $string) : BigInteger
{
$number = BigInteger::of(0);
foreach (str_split(strrev($string)) as $char) {
$number = $number->multipliedBy($this->alphabetLength)->plus(array_search($char, $this->alphabet, false));
}
return $number;
}
/**
* @param array $alphabet
*/
private function setAlphabet(array $alphabet): void
{
$this->alphabet = $alphabet;
$this->alphabetLength = count($alphabet);
}
/**
* Returns the currently used alphabet for encoding and decoding.
*
* @return array
*/
public function getAlphabet() : array
{
return $this->alphabet;
}
}