Created
October 26, 2016 23:37
-
-
Save ramsey/02bb981edbe19c7c96d4ed6722daf805 to your computer and use it in GitHub Desktop.
Creating an ordered time UUID alongside a timestamp-first COMB UUID.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// composer require ramsey/uuid moontoast/math | |
require_once 'vendor/autoload.php'; | |
use Ramsey\Uuid\Codec\OrderedTimeCodec; | |
use Ramsey\Uuid\Codec\TimestampFirstCombCodec; | |
use Ramsey\Uuid\Generator\CombGenerator; | |
use Ramsey\Uuid\UuidFactory; | |
/** | |
* Creating a v1 UUID with ordered time fields | |
*/ | |
$orderedTimeFactory = new UuidFactory(); | |
$orderedTimeFactory->setCodec(new OrderedTimeCodec($orderedTimeFactory->getUuidBuilder())); | |
$orderedTimeUuid = $orderedTimeFactory->uuid1(); | |
echo "v{$orderedTimeUuid->getVersion()}: "; | |
echo $orderedTimeUuid . "\n"; | |
/** | |
* Creating a v4 UUID with timestamp included in first 6 bytes | |
*/ | |
$timestampFirstCombFactory = new UuidFactory(); | |
$combGenerator = new CombGenerator( | |
$timestampFirstCombFactory->getRandomGenerator(), | |
$timestampFirstCombFactory->getNumberConverter() | |
); | |
$timestampFirstCombCodec = new TimestampFirstCombCodec( | |
$timestampFirstCombFactory->getUuidBuilder() | |
); | |
$timestampFirstCombFactory->setRandomGenerator($combGenerator); | |
$timestampFirstCombFactory->setCodec($timestampFirstCombCodec); | |
$timestampFirstCombUuid = $timestampFirstCombFactory->uuid4(); | |
echo "v{$timestampFirstCombUuid->getVersion()}: "; | |
echo $timestampFirstCombUuid . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment