From 3e1b7259095b344263fe1f2ce245c6f754e1da5a Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 13 Sep 2023 15:18:09 +0100 Subject: [PATCH 1/2] tweak: fix links --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2f48c02..9317cde 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ This solves the problems exposed with working with auto-incrementing integer pri Build status - + Code quality - + Code coverage From df1399686eacae40b2b4cf23f1f9974062aaa170 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 13 Sep 2023 15:32:49 +0100 Subject: [PATCH 2/2] feature: stripe-style prefixes --- src/Ulid.php | 28 +++++++++++++++++++--------- test/phpunit/UlidTest.php | 14 ++++++++++---- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Ulid.php b/src/Ulid.php index 0ad0dd6..007bc33 100644 --- a/src/Ulid.php +++ b/src/Ulid.php @@ -11,14 +11,12 @@ class Ulid implements Stringable { private string $randomString; public function __construct( - float|int $init = null, + private ?string $prefix = null, + float|int $timestamp = null, private int $length = self::DEFAULT_TOTAL_LENGTH, private int $timestampLength = self::DEFAULT_TIMESTAMP_LENGTH, ) { - if(!is_null($init)) { - $timestamp = $init; - } - else { + if(is_null($timestamp)) { $timestamp = microtime(true); } @@ -27,19 +25,31 @@ public function __construct( $this->randomString = ""; for($i = 0; $i < $this->length - $this->timestampLength; $i++) { $rnd = random_int(0, 31); - $this->randomString .= $this->base32( - $rnd - ); + $this->randomString .= $this->base32($rnd); } } public function __toString():string { $timestampString = $this->getTimestampString(); $randomString = $this->getRandomString(); - return implode("", [ + + $string = implode("", [ $timestampString, $randomString, ]); + + if($this->prefix) { + $string = implode("_", [ + $this->prefix, + $string, + ]); + } + + return $string; + } + + public function getPrefix():?string { + return $this->prefix; } public function getTimestamp():float { diff --git a/test/phpunit/UlidTest.php b/test/phpunit/UlidTest.php index 4833631..074dffc 100644 --- a/test/phpunit/UlidTest.php +++ b/test/phpunit/UlidTest.php @@ -13,7 +13,7 @@ public function testGetTimestamp():void { public function testGetTimestamp_setInConstructor():void { $timestamp = (float)strtotime("5th April 1988"); - $sut = new Ulid($timestamp); + $sut = new Ulid(timestamp: $timestamp); self::assertSame($timestamp, $sut->getTimestamp()); } @@ -22,7 +22,7 @@ public function testGetHexTimestamp_lexSorting():void { for($year = 1970; $year < 2676; $year++) { $timestamp = (float)strtotime("1st January $year"); $timestamp += rand(-1000, 1000) / 1000; - $sut = new Ulid($timestamp); + $sut = new Ulid(timestamp: $timestamp); $hex = $sut->getTimestampString(); if($lastHex) { self::assertGreaterThan($lastHex, $hex, $year); @@ -30,7 +30,7 @@ public function testGetHexTimestamp_lexSorting():void { $lastHex = $hex; } - $sut = new Ulid(strtotime("5th April 1988")); + $sut = new Ulid(timestamp: strtotime("5th April 1988")); self::assertLessThan($lastHex, $sut->getTimestampString()); } @@ -41,7 +41,7 @@ public function testToString_unique():void { public function testToString_length():void { // Testing multiple times in case randomness causes different length strings. for($i = 0; $i < 1_000; $i++) { - $sut = (string)(new Ulid(0)); + $sut = (string)(new Ulid(timestamp: 0)); self::assertSame(Ulid::DEFAULT_TOTAL_LENGTH, strlen($sut)); } } @@ -79,4 +79,10 @@ public function testConstruct_setTimestampLength():void { self::assertSame($tLength, strlen($tString)); } } + + public function testConstruct_prefix():void { + $sut = new Ulid("customer"); + self::assertStringStartsWith("customer_", $sut); + self::assertGreaterThan(strlen("customer_") + 10, strlen($sut)); + } }