From 929a6d8285a699fe4ccb33c9b665ccefdf061418 Mon Sep 17 00:00:00 2001 From: Anis Ahmad Date: Sat, 24 Oct 2015 12:54:03 +0600 Subject: [PATCH] Updated random number generation. Added example usages. --- example.php | 69 ++++++++++++++++++++++++++++++++++++++++ phpunit.xml.dist | 22 +++++++++++++ src/ShortCode/Random.php | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 example.php create mode 100644 phpunit.xml.dist diff --git a/example.php b/example.php new file mode 100644 index 0000000..3cf4117 --- /dev/null +++ b/example.php @@ -0,0 +1,69 @@ +
';
+}
+
+$time_start = microtime(true);
+
+echo "{$br}Random code generation $br";
+echo "======================================================== $br";
+
+echo "Create some random codes $br";
+echo "-------------------------------------------------------- $br";
+for($i = 0; $i < 5; $i++) {
+    echo "Code $i : ". ShortCode\Random::get(). $br;
+}
+
+echo "{$br}Create random codes of 4 characters $br";
+echo "-------------------------------------------------------- $br";
+for($i = 0; $i < 5; $i++) {
+    echo "Code $i : ". ShortCode\Random::get(4). $br;
+}
+
+echo "{$br}Create random codes of 6 characters, with small letter only alpha-numaric $br";
+echo "-------------------------------------------------------- $br";
+for($i = 0; $i < 5; $i++) {
+    echo "Code $i : ". ShortCode\Random::get(4, \ShortCode\Code::FORMAT_ALNUM_SMALL). $br;
+}
+
+echo "{$br}Create random codes of 10 characters, with capital letter only $br";
+echo "-------------------------------------------------------- $br";
+for($i = 0; $i < 5; $i++) {
+    echo "Code $i : ". ShortCode\Random::get(10, \ShortCode\Code::FORMAT_CHAR_CAPITAL). $br;
+}
+
+echo "{$br}Create random codes of 4 characters, numbers only $br";
+echo "-------------------------------------------------------- $br";
+for($i = 0; $i < 5; $i++) {
+    echo "Code $i : ". ShortCode\Random::get(4, \ShortCode\Code::FORMAT_NUMBER). $br;
+}
+
+echo "{$br}Reversible code generation and reverting $br";
+echo "======================================================== $br";
+
+$number = 54382982;
+$alnum = \ShortCode\Reversible::convert($number);
+echo "{$br}Converting $number to alphanumeric code : $alnum {$br}";
+echo "Reverting $alnum back to number : ". \ShortCode\Reversible::revert($alnum);
+
+$number = 54382982;
+$alnumCapital = \ShortCode\Reversible::convert($number, \ShortCode\Code::FORMAT_ALNUM_CAPITAL);
+echo "{$br}{$br}Converting $number to (capital letter only) alphanumeric code : $alnumCapital {$br}";
+echo "Reverting $alnumCapital back to number : ". \ShortCode\Reversible::revert($alnumCapital, \ShortCode\Code::FORMAT_ALNUM_CAPITAL);
+
+$number = 54382982;
+$smallChars = \ShortCode\Reversible::convert($number, \ShortCode\Code::FORMAT_CHAR_SMALL);
+echo "{$br}{$br}Converting $number to small letter only code : $smallChars {$br}";
+echo "Reverting $smallChars back to number : ". \ShortCode\Reversible::revert($smallChars, \ShortCode\Code::FORMAT_CHAR_SMALL);
+
+$time_end = microtime(true);
+$time = $time_end - $time_start;
+
+echo "{$br}{$br}Total time taken : $time seconds. $br";
+
+if(PHP_SAPI != 'cli') {
+    echo '
'; +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..8f7462e --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,22 @@ + + + + + + tests/ShortCode/ + + + + + + vendor/ + + + src/ShortCode/ + + + + + + + \ No newline at end of file diff --git a/src/ShortCode/Random.php b/src/ShortCode/Random.php index b17277f..255432a 100644 --- a/src/ShortCode/Random.php +++ b/src/ShortCode/Random.php @@ -34,7 +34,7 @@ public static function get($length = 8, $outputFormat = Code::FORMAT_ALNUM) { static::throwUnlessAcceptable($outputFormat, $length); - $number = str_replace('.', '', microtime(true)) . rand(100, 900); + $number = rand(100, 900) . str_replace('.', '', microtime(true)); $output = self::convertBase($number, self::FORMAT_NUMBER, $outputFormat); if(strlen($output) < $length) {