Skip to content

Commit c7076a5

Browse files
committed
Separate classes/interfaces. This should fix #7
1 parent c508408 commit c7076a5

File tree

9 files changed

+96
-86
lines changed

9 files changed

+96
-86
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
},
88
"require-dev": {
99
"ext-xdebug": "*",
10-
"phpunit/phpunit": "^5.4"
10+
"phpunit/phpunit": "^6.0"
11+
},
12+
"autoload": {
13+
"psr-4": { "lewiscowles\\core\\": "src" }
1114
},
1215
"license": "AGPL-3.0",
1316
"authors": [

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit colors="true">
3+
<phpunit colors="true" bootstrap="vendor/autoload.php">
44
<testsuites>
55
<testsuite name="Lewiscowles1986/ulid">
6-
<directory suffix="Test.php">./tests/</directory>
6+
<directory suffix="Test.php">tests</directory>
77
</testsuite>
88
</testsuites>
99
</phpunit>

src/LcgRandomGenerator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace lewiscowles\core;
4+
5+
class LcgRandomGenerator implements RandomFloatInterface
6+
{
7+
public function generate(): float
8+
{
9+
return lcg_value();
10+
}
11+
}

src/PHPTimeSource.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace lewiscowles\core;
4+
5+
class PHPTimeSource implements TimeSourceInterface
6+
{
7+
public function getTime(): int
8+
{
9+
return time();
10+
}
11+
}

src/RandomFloatInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace lewiscowles\core;
4+
5+
interface RandomFloatInterface
6+
{
7+
public function generate(): float;
8+
}

src/TimeSourceInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace lewiscowles\core;
4+
5+
interface TimeSourceInterface
6+
{
7+
public function getTime(): int;
8+
}

src/Ulid.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace lewiscowles\core;
4+
5+
final class Ulid
6+
{
7+
const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
8+
const ENCODING_LENGTH = 32;
9+
10+
private $time_src;
11+
private $random_float_src;
12+
13+
public function __construct(TimeSourceInterface $ts, RandomFloatInterface $rf)
14+
{
15+
$this->time_src = $ts;
16+
$this->random_float_src = $rf;
17+
}
18+
19+
public function get(): string
20+
{
21+
return sprintf(
22+
'%s%s',
23+
$this->encodeTime($this->time_src->getTime(), 10),
24+
$this->encodeRandom(16)
25+
);
26+
}
27+
28+
private function encodeTime(int $time, int $length): string
29+
{
30+
$out = '';
31+
while ($length > 0) {
32+
$mod = (int) ($time % self::ENCODING_LENGTH);
33+
$out = self::ENCODING[$mod] . $out;
34+
$time = ($time - $mod) / self::ENCODING_LENGTH;
35+
$length--;
36+
}
37+
38+
return $out;
39+
}
40+
41+
private function encodeRandom(int $length): string
42+
{
43+
$out = '';
44+
while ($length > 0) {
45+
$rand = (int) floor(self::ENCODING_LENGTH * $this->random_float_src->generate());
46+
$out = self::ENCODING[$rand] . $out;
47+
$length--;
48+
}
49+
50+
return $out;
51+
}
52+
}

src/ulid.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

tests/BaseTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?php
22

3-
require_once __DIR__.'/../vendor/autoload.php';
4-
require_once __DIR__.'/../src/ulid.php';
5-
63
use lewiscowles\core\Ulid;
74
use lewiscowles\core\LcgRandomGenerator;
85
use lewiscowles\core\PHPTimeSource;

0 commit comments

Comments
 (0)