|
| 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 | +} |
0 commit comments