Skip to content

Commit

Permalink
Merge pull request #58 from PhpGt/prefix
Browse files Browse the repository at this point in the history
Feature: Stripe-style prefix
  • Loading branch information
g105b authored Sep 13, 2023
2 parents 6a1f9f3 + df13996 commit 7540b26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ This solves the problems exposed with working with auto-incrementing integer pri
<a href="https://github.com/PhpGt/Ulid/actions" target="_blank">
<img src="https://badge.status.php.gt/ulid-build.svg" alt="Build status" />
</a>
<a href="https://scrutinizer-ci.com/g/PhpGt/Ulid" target="_blank">
<a href="https://app.codacy.com/gh/PhpGt/Ulid" target="_blank">
<img src="https://badge.status.php.gt/ulid-quality.svg" alt="Code quality" />
</a>
<a href="https://scrutinizer-ci.com/g/PhpGt/Ulid" target="_blank">
<a href="https://app.codecov.io/gh/PhpGt/Ulid" target="_blank">
<img src="https://badge.status.php.gt/ulid-coverage.svg" alt="Code coverage" />
</a>
<a href="https://packagist.org/packages/PhpGt/Ulid" target="_blank">
Expand Down
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;
}

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 7540b26

Please sign in to comment.