diff --git a/src/ShortCode/Code.php b/src/ShortCode/Code.php index d533aff..4a592fd 100644 --- a/src/ShortCode/Code.php +++ b/src/ShortCode/Code.php @@ -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 @@ -37,7 +37,7 @@ abstract class Code */ protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) { - if ($fromBaseInput == $toBaseInput) { + if($fromBaseInput == $toBaseInput) { return $numberInput; } @@ -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); } @@ -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]; diff --git a/src/ShortCode/Random.php b/src/ShortCode/Random.php index 8543f5f..6d62fcb 100644 --- a/src/ShortCode/Random.php +++ b/src/ShortCode/Random.php @@ -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 @@ -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); } diff --git a/src/ShortCode/Reversible.php b/src/ShortCode/Reversible.php index 5afa1ae..f493796 100644 --- a/src/ShortCode/Reversible.php +++ b/src/ShortCode/Reversible.php @@ -22,7 +22,6 @@ */ class Reversible extends Code { - /** * Get a code created from a number * @@ -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); @@ -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; @@ -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); } }