-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #55
- Loading branch information
Showing
6 changed files
with
231 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace Gt\Ulid; | ||
|
||
class Base32 { | ||
public function __construct( | ||
private string $skipCharacters = "ilou" | ||
) {} | ||
|
||
public function fromDec(int $base10):string { | ||
$skipCharacters = str_split($this->skipCharacters); | ||
|
||
$base10 = abs($base10); | ||
$converted = base_convert((string)$base10, 10, 32); | ||
|
||
for($i = 0, $len = strlen($converted); $i < $len; $i++) { | ||
$ord = ord($converted[$i]); | ||
foreach($skipCharacters as $skip) { | ||
$skipOrd = ord($skip); | ||
if($ord >= $skipOrd) { | ||
$ord++; | ||
} | ||
$converted[$i] = chr($ord); | ||
} | ||
} | ||
|
||
return strtoupper($converted); | ||
} | ||
|
||
public function toDec(string $base32): int { | ||
$skipCharacters = str_split($this->skipCharacters); | ||
$base32 = strtoupper($base32); | ||
|
||
for ($i = 0, $len = strlen($base32); $i < $len; $i++) { | ||
$ord = ord($base32[$i]); | ||
$adjustment = 0; | ||
|
||
foreach ($skipCharacters as $skip) { | ||
$skipOrd = ord(strtoupper($skip)); | ||
if ($ord > $skipOrd) { | ||
$adjustment++; | ||
} | ||
} | ||
|
||
$ord -= $adjustment; | ||
$base32[$i] = chr($ord); | ||
} | ||
|
||
return intval(base_convert($base32, 32, 10)); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
namespace Gt\Ulid; | ||
|
||
class Extractor { | ||
private Base32 $base32; | ||
|
||
public function __construct( | ||
private int $length = Ulid::DEFAULT_TOTAL_LENGTH, | ||
private int $timestampLength = Ulid::DEFAULT_TIMESTAMP_LENGTH, | ||
?Base32 $base32 = null, | ||
) { | ||
$this->base32 = $base32 ?? new Base32(); | ||
} | ||
|
||
public function extractPrefix(string $initString):?string { | ||
$underscorePos = strpos($initString, "_"); | ||
if($underscorePos === false) { | ||
return null; | ||
} | ||
|
||
return substr($initString, 0, $underscorePos); | ||
} | ||
|
||
public function extractTimestamp(string $initString):int { | ||
$underscorePos = $this->positionAfterUnderscore($initString); | ||
|
||
$timestampString = substr( | ||
$initString, | ||
$underscorePos, | ||
$this->timestampLength, | ||
); | ||
return $this->base32->toDec($timestampString); | ||
} | ||
|
||
public function extractRandomString(string $initString):string { | ||
$underscorePos = $this->positionAfterUnderscore($initString); | ||
|
||
$initString = substr( | ||
$initString, | ||
$underscorePos, | ||
); | ||
|
||
return substr( | ||
$initString, | ||
$this->timestampLength, | ||
$this->length, | ||
); | ||
} | ||
|
||
protected function positionAfterUnderscore(string $initString):int { | ||
$underscorePos = strpos($initString, "_"); | ||
if($underscorePos) { | ||
$underscorePos += 1; | ||
} | ||
|
||
return $underscorePos ?: 0; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Gt\Ulid\Test; | ||
|
||
use Gt\Ulid\Base32; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class Base32Test extends TestCase { | ||
public function testFromDec():void { | ||
$sut = new Base32(); | ||
$b32 = $sut->fromDec(105); | ||
self::assertSame(105, $sut->toDec($b32)); | ||
} | ||
|
||
public function testFromDec_timestamp():void { | ||
$timestamp = strtotime("5th April 1988 17:24:00"); | ||
$sut = new Base32(); | ||
$b32 = $sut->fromDec($timestamp); | ||
self::assertSame($timestamp, $sut->toDec($b32)); | ||
} | ||
|
||
public function testFromDec_timestampWayInTheFuture():void { | ||
$timestamp = strtotime("31st December 9999 23:59:59"); | ||
$sut = new Base32(); | ||
$b32 = $sut->fromDec($timestamp); | ||
self::assertSame($timestamp, $sut->toDec($b32)); | ||
} | ||
|
||
public function testFromDec_timestampWayInThePast():void { | ||
$timestamp = strtotime("1st January -5000 13:05:00"); | ||
$sut = new Base32(); | ||
$b32 = $sut->fromDec($timestamp); | ||
self::assertSame($timestamp, $sut->toDec($b32)); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
namespace Gt\Ulid\Test; | ||
|
||
use Gt\Ulid\Extractor; | ||
use Gt\Ulid\Ulid; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ExtractorTest extends TestCase { | ||
public function testExtractTimestamp():void { | ||
$currentTimestamp = 1694626973857; | ||
$ulid = "01HA7T72510000000000"; | ||
|
||
$sut = new Extractor(); | ||
$timestamp = $sut->extractTimestamp($ulid); | ||
self::assertSame($currentTimestamp, $timestamp); | ||
} | ||
|
||
public function testExtractTimestamp_specificKnownDate():void { | ||
$knownTimestamp = strtotime("5th April 1988 17:24:00"); | ||
|
||
$ulid = self::createMock(Ulid::class); | ||
$ulid->method("__toString") | ||
->willReturn("0000H5J61G" . "DYSS02F0S3"); | ||
|
||
$sut = new Extractor(); | ||
$timestamp = $sut->extractTimestamp($ulid); | ||
self::assertSame($knownTimestamp, $timestamp); | ||
} | ||
|
||
public function testExtractPrefix():void { | ||
$ulid = "example_1234567890"; | ||
$sut = new Extractor(); | ||
self::assertSame("example", $sut->extractPrefix($ulid)); | ||
} | ||
|
||
public function testExtractPrefix_noPrefix():void { | ||
$ulid = "1234567890"; | ||
$sut = new Extractor(); | ||
self::assertNull($sut->extractPrefix($ulid)); | ||
} | ||
|
||
public function testExtractRandomString():void { | ||
$random = "AABBCCDDEE"; | ||
$timestamp = "0000000000"; | ||
$ulid = "example_" . $timestamp . $random; | ||
$sut = new Extractor(); | ||
self::assertSame($random, $sut->extractRandomString($ulid)); | ||
} | ||
} |
This file contains 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