Skip to content

Commit

Permalink
Merge pull request #13 from garak/split-files
Browse files Browse the repository at this point in the history
Separate classes/interfaces. This should fix #7 @garak is 👍 🥇
Lewiscowles1986 authored Sep 13, 2017
2 parents c508408 + c7076a5 commit e2364fe
Showing 9 changed files with 96 additions and 86 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -7,7 +7,10 @@
},
"require-dev": {
"ext-xdebug": "*",
"phpunit/phpunit": "^5.4"
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": { "lewiscowles\\core\\": "src" }
},
"license": "AGPL-3.0",
"authors": [
4 changes: 2 additions & 2 deletions phpunit.xml.dist
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>
11 changes: 11 additions & 0 deletions src/LcgRandomGenerator.php
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();
}
}
11 changes: 11 additions & 0 deletions src/PHPTimeSource.php
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();
}
}
8 changes: 8 additions & 0 deletions src/RandomFloatInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace lewiscowles\core;

interface RandomFloatInterface
{
public function generate(): float;
}
8 changes: 8 additions & 0 deletions src/TimeSourceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace lewiscowles\core;

interface TimeSourceInterface
{
public function getTime(): int;
}
52 changes: 52 additions & 0 deletions src/Ulid.php
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;
}
}
80 changes: 0 additions & 80 deletions src/ulid.php

This file was deleted.

3 changes: 0 additions & 3 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../src/ulid.php';

use lewiscowles\core\Ulid;
use lewiscowles\core\LcgRandomGenerator;
use lewiscowles\core\PHPTimeSource;

0 comments on commit e2364fe

Please sign in to comment.