Skip to content

Commit

Permalink
Updated random number generation. Added example usages.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaxray committed Oct 24, 2015
1 parent 228a489 commit 929a6d8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
69 changes: 69 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
include 'vendor/autoload.php';

$br = PHP_EOL;
if(PHP_SAPI != 'cli') {
echo '<html><body><pre>';
}

$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 '</pre></body></html>';
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="ShortCode Test Suite">
<directory>tests/ShortCode/</directory>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory suffix=".php">vendor/</directory>
</blacklist>
<whitelist>
<directory suffix=".php">src/ShortCode/</directory>
</whitelist>
</filter>

<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/ShortCode/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 929a6d8

Please sign in to comment.