-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from garak/split-files
Showing
9 changed files
with
96 additions
and
86 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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit colors="true"> | ||
<phpunit colors="true" bootstrap="vendor/autoload.php"> | ||
<testsuites> | ||
<testsuite name="Lewiscowles1986/ulid"> | ||
<directory suffix="Test.php">./tests/</directory> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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,11 @@ | ||
<?php | ||
|
||
namespace lewiscowles\core; | ||
|
||
class LcgRandomGenerator implements RandomFloatInterface | ||
{ | ||
public function generate(): float | ||
{ | ||
return lcg_value(); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace lewiscowles\core; | ||
|
||
class PHPTimeSource implements TimeSourceInterface | ||
{ | ||
public function getTime(): int | ||
{ | ||
return time(); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace lewiscowles\core; | ||
|
||
interface RandomFloatInterface | ||
{ | ||
public function generate(): float; | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace lewiscowles\core; | ||
|
||
interface TimeSourceInterface | ||
{ | ||
public function getTime(): int; | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace lewiscowles\core; | ||
|
||
final class Ulid | ||
{ | ||
const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; | ||
const ENCODING_LENGTH = 32; | ||
|
||
private $time_src; | ||
private $random_float_src; | ||
|
||
public function __construct(TimeSourceInterface $ts, RandomFloatInterface $rf) | ||
{ | ||
$this->time_src = $ts; | ||
$this->random_float_src = $rf; | ||
} | ||
|
||
public function get(): string | ||
{ | ||
return sprintf( | ||
'%s%s', | ||
$this->encodeTime($this->time_src->getTime(), 10), | ||
$this->encodeRandom(16) | ||
); | ||
} | ||
|
||
private function encodeTime(int $time, int $length): string | ||
{ | ||
$out = ''; | ||
while ($length > 0) { | ||
$mod = (int) ($time % self::ENCODING_LENGTH); | ||
$out = self::ENCODING[$mod] . $out; | ||
$time = ($time - $mod) / self::ENCODING_LENGTH; | ||
$length--; | ||
} | ||
|
||
return $out; | ||
} | ||
|
||
private function encodeRandom(int $length): string | ||
{ | ||
$out = ''; | ||
while ($length > 0) { | ||
$rand = (int) floor(self::ENCODING_LENGTH * $this->random_float_src->generate()); | ||
$out = self::ENCODING[$rand] . $out; | ||
$length--; | ||
} | ||
|
||
return $out; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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