From f2fd282e7a729420226d2df55aa38726a7ce8e12 Mon Sep 17 00:00:00 2001 From: sohag hossain Date: Thu, 28 Oct 2021 07:15:21 +0600 Subject: [PATCH 1/4] Reformat & Refactored --- src/ShortCode/Code.php | 3 ++ src/ShortCode/Random.php | 65 +++++++++++++++++------------------- src/ShortCode/Reversible.php | 12 +++---- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/ShortCode/Code.php b/src/ShortCode/Code.php index d533aff..1c8724d 100644 --- a/src/ShortCode/Code.php +++ b/src/ShortCode/Code.php @@ -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); diff --git a/src/ShortCode/Random.php b/src/ShortCode/Random.php index 8543f5f..c84d1f0 100644 --- a/src/ShortCode/Random.php +++ b/src/ShortCode/Random.php @@ -19,40 +19,35 @@ * * @author Anis Uddin Ahmad */ -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" ); + } + } } diff --git a/src/ShortCode/Reversible.php b/src/ShortCode/Reversible.php index 5afa1ae..b66a3d9 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); @@ -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; @@ -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); } } From 7b362932309c7441b45ddaa9a561cad5db7c51ab Mon Sep 17 00:00:00 2001 From: sohag hossain Date: Sat, 30 Oct 2021 18:53:41 +0600 Subject: [PATCH 2/4] Resolved feedback Fixed applying PSR rules --- src/ShortCode/Code.php | 109 ++++++++++++++++++----------------- src/ShortCode/Random.php | 22 +++---- src/ShortCode/Reversible.php | 108 +++++++++++++++++----------------- 3 files changed, 118 insertions(+), 121 deletions(-) diff --git a/src/ShortCode/Code.php b/src/ShortCode/Code.php index 1c8724d..9210c78 100644 --- a/src/ShortCode/Code.php +++ b/src/ShortCode/Code.php @@ -19,67 +19,68 @@ */ abstract class Code { - 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_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'; - /** - * @see http://php.net/manual/en/function.base-convert.php#106546 - * - * @param $numberInput - * @param $fromBaseInput - * @param $toBaseInput - * - * @return int|string - */ - protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) - { - if ($fromBaseInput == $toBaseInput) { - return $numberInput; - } + /** + * @see http://php.net/manual/en/function.base-convert.php#106546 + * + * @param $numberInput + * @param $fromBaseInput + * @param $toBaseInput + * + * @return int|string + */ + protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) + { + if($fromBaseInput == $toBaseInput) { + return $numberInput; + } - $fromBase = str_split($fromBaseInput, 1); - $toBase = str_split($toBaseInput, 1); - $number = str_split($numberInput, 1); - $fromLen = strlen($fromBaseInput); - $toLen = strlen($toBaseInput); - $numberLen = strlen($numberInput); - $retval = ''; + $fromBase = str_split($fromBaseInput, 1); + $toBase = str_split($toBaseInput, 1); + $number = str_split($numberInput, 1); + $fromLen = strlen($fromBaseInput); + $toLen = strlen($toBaseInput); + $numberLen = strlen($numberInput); + $retval = ''; - if ($toBaseInput == self::FORMAT_NUMBER) { - $retval = 0; - for ($i = 1; $i <= $numberLen; $i++) { - $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); - } - return $retval; - } + if($toBaseInput == self::FORMAT_NUMBER) { + $retval = 0; + for($i = 1; $i <= $numberLen; $i ++) { + $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); + } - if ($fromBaseInput != self::FORMAT_NUMBER) { - $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER); - } else { - $base10 = $numberInput; - } + return $retval; + } - if ($base10 < strlen($toBaseInput)) { - return $toBase[$base10]; - } + if($fromBaseInput != self::FORMAT_NUMBER) { + $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER); + } else { + $base10 = $numberInput; + } - while ($base10 != '0') { - $retval = $toBase[bcmod($base10, $toLen)] . $retval; - $base10 = bcdiv($base10, $toLen, 0); - } + if($base10 < strlen($toBaseInput)) { + return $toBase[$base10]; + } - return $retval; - } + while($base10 != '0') { + $retval = $toBase[bcmod($base10, $toLen)] . $retval; + $base10 = bcdiv($base10, $toLen, 0); + } - protected static function getTypeName($value) - { - $class = new \ReflectionClass(__CLASS__); - $constants = array_flip($class->getConstants()); + return $retval; + } - return $constants[$value]; - } + protected static function getTypeName($value) + { + $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 c84d1f0..0a93f74 100644 --- a/src/ShortCode/Random.php +++ b/src/ShortCode/Random.php @@ -28,25 +28,25 @@ class Random extends Code { * * @return string */ - public static function get( $length = 8, $outputFormat = Code::FORMAT_ALNUM ) { - static::throwUnlessAcceptable( $outputFormat, $length ); + 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 ); + $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(str_shuffle($outputFormat . $outputFormat), 0, ($length - strlen($output))); } else { - $output = substr( $output, 0, $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" ); + private static function throwUnlessAcceptable($type, $length) { + if($length > 20) { + $typeName = self::getTypeName($type); + throw new UnexpectedCodeLength("Code length $length is not acceptable for $typeName"); } } diff --git a/src/ShortCode/Reversible.php b/src/ShortCode/Reversible.php index b66a3d9..2facf77 100644 --- a/src/ShortCode/Reversible.php +++ b/src/ShortCode/Reversible.php @@ -20,69 +20,65 @@ * * @author Anis Uddin Ahmad */ -class Reversible extends Code -{ - /** - * Get a code created from a number - * - * @param $input - * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM - * @param null $minLength - * - * @return string - */ - public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null) - { - if(is_int($minLength)) { - $input += self::getMinForLength($outputFormat, $minLength); - } +class Reversible extends Code { + /** + * Get a code created from a number + * + * @param $input + * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM + * @param null $minLength + * + * @return string + */ + public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null) { + if(is_int($minLength)) { + $input += self::getMinForLength($outputFormat, $minLength); + } - static::throwUnlessAcceptable($outputFormat, $input); + static::throwUnlessAcceptable($outputFormat, $input); - return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat); - } + return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat); + } - /** - * Revert a code to it's original number - * - * @param $input - * @param string $inputFormat - * @param null $minLength - * - * @return int - */ - public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null) - { - $number = self::convertBase($input, $inputFormat, Code::FORMAT_NUMBER); + /** + * Revert a code to it's original number + * + * @param $input + * @param string $inputFormat + * @param null $minLength + * + * @return int + */ + public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null) { + $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; - } + return $number; + } - private static function throwUnlessAcceptable($type, $input) - { - if(false !== strpos("$input", 'E+')) { - throw new InputIsTooLarge("Input is too large to process."); - } + private static function throwUnlessAcceptable($type, $input) { + if(false !== strpos("$input", 'E+')) { + throw new InputIsTooLarge("Input is too large to process."); + } - if($input < 0) { - throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion."); - } - } + if($input < 0) { + throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion."); + } + } - /** - * @param $outputFormat - * @param $minLength - * - * @return int|string - */ - private static function getMinForLength($outputFormat, $minLength) - { - $offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]); - return \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER); - } + /** + * @param $outputFormat + * @param $minLength + * + * @return int|string + */ + private static function getMinForLength($outputFormat, $minLength) { + $offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]); + + return \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER); + } } From 2abb6871fc4c764e6f9054b3a9b37149b435e944 Mon Sep 17 00:00:00 2001 From: sohag Date: Sat, 13 Nov 2021 19:24:11 +0600 Subject: [PATCH 3/4] Code formatted. --- src/ShortCode/Code.php | 120 +++++++++++++++++------------------ src/ShortCode/Random.php | 75 +++++++++++----------- src/ShortCode/Reversible.php | 119 +++++++++++++++++----------------- 3 files changed, 161 insertions(+), 153 deletions(-) diff --git a/src/ShortCode/Code.php b/src/ShortCode/Code.php index 9210c78..12fbc5d 100644 --- a/src/ShortCode/Code.php +++ b/src/ShortCode/Code.php @@ -11,76 +11,76 @@ namespace ShortCode; /** - * Code - * - * Abstract parent for all type of code generation classes. - * - * @author Anis Uddin Ahmad - */ + * Code + * + * Abstract parent for all type of code generation classes. + * + * @author Anis Uddin Ahmad + */ abstract class Code { - 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_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'; - /** - * @see http://php.net/manual/en/function.base-convert.php#106546 - * - * @param $numberInput - * @param $fromBaseInput - * @param $toBaseInput - * - * @return int|string - */ - protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) - { - if($fromBaseInput == $toBaseInput) { - return $numberInput; - } + /** + * @see http://php.net/manual/en/function.base-convert.php#106546 + * + * @param $numberInput + * @param $fromBaseInput + * @param $toBaseInput + * + * @return int|string + */ + protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) + { + if($fromBaseInput == $toBaseInput) { + return $numberInput; + } - $fromBase = str_split($fromBaseInput, 1); - $toBase = str_split($toBaseInput, 1); - $number = str_split($numberInput, 1); - $fromLen = strlen($fromBaseInput); - $toLen = strlen($toBaseInput); - $numberLen = strlen($numberInput); - $retval = ''; + $fromBase = str_split($fromBaseInput, 1); + $toBase = str_split($toBaseInput, 1); + $number = str_split($numberInput, 1); + $fromLen = strlen($fromBaseInput); + $toLen = strlen($toBaseInput); + $numberLen = strlen($numberInput); + $retval = ''; - if($toBaseInput == self::FORMAT_NUMBER) { - $retval = 0; - for($i = 1; $i <= $numberLen; $i ++) { - $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); - } + if($toBaseInput == self::FORMAT_NUMBER) { + $retval = 0; + for($i = 1; $i <= $numberLen; $i++) { + $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); + } - return $retval; - } + return $retval; + } - if($fromBaseInput != self::FORMAT_NUMBER) { - $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER); - } else { - $base10 = $numberInput; - } + if($fromBaseInput != self::FORMAT_NUMBER) { + $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER); + } else { + $base10 = $numberInput; + } - if($base10 < strlen($toBaseInput)) { - return $toBase[$base10]; - } + if($base10 < strlen($toBaseInput)) { + return $toBase[$base10]; + } - while($base10 != '0') { - $retval = $toBase[bcmod($base10, $toLen)] . $retval; - $base10 = bcdiv($base10, $toLen, 0); - } + while($base10 != '0') { + $retval = $toBase[bcmod($base10, $toLen)] . $retval; + $base10 = bcdiv($base10, $toLen, 0); + } - return $retval; - } + return $retval; + } - protected static function getTypeName($value) - { - $class = new \ReflectionClass(__CLASS__); - $constants = array_flip($class->getConstants()); + protected static function getTypeName($value) + { + $class = new \ReflectionClass(__CLASS__); + $constants = array_flip($class->getConstants()); - return $constants[$value]; - } + return $constants[$value]; + } } diff --git a/src/ShortCode/Random.php b/src/ShortCode/Random.php index 0a93f74..c5e4e88 100644 --- a/src/ShortCode/Random.php +++ b/src/ShortCode/Random.php @@ -13,41 +13,44 @@ use ShortCode\Exception\UnexpectedCodeLength; /** - * Random - * - * Generate Random code string of desired length and format - * - * @author Anis Uddin Ahmad - */ -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"); - } - } + * Random + * + * Generate Random code string of desired length and format + * + * @author Anis Uddin Ahmad + */ +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"); + } + } } diff --git a/src/ShortCode/Reversible.php b/src/ShortCode/Reversible.php index 2facf77..55b95bd 100644 --- a/src/ShortCode/Reversible.php +++ b/src/ShortCode/Reversible.php @@ -14,71 +14,76 @@ use ShortCode\Exception\UnexpectedCodeLength; /** - * Reversible - * - * Generate reversible codes from numbers and revert them to original number - * - * @author Anis Uddin Ahmad - */ -class Reversible extends Code { - /** - * Get a code created from a number - * - * @param $input - * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM - * @param null $minLength - * - * @return string - */ - public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null) { - if(is_int($minLength)) { - $input += self::getMinForLength($outputFormat, $minLength); - } + * Reversible + * + * Generate reversible codes from numbers and revert them to original number + * + * @author Anis Uddin Ahmad + */ +class Reversible extends Code +{ + /** + * Get a code created from a number + * + * @param $input + * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM + * @param null $minLength + * + * @return string + */ + public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null) + { + if(is_int($minLength)) { + $input += self::getMinForLength($outputFormat, $minLength); + } - static::throwUnlessAcceptable($outputFormat, $input); + static::throwUnlessAcceptable($outputFormat, $input); - return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat); - } + return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat); + } - /** - * Revert a code to it's original number - * - * @param $input - * @param string $inputFormat - * @param null $minLength - * - * @return int - */ - public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null) { - $number = self::convertBase($input, $inputFormat, Code::FORMAT_NUMBER); + /** + * Revert a code to it's original number + * + * @param $input + * @param string $inputFormat + * @param null $minLength + * + * @return int + */ + public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null) + { + $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; - } + return $number; + } - private static function throwUnlessAcceptable($type, $input) { - if(false !== strpos("$input", 'E+')) { - throw new InputIsTooLarge("Input is too large to process."); - } + private static function throwUnlessAcceptable($type, $input) + { + if(false !== strpos("$input", 'E+')) { + throw new InputIsTooLarge("Input is too large to process."); + } - if($input < 0) { - throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion."); - } - } + if($input < 0) { + throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion."); + } + } - /** - * @param $outputFormat - * @param $minLength - * - * @return int|string - */ - private static function getMinForLength($outputFormat, $minLength) { - $offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]); + /** + * @param $outputFormat + * @param $minLength + * + * @return int|string + */ + private static function getMinForLength($outputFormat, $minLength) + { + $offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]); - return \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER); - } + return \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER); + } } From a26f03f242aeb32d9ca9bb6c3e9bf95357c9c762 Mon Sep 17 00:00:00 2001 From: sohag Date: Sat, 13 Nov 2021 19:27:36 +0600 Subject: [PATCH 4/4] Code formatted. --- src/ShortCode/Code.php | 120 +++++++++++++++++------------------ src/ShortCode/Random.php | 74 ++++++++++----------- src/ShortCode/Reversible.php | 120 +++++++++++++++++------------------ 3 files changed, 157 insertions(+), 157 deletions(-) diff --git a/src/ShortCode/Code.php b/src/ShortCode/Code.php index 12fbc5d..4a592fd 100644 --- a/src/ShortCode/Code.php +++ b/src/ShortCode/Code.php @@ -11,76 +11,76 @@ namespace ShortCode; /** - * Code - * - * Abstract parent for all type of code generation classes. - * - * @author Anis Uddin Ahmad - */ + * Code + * + * Abstract parent for all type of code generation classes. + * + * @author Anis Uddin Ahmad + */ abstract class Code { - 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_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'; - /** - * @see http://php.net/manual/en/function.base-convert.php#106546 - * - * @param $numberInput - * @param $fromBaseInput - * @param $toBaseInput - * - * @return int|string - */ - protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) - { - if($fromBaseInput == $toBaseInput) { - return $numberInput; - } + /** + * @see http://php.net/manual/en/function.base-convert.php#106546 + * + * @param $numberInput + * @param $fromBaseInput + * @param $toBaseInput + * + * @return int|string + */ + protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) + { + if($fromBaseInput == $toBaseInput) { + return $numberInput; + } - $fromBase = str_split($fromBaseInput, 1); - $toBase = str_split($toBaseInput, 1); - $number = str_split($numberInput, 1); - $fromLen = strlen($fromBaseInput); - $toLen = strlen($toBaseInput); - $numberLen = strlen($numberInput); - $retval = ''; + $fromBase = str_split($fromBaseInput, 1); + $toBase = str_split($toBaseInput, 1); + $number = str_split($numberInput, 1); + $fromLen = strlen($fromBaseInput); + $toLen = strlen($toBaseInput); + $numberLen = strlen($numberInput); + $retval = ''; - if($toBaseInput == self::FORMAT_NUMBER) { - $retval = 0; - for($i = 1; $i <= $numberLen; $i++) { - $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); - } + if($toBaseInput == self::FORMAT_NUMBER) { + $retval = 0; + for($i = 1; $i <= $numberLen; $i++) { + $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); + } - return $retval; - } + return $retval; + } - if($fromBaseInput != self::FORMAT_NUMBER) { - $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER); - } else { - $base10 = $numberInput; - } + if($fromBaseInput != self::FORMAT_NUMBER) { + $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER); + } else { + $base10 = $numberInput; + } - if($base10 < strlen($toBaseInput)) { - return $toBase[$base10]; - } + if($base10 < strlen($toBaseInput)) { + return $toBase[$base10]; + } - while($base10 != '0') { - $retval = $toBase[bcmod($base10, $toLen)] . $retval; - $base10 = bcdiv($base10, $toLen, 0); - } + while($base10 != '0') { + $retval = $toBase[bcmod($base10, $toLen)] . $retval; + $base10 = bcdiv($base10, $toLen, 0); + } - return $retval; - } + return $retval; + } - protected static function getTypeName($value) - { - $class = new \ReflectionClass(__CLASS__); - $constants = array_flip($class->getConstants()); + protected static function getTypeName($value) + { + $class = new \ReflectionClass(__CLASS__); + $constants = array_flip($class->getConstants()); - return $constants[$value]; - } + return $constants[$value]; + } } diff --git a/src/ShortCode/Random.php b/src/ShortCode/Random.php index c5e4e88..6d62fcb 100644 --- a/src/ShortCode/Random.php +++ b/src/ShortCode/Random.php @@ -13,44 +13,44 @@ use ShortCode\Exception\UnexpectedCodeLength; /** - * Random - * - * Generate Random code string of desired length and format - * - * @author Anis Uddin Ahmad - */ + * Random + * + * Generate Random code string of desired length and format + * + * @author Anis Uddin Ahmad + */ 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"); - } - } + /** + * 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"); + } + } } diff --git a/src/ShortCode/Reversible.php b/src/ShortCode/Reversible.php index 55b95bd..f493796 100644 --- a/src/ShortCode/Reversible.php +++ b/src/ShortCode/Reversible.php @@ -14,76 +14,76 @@ use ShortCode\Exception\UnexpectedCodeLength; /** - * Reversible - * - * Generate reversible codes from numbers and revert them to original number - * - * @author Anis Uddin Ahmad - */ + * Reversible + * + * Generate reversible codes from numbers and revert them to original number + * + * @author Anis Uddin Ahmad + */ class Reversible extends Code { - /** - * Get a code created from a number - * - * @param $input - * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM - * @param null $minLength - * - * @return string - */ - public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null) - { - if(is_int($minLength)) { - $input += self::getMinForLength($outputFormat, $minLength); - } + /** + * Get a code created from a number + * + * @param $input + * @param string $outputFormat One of Code::FORMAT_* constants. Default Code::FORMAT_ALNUM + * @param null $minLength + * + * @return string + */ + public static function convert($input, $outputFormat = Code::FORMAT_ALNUM, $minLength = null) + { + if(is_int($minLength)) { + $input += self::getMinForLength($outputFormat, $minLength); + } - static::throwUnlessAcceptable($outputFormat, $input); + static::throwUnlessAcceptable($outputFormat, $input); - return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat); - } + return self::convertBase($input, self::FORMAT_NUMBER, $outputFormat); + } - /** - * Revert a code to it's original number - * - * @param $input - * @param string $inputFormat - * @param null $minLength - * - * @return int - */ - public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null) - { - $number = self::convertBase($input, $inputFormat, Code::FORMAT_NUMBER); + /** + * Revert a code to it's original number + * + * @param $input + * @param string $inputFormat + * @param null $minLength + * + * @return int + */ + public static function revert($input, $inputFormat = Code::FORMAT_ALNUM, $minLength = null) + { + $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; - } + return $number; + } - private static function throwUnlessAcceptable($type, $input) - { - if(false !== strpos("$input", 'E+')) { - throw new InputIsTooLarge("Input is too large to process."); - } + private static function throwUnlessAcceptable($type, $input) + { + if(false !== strpos("$input", 'E+')) { + throw new InputIsTooLarge("Input is too large to process."); + } - if($input < 0) { - throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion."); - } - } + if($input < 0) { + throw new UnexpectedCodeLength("Negative numbers are not acceptable for conversion."); + } + } - /** - * @param $outputFormat - * @param $minLength - * - * @return int|string - */ - private static function getMinForLength($outputFormat, $minLength) - { - $offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]); + /** + * @param $outputFormat + * @param $minLength + * + * @return int|string + */ + private static function getMinForLength($outputFormat, $minLength) + { + $offset = str_pad($outputFormat[1], $minLength, $outputFormat[0]); - return \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER); - } + return \ShortCode\Code::convertBase($offset, $outputFormat, \ShortCode\Code::FORMAT_NUMBER); + } }