Skip to content

Reformat & Refactored #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/ShortCode/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
*/
abstract class Code
{
const FORMAT_NUMBER = '0123456789';
const FORMAT_ALNUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const FORMAT_ALNUM_SMALL = '0123456789abcdefghijklmnopqrstuvwxyz';
const FORMAT_NUMBER = '0123456789';
const FORMAT_ALNUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const FORMAT_ALNUM_SMALL = '0123456789abcdefghijklmnopqrstuvwxyz';
const FORMAT_ALNUM_CAPITAL = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const FORMAT_CHAR_SMALL = 'abcdefghijklmnopqrstwxyz';
const FORMAT_CHAR_CAPITAL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const FORMAT_CHAR_SMALL = 'abcdefghijklmnopqrstwxyz';
const FORMAT_CHAR_CAPITAL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

/**
* @see http://php.net/manual/en/function.base-convert.php#106546
Expand All @@ -37,7 +37,7 @@ abstract class Code
*/
protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput)
{
if ($fromBaseInput == $toBaseInput) {
if($fromBaseInput == $toBaseInput) {
return $numberInput;
}

Expand All @@ -49,22 +49,26 @@ protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput
$numberLen = strlen($numberInput);
$retval = '';

if ($toBaseInput == self::FORMAT_NUMBER) {
if($toBaseInput == self::FORMAT_NUMBER) {
$retval = 0;
for ($i = 1; $i <= $numberLen; $i++) {
for($i = 1; $i <= $numberLen; $i++) {
$retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i)));
}

return $retval;
}
if ($fromBaseInput != self::FORMAT_NUMBER) {

if($fromBaseInput != self::FORMAT_NUMBER) {
$base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER);
} else {
$base10 = $numberInput;
}
if ($base10 < strlen($toBaseInput)) {

if($base10 < strlen($toBaseInput)) {
return $toBase[$base10];
}
while ($base10 != '0') {

while($base10 != '0') {
$retval = $toBase[bcmod($base10, $toLen)] . $retval;
$base10 = bcdiv($base10, $toLen, 0);
}
Expand All @@ -74,7 +78,7 @@ protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput

protected static function getTypeName($value)
{
$class = new \ReflectionClass(__CLASS__);
$class = new \ReflectionClass(__CLASS__);
$constants = array_flip($class->getConstants());

return $constants[$value];
Expand Down
8 changes: 3 additions & 5 deletions src/ShortCode/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
*/
class Random extends Code
{

/**
* Get a random code of fixed length.
*
* @param int $length length of code, default 8
* @param int $length length of code, default 8
* @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM
*
* @return string
Expand All @@ -38,9 +37,8 @@ public static function get($length = 8, $outputFormat = Code::FORMAT_ALNUM)
$output = self::convertBase($number, self::FORMAT_NUMBER, $outputFormat);

if(strlen($output) < $length) {
$output .= substr(str_shuffle($outputFormat.$outputFormat), 0, ($length - strlen($output)));
}
if(strlen($output) > $length) {
$output .= substr(str_shuffle($outputFormat . $outputFormat), 0, ($length - strlen($output)));
} else {
$output = substr($output, 0, $length);
}

Expand Down
15 changes: 7 additions & 8 deletions src/ShortCode/Reversible.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
class Reversible extends Code
{

/**
* Get a code created from a number
*
Expand All @@ -35,7 +34,7 @@ class Reversible extends Code
public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null)
{
if(is_int($minLength)) {
$input += self::getMinForlength($outputFormat, $minLength);
$input += self::getMinForLength($outputFormat, $minLength);
}

static::throwUnlessAcceptable($outputFormat, $input);
Expand All @@ -56,8 +55,8 @@ public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLen
{
$number = self::convertBase($input, $inputFormat, Code::FORMAT_NUMBER);

if (is_int($minLength)) {
$number -= self::getMinForlength($inputFormat, $minLength);
if(is_int($minLength)) {
$number -= self::getMinForLength($inputFormat, $minLength);
}

return $number;
Expand All @@ -80,11 +79,11 @@ private static function throwUnlessAcceptable($type, $input)
*
* @return int|string
*/
private static function getMinForlength($outputFormat, $minLength)
private static function getMinForLength($outputFormat, $minLength)
{
$offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]);
$offsetAsNumber = \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER);
return $offsetAsNumber;
$offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]);

return \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER);
}

}