Skip to content

Commit

Permalink
feature: stripe-style prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Sep 13, 2023
1 parent 3e1b725 commit df13996
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
28 changes: 19 additions & 9 deletions src/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;

Check warning on line 52 in src/Ulid.php

View check run for this annotation

Codecov / codecov/patch

src/Ulid.php#L51-L52

Added lines #L51 - L52 were not covered by tests
}

public function getTimestamp():float {
Expand Down
14 changes: 10 additions & 4 deletions test/phpunit/UlidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -22,15 +22,15 @@ 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);
}
$lastHex = $hex;
}

$sut = new Ulid(strtotime("5th April 1988"));
$sut = new Ulid(timestamp: strtotime("5th April 1988"));
self::assertLessThan($lastHex, $sut->getTimestampString());
}

Expand All @@ -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));
}
}
Expand Down Expand Up @@ -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));
}
}

0 comments on commit df13996

Please sign in to comment.