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 1 commit
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
3 changes: 3 additions & 0 deletions src/ShortCode/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput
}
return $retval;
}

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

if ($base10 < strlen($toBaseInput)) {
return $toBase[$base10];
}

while ($base10 != '0') {
$retval = $toBase[bcmod($base10, $toLen)] . $retval;
$base10 = bcdiv($base10, $toLen, 0);
Expand Down
65 changes: 30 additions & 35 deletions src/ShortCode/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,35 @@
*
* @author Anis Uddin Ahmad <anis.programmer@gmail.com>
*/
class Random extends Code
{

/**
* Get a random code of fixed length.
*
* @param int $length length of code, default 8
* @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM
*
* @return string
*/
public static function get($length = 8, $outputFormat = Code::FORMAT_ALNUM)
{
static::throwUnlessAcceptable($outputFormat, $length);

$number = rand(100, 900) . str_replace('.', '', microtime(true));
$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($output, 0, $length);
}

return $output;
}

private static function throwUnlessAcceptable($type, $length)
{
if($length > 20) {
$typeName = self::getTypeName($type);
throw new UnexpectedCodeLength("Code length $length is not acceptable for $typeName");
}
}
class Random extends Code {
/**
* Get a random code of fixed length.
*
* @param int $length length of code, default 8
* @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM
*
* @return string
*/
public static function get( $length = 8, $outputFormat = Code::FORMAT_ALNUM ) {
static::throwUnlessAcceptable( $outputFormat, $length );

$number = rand( 100, 900 ) . str_replace( '.', '', microtime( true ) );
$output = self::convertBase( $number, self::FORMAT_NUMBER, $outputFormat );

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

return $output;
}

private static function throwUnlessAcceptable( $type, $length ) {
if ( $length > 20 ) {
$typeName = self::getTypeName( $type );
throw new UnexpectedCodeLength( "Code length $length is not acceptable for $typeName" );
}
}

}
12 changes: 5 additions & 7 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 @@ -57,7 +56,7 @@ 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);
$number -= self::getMinForLength($inputFormat, $minLength);
}

return $number;
Expand All @@ -80,11 +79,10 @@ 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);
}

}