From 11ffd93b20459f72a7393659980ea81ce2e2dabe Mon Sep 17 00:00:00 2001 From: kylebragger Date: Fri, 2 Oct 2009 16:44:23 -0400 Subject: [PATCH] add Tiny class to PHP version, add tests --- tiny.php | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/tiny.php b/tiny.php index 190507e..2b71a43 100644 --- a/tiny.php +++ b/tiny.php @@ -9,38 +9,43 @@ * Jacob DeHart (original PHP version) and Kyle Bragger (Ruby port) * * Usage: - * $obfuscated_id = tiny(123) + * $obfuscated_id = toTiny(123) * $original_id = reverseTiny($obfuscated_id) * * Configuration: * * You must run Tiny::generate_set() from console (using tiny.rb) to generate your $set * Do *not* change this once you start using Tiny, as you won't be able to reverseTiny() - * any values tiny()'ed with another set. + * any values toTiny()'ed with another set. * */ -public function tiny($id){ - $set = '__use the set you generate with tiny.rb (see tiny.rb for more)__'; +class Tiny { + public static function toTiny($id){ + $set = 'khvrbAfsCXEzIYt30ZdVLBQy5oJeglPHnu9M67qpKxUFj1OS8GawRTcm24DWiN';//'__use the set you generate with tiny.rb (see tiny.rb for more)__'; - $HexN=""; - $id = floor(abs(intval($id))); - $radix = strlen($set); - while (true) { - $R=$id%$radix; - $HexN = $set{$R}.$HexN; - $id=($id-$R)/$radix; - if ($id==0) break; + $HexN=""; + $id = floor(abs(intval($id))); + $radix = strlen($set); + while (true) { + $R=$id%$radix; + $HexN = $set{$R}.$HexN; + $id=($id-$R)/$radix; + if ($id==0) break; + } + return $HexN; } - return $HexN; -} -public function reverseTiny($str){ - $set = '__use the SAME SET as tiny() above__'; - $radix = strlen($set); - $strlen = strlen($str); - $N = 0; - for($i=0;$i<$strlen;$i++){ - $N += strpos($set,$str{$i})*pow($radix,($strlen-$i-1)); + public static function reverseTiny($str){ + $set = 'khvrbAfsCXEzIYt30ZdVLBQy5oJeglPHnu9M67qpKxUFj1OS8GawRTcm24DWiN';//$set = '__use the SAME SET as tiny() above__'; + $radix = strlen($set); + $strlen = strlen($str); + $N = 0; + for($i=0;$i<$strlen;$i++){ + $N += strpos($set,$str{$i})*pow($radix,($strlen-$i-1)); + } + return "{$N}"; } - return "{$N}"; } + +echo Tiny::toTiny(123); +echo Tiny::reverseTiny(Tiny::toTiny(123)); \ No newline at end of file