Skip to content

Commit

Permalink
Merge branch 'release/v1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
levidurfee committed Nov 30, 2018
2 parents ad1959f + b3dc52c commit 381b76c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 37 deletions.
11 changes: 11 additions & 0 deletions src/BaseId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Bulldog\id;

class BaseId
{
protected function encode($input)
{
return rtrim(strtr(base64_encode($input), '+/', '-_'), '=');
}
}
35 changes: 35 additions & 0 deletions src/IncrementalId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Bulldog\id;

use Bulldog\id\Contracts\ObjectIdInterface;

class IncrementalId extends BaseId implements ObjectIdInterface
{
protected $prefix;
protected $id;

public function __construct(string $prefix = '')
{
$this->prefix = $prefix;
}

public function create(...$ids)
{
$this->id = '';
foreach($ids as $i) {
$this->id .= $this->encode($i);
}
return $this->id;
}

public function get($length)
{
return $this->prefix . substr($this->id, 0, $length);
}

public function setPrefix(string $prefix)
{
$this->prefix = $prefix;
}
}
70 changes: 33 additions & 37 deletions src/ObjectId.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
<?php

namespace Bulldog\id
{
class ObjectId implements \Bulldog\id\Contracts\ObjectIdInterface
{
protected $prefix;
namespace Bulldog\id;

public function __construct(string $prefix = '')
{
$this->prefix = $prefix;
}
use Bulldog\id\Contracts\ObjectIdInterface;

public function get($length)
{
$parts = [];
class ObjectId extends BaseId implements ObjectIdInterface
{
protected $prefix;

// By using floor and ceil, if we divide an odd number by 2
// we only get whole numbers that add up to the length.
$parts[0] = $this->bucket(floor($length / 2));
$parts[1] = $this->random(ceil($length / 2));
public function __construct(string $prefix = '')
{
$this->prefix = $prefix;
}

$id = $parts[0].$parts[1];
public function get($length)
{
$parts = [];

return $this->prefix . $id;
}
// By using floor and ceil, if we divide an odd number by 2
// we only get whole numbers that add up to the length.
$parts[0] = $this->bucket(floor($length / 2));
$parts[1] = $this->random(ceil($length / 2));

public function setPrefix(string $prefix)
{
$this->prefix = $prefix;
}
$id = $parts[0].$parts[1];

protected function bucket($length)
{
$seconds = time() - mktime(0, 0, 0, 1, 1, date('Y'));
$secondsEncoded = $this->encode($seconds);
return $this->prefix . $id;
}

return substr($secondsEncoded, 0, $length);
}
public function setPrefix(string $prefix)
{
$this->prefix = $prefix;
}

protected function bucket($length)
{
$seconds = time() - mktime(0, 0, 0, 1, 1, date('Y'));
$secondsEncoded = $this->encode($seconds);

protected function random($length)
{
return substr($this->encode(random_bytes($length)), 0, $length);
}
return substr($secondsEncoded, 0, $length);
}

protected function encode($input)
{
return rtrim(strtr(base64_encode($input), '+/', '-_'), '=');
}
protected function random($length)
{
return substr($this->encode(random_bytes($length)), 0, $length);
}
}
9 changes: 9 additions & 0 deletions tests/IdTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public function testUsingSetPrefixMethod()
$this->assertFalse($result);
}
}

public function testIncrementalId()
{
$iId = new \Bulldog\id\IncrementalId;

$id = $iId->create(4, 'dog', 9);
$this->assertEquals('NAZG9nOQ', $id);
$this->assertEquals('NAZG', $iId->get(4));
}
}

0 comments on commit 381b76c

Please sign in to comment.