-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/multiple-ids' into develop
- Loading branch information
Showing
4 changed files
with
88 additions
and
37 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,11 @@ | ||
<?php | ||
|
||
namespace Bulldog\id; | ||
|
||
class BaseId | ||
{ | ||
protected function encode($input) | ||
{ | ||
return rtrim(strtr(base64_encode($input), '+/', '-_'), '='); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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