Skip to content

Commit c1870c1

Browse files
author
Michał Żukowski
committed
Initial implementation
1 parent 0cf4c2c commit c1870c1

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
composer.lock
3+
vendor

README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
# php-uuid-v6
2-
UUID v6 implementation
2+
3+
UUID v6 implementation as proposed at https://bradleypeabody.github.io/uuidv6/
4+
5+
## Example usage
6+
7+
for ($i = 0; $i < 10; $i++) {
8+
echo \mikemix\Uuid::uuid6() , PHP_EOL;
9+
}
10+
11+
> 1e859157-d7dc-6078-b7b8-02421fa799ff
12+
> 1e859157-d7e3-64d6-9819-02421fa799ff
13+
> 1e859157-d7e3-6602-8ecf-02421fa799ff
14+
> 1e859157-d7e3-66fc-9898-02421fa799ff
15+
> 1e859157-d7e3-67ec-ae4c-02421fa799ff
16+
> 1e859157-d7e3-68d2-810a-02421fa799ff
17+
> 1e859157-d7e3-69c2-b7cc-02421fa799ff
18+
> 1e859157-d7e3-6aa8-871b-02421fa799ff
19+
> 1e859157-d7e3-6b98-85c3-02421fa799ff
20+
> 1e859157-d7e3-6c7e-a2c7-02421fa799ff

composer.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "mikemix/php-uuid-v6",
3+
"description": "Version 6 UUID https://bradleypeabody.github.io/uuidv6/",
4+
"type": "library",
5+
"keywords": ["uuid", "identifier", "guid"],
6+
"homepage": "https://github.com/mikemix/php-uuid-v6",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Michal Zukowski",
11+
"homepage": "https://dotcom.software"
12+
}
13+
],
14+
"require": {
15+
"php": "^7.0",
16+
"ramsey/uuid": "^3.7"
17+
},
18+
"autoload": {
19+
"psr-4": {"mikemix\\": "src/"}
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^6.5"
23+
}
24+
}

src/Uuid.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace mikemix;
4+
5+
class Uuid extends \Ramsey\Uuid\Uuid
6+
{
7+
/**
8+
* @param int|string $node A 48-bit number representing the hardware address
9+
* This number may be represented as an integer or a hexadecimal string.
10+
* @param int $clockSeq A 14-bit number used to help avoid duplicates that
11+
* could arise when the clock is set backwards in time or if the node ID
12+
* changes.
13+
* @return string
14+
*/
15+
public static function uuid6($node = null, $clockSeq = null)
16+
{
17+
return self::uuid1ToUuid6(parent::uuid1($node, $clockSeq)->toString());
18+
}
19+
20+
/**
21+
* Convert UUID v1 to UUID v6.
22+
*
23+
* @see https://bradleypeabody.github.io/uuidv6/
24+
*
25+
* @param string $uuidString
26+
*
27+
* @return string
28+
*/
29+
public static function uuid1ToUuid6($uuidString)
30+
{
31+
$uuidString = str_replace('-', '', $uuidString);
32+
$timeLow1 = substr($uuidString, 0, 5);
33+
$timeLow2 = substr($uuidString, 5, 3);
34+
$timeMid = substr($uuidString, 8, 4);
35+
$timeHigh = substr($uuidString, 13, 3);
36+
$rest = substr($uuidString, 16);
37+
38+
return
39+
$timeHigh.$timeMid.$timeLow1[0].'-'.
40+
substr($timeLow1, 1).'-'.
41+
'6'.$timeLow2.'-'.
42+
substr($rest, 0, 4).'-'.
43+
substr($rest, 4);
44+
}
45+
}

tests/run.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
require __DIR__.'/../vendor/autoload.php';
3+
4+
define('UUID_V1', 'ea6974c6-590f-11e8-b0f9-02421fa799ff');
5+
define('UUID_V6', '1e8590fe-a697-64c6-b0f9-02421fa799ff');
6+
7+
if (($uuid6 = \mikemix\Uuid::uuid1ToUuid6(UUID_V1)) !== UUID_V6) {
8+
throw new LogicException(sprintf('Test failed. "%s" given, but "%s" was expected', $uuid6, UUID_V6));
9+
}

0 commit comments

Comments
 (0)