Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Stripe-style prefix #58

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
$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));
}
}