Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Żukowski committed May 16, 2018
1 parent 0cf4c2c commit c1870c1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
composer.lock
vendor
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# php-uuid-v6
UUID v6 implementation

UUID v6 implementation as proposed at https://bradleypeabody.github.io/uuidv6/

## Example usage

for ($i = 0; $i < 10; $i++) {
echo \mikemix\Uuid::uuid6() , PHP_EOL;
}

> 1e859157-d7dc-6078-b7b8-02421fa799ff
> 1e859157-d7e3-64d6-9819-02421fa799ff
> 1e859157-d7e3-6602-8ecf-02421fa799ff
> 1e859157-d7e3-66fc-9898-02421fa799ff
> 1e859157-d7e3-67ec-ae4c-02421fa799ff
> 1e859157-d7e3-68d2-810a-02421fa799ff
> 1e859157-d7e3-69c2-b7cc-02421fa799ff
> 1e859157-d7e3-6aa8-871b-02421fa799ff
> 1e859157-d7e3-6b98-85c3-02421fa799ff
> 1e859157-d7e3-6c7e-a2c7-02421fa799ff
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "mikemix/php-uuid-v6",
"description": "Version 6 UUID https://bradleypeabody.github.io/uuidv6/",
"type": "library",
"keywords": ["uuid", "identifier", "guid"],
"homepage": "https://github.com/mikemix/php-uuid-v6",
"license": "MIT",
"authors": [
{
"name": "Michal Zukowski",
"homepage": "https://dotcom.software"
}
],
"require": {
"php": "^7.0",
"ramsey/uuid": "^3.7"
},
"autoload": {
"psr-4": {"mikemix\\": "src/"}
},
"require-dev": {
"phpunit/phpunit": "^6.5"
}
}
45 changes: 45 additions & 0 deletions src/Uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace mikemix;

class Uuid extends \Ramsey\Uuid\Uuid
{
/**
* @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 $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 uuid6($node = null, $clockSeq = null)
{
return self::uuid1ToUuid6(parent::uuid1($node, $clockSeq)->toString());
}

/**
* Convert UUID v1 to UUID v6.
*
* @see https://bradleypeabody.github.io/uuidv6/
*
* @param string $uuidString
*
* @return string
*/
public static function uuid1ToUuid6($uuidString)
{
$uuidString = str_replace('-', '', $uuidString);
$timeLow1 = substr($uuidString, 0, 5);
$timeLow2 = substr($uuidString, 5, 3);
$timeMid = substr($uuidString, 8, 4);
$timeHigh = substr($uuidString, 13, 3);
$rest = substr($uuidString, 16);

return
$timeHigh.$timeMid.$timeLow1[0].'-'.
substr($timeLow1, 1).'-'.
'6'.$timeLow2.'-'.
substr($rest, 0, 4).'-'.
substr($rest, 4);
}
}
9 changes: 9 additions & 0 deletions tests/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
require __DIR__.'/../vendor/autoload.php';

define('UUID_V1', 'ea6974c6-590f-11e8-b0f9-02421fa799ff');
define('UUID_V6', '1e8590fe-a697-64c6-b0f9-02421fa799ff');

if (($uuid6 = \mikemix\Uuid::uuid1ToUuid6(UUID_V1)) !== UUID_V6) {
throw new LogicException(sprintf('Test failed. "%s" given, but "%s" was expected', $uuid6, UUID_V6));
}

0 comments on commit c1870c1

Please sign in to comment.