@@ -15,7 +15,7 @@ |
||
| 15 | 15 | /** |
| 16 | 16 | * |
| 17 | 17 | */ |
| 18 | -class Base32Characters{ |
|
| 18 | +class Base32Characters { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * RFC3548 |
@@ -17,6 +17,6 @@ |
||
| 17 | 17 | /** |
| 18 | 18 | * |
| 19 | 19 | */ |
| 20 | -class Base32Exception extends Exception{ |
|
| 20 | +class Base32Exception extends Exception { |
|
| 21 | 21 | |
| 22 | 22 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * Provides Base32 conversion |
| 51 | 51 | * |
| 52 | 52 | */ |
| 53 | -class Base32{ |
|
| 53 | +class Base32 { |
|
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * charset |
@@ -73,11 +73,11 @@ discard block |
||
| 73 | 73 | * |
| 74 | 74 | * @throws \chillerlan\Base32\Base32Exception |
| 75 | 75 | */ |
| 76 | - public static function setCharset($charset = Base32Characters::RFC3548){ |
|
| 77 | - if(strlen($charset) === 32){ |
|
| 76 | + public static function setCharset($charset = Base32Characters::RFC3548) { |
|
| 77 | + if (strlen($charset) === 32) { |
|
| 78 | 78 | self::$charset = strtoupper($charset); |
| 79 | 79 | } |
| 80 | - else{ |
|
| 80 | + else { |
|
| 81 | 81 | throw new Base32Exception('Length must be exactly 32'); |
| 82 | 82 | } |
| 83 | 83 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @return string String of 0's and 1's |
| 93 | 93 | */ |
| 94 | - public static function str2bin($str){ |
|
| 94 | + public static function str2bin($str) { |
|
| 95 | 95 | $chrs = unpack('C*', $str); |
| 96 | 96 | |
| 97 | 97 | return vsprintf(str_repeat('%08b', count($chrs)), $chrs); |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * @return string The ascii output |
| 108 | 108 | * @throws \chillerlan\Base32\Base32Exception |
| 109 | 109 | */ |
| 110 | - public static function bin2str($str){ |
|
| 110 | + public static function bin2str($str) { |
|
| 111 | 111 | self::checkLength($str); |
| 112 | 112 | self::checkBin($str); |
| 113 | 113 | |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | * @return string String encoded as base32 |
| 131 | 131 | * @throws \chillerlan\Base32\Base32Exception |
| 132 | 132 | */ |
| 133 | - public static function fromBin($str){ |
|
| 133 | + public static function fromBin($str) { |
|
| 134 | 134 | self::checkLength($str); |
| 135 | 135 | self::checkBin($str); |
| 136 | 136 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | $length = strlen($str); |
| 142 | 142 | $rbits = $length&7; |
| 143 | 143 | |
| 144 | - if($rbits > 0){ |
|
| 144 | + if ($rbits > 0) { |
|
| 145 | 145 | // Excessive bits need to be padded |
| 146 | 146 | $ebits = substr($str, $length - $rbits); |
| 147 | 147 | $str = substr($str, 0, $length - $rbits).'000'.$ebits.str_repeat('0', 5 - strlen($ebits)); |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | |
| 150 | 150 | preg_match_all('/.{8}/', $str, $chrs); |
| 151 | 151 | |
| 152 | - $chrs = array_map(function($str){ |
|
| 152 | + $chrs = array_map(function($str) { |
|
| 153 | 153 | return self::$charset[bindec($str)]; |
| 154 | 154 | }, $chrs[0]); |
| 155 | 155 | |
@@ -166,13 +166,13 @@ discard block |
||
| 166 | 166 | * @return string Ascii binary string |
| 167 | 167 | * @throws \chillerlan\Base32\Base32Exception |
| 168 | 168 | */ |
| 169 | - public static function toBin($str){ |
|
| 170 | - if(!preg_match('/^['.self::$charset.']+$/', $str)){ |
|
| 169 | + public static function toBin($str) { |
|
| 170 | + if (!preg_match('/^['.self::$charset.']+$/', $str)) { |
|
| 171 | 171 | throw new Base32Exception('Must match character set'); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | // Convert the base32 string back to a binary string |
| 175 | - $str = array_map(function ($chr){ |
|
| 175 | + $str = array_map(function($chr) { |
|
| 176 | 176 | return sprintf('%08b', strpos(self::$charset, $chr)); |
| 177 | 177 | }, str_split($str)); |
| 178 | 178 | |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $length = strlen($str); |
| 184 | 184 | $rbits = $length&7; |
| 185 | 185 | |
| 186 | - if($rbits > 0){ |
|
| 186 | + if ($rbits > 0) { |
|
| 187 | 187 | $str = substr($str, 0, $length - $rbits); |
| 188 | 188 | } |
| 189 | 189 | |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | * |
| 201 | 201 | * @return string The converted base32 string |
| 202 | 202 | */ |
| 203 | - public static function fromString($str){ |
|
| 203 | + public static function fromString($str) { |
|
| 204 | 204 | return self::fromBin(self::str2bin($str)); |
| 205 | 205 | } |
| 206 | 206 | |
@@ -214,11 +214,11 @@ discard block |
||
| 214 | 214 | * |
| 215 | 215 | * @return string The normal string |
| 216 | 216 | */ |
| 217 | - public static function toString($str){ |
|
| 217 | + public static function toString($str) { |
|
| 218 | 218 | $str = strtoupper($str); |
| 219 | 219 | |
| 220 | 220 | // csSave actually has to be able to consider extra characters |
| 221 | - if(self::$charset === Base32Characters::CROCKFORD){ |
|
| 221 | + if (self::$charset === Base32Characters::CROCKFORD) { |
|
| 222 | 222 | $str = str_replace('O', '0', $str); |
| 223 | 223 | $str = str_replace(['I', 'L'], '1', $str); |
| 224 | 224 | } |
@@ -231,8 +231,8 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | * @throws \chillerlan\Base32\Base32Exception |
| 233 | 233 | */ |
| 234 | - protected static function checkLength($str){ |
|
| 235 | - if(strlen($str) % 8 > 0){ |
|
| 234 | + protected static function checkLength($str) { |
|
| 235 | + if (strlen($str) % 8 > 0) { |
|
| 236 | 236 | throw new Base32Exception('Length must be divisible by 8'); |
| 237 | 237 | } |
| 238 | 238 | } |
@@ -242,8 +242,8 @@ discard block |
||
| 242 | 242 | * |
| 243 | 243 | * @throws \chillerlan\Base32\Base32Exception |
| 244 | 244 | */ |
| 245 | - protected static function checkBin($str){ |
|
| 246 | - if(!preg_match('/^[01]+$/', $str)){ |
|
| 245 | + protected static function checkBin($str) { |
|
| 246 | + if (!preg_match('/^[01]+$/', $str)) { |
|
| 247 | 247 | throw new Base32Exception('Only 0 and 1 are permitted'); |
| 248 | 248 | } |
| 249 | 249 | } |