@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * Provides Base32 conversion |
50 | 50 | * |
51 | 51 | */ |
52 | -class Base32{ |
|
52 | +class Base32 { |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * RFC3548 |
@@ -110,11 +110,11 @@ discard block |
||
110 | 110 | * |
111 | 111 | * @throws \Exception |
112 | 112 | */ |
113 | - public static function setCharset($charset = self::RFC3548){ |
|
114 | - if(strlen($charset) === 32){ |
|
113 | + public static function setCharset($charset = self::RFC3548) { |
|
114 | + if (strlen($charset) === 32) { |
|
115 | 115 | self::$charset = strtoupper($charset); |
116 | 116 | } |
117 | - else{ |
|
117 | + else { |
|
118 | 118 | throw new Exception('Length must be exactly 32'); |
119 | 119 | } |
120 | 120 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @return string String of 0's and 1's |
130 | 130 | */ |
131 | - public static function str2bin($str){ |
|
131 | + public static function str2bin($str) { |
|
132 | 132 | $chrs = unpack('C*', $str); |
133 | 133 | |
134 | 134 | return vsprintf(str_repeat('%08b', count($chrs)), $chrs); |
@@ -144,12 +144,12 @@ discard block |
||
144 | 144 | * @return string The ascii output |
145 | 145 | * @throws \Exception |
146 | 146 | */ |
147 | - public static function bin2str($str){ |
|
148 | - if(strlen($str) % 8 > 0){ |
|
147 | + public static function bin2str($str) { |
|
148 | + if (strlen($str) % 8 > 0) { |
|
149 | 149 | throw new Exception('Length must be divisible by 8'); |
150 | 150 | } |
151 | 151 | |
152 | - if(!preg_match('/^[01]+$/', $str)){ |
|
152 | + if (!preg_match('/^[01]+$/', $str)) { |
|
153 | 153 | throw new Exception('Only 0\'s and 1\'s are permitted'); |
154 | 154 | } |
155 | 155 | |
@@ -172,12 +172,12 @@ discard block |
||
172 | 172 | * @return string String encoded as base32 |
173 | 173 | * @throws exception |
174 | 174 | */ |
175 | - public static function fromBin($str){ |
|
176 | - if(strlen($str) % 8 > 0){ |
|
175 | + public static function fromBin($str) { |
|
176 | + if (strlen($str) % 8 > 0) { |
|
177 | 177 | throw new Exception('Length must be divisible by 8'); |
178 | 178 | } |
179 | 179 | |
180 | - if(!preg_match('/^[01]+$/', $str)){ |
|
180 | + if (!preg_match('/^[01]+$/', $str)) { |
|
181 | 181 | throw new Exception('Only 0\'s and 1\'s are permitted'); |
182 | 182 | } |
183 | 183 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | $length = strlen($str); |
189 | 189 | $rbits = $length&7; |
190 | 190 | |
191 | - if($rbits > 0){ |
|
191 | + if ($rbits > 0) { |
|
192 | 192 | // Excessive bits need to be padded |
193 | 193 | $ebits = substr($str, $length - $rbits); |
194 | 194 | $str = substr($str, 0, $length - $rbits).'000'.$ebits.str_repeat('0', 5 - strlen($ebits)); |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | |
197 | 197 | preg_match_all('/.{8}/', $str, $chrs); |
198 | 198 | |
199 | - $chrs = array_map(function($str){ |
|
199 | + $chrs = array_map(function($str) { |
|
200 | 200 | return self::$charset[bindec($str)]; |
201 | 201 | }, $chrs[0]); |
202 | 202 | |
@@ -213,13 +213,13 @@ discard block |
||
213 | 213 | * @return string Ascii binary string |
214 | 214 | * @throws \Exception |
215 | 215 | */ |
216 | - public static function toBin($str){ |
|
217 | - if(!preg_match('/^['.self::$charset.']+$/', $str)){ |
|
216 | + public static function toBin($str) { |
|
217 | + if (!preg_match('/^['.self::$charset.']+$/', $str)) { |
|
218 | 218 | throw new Exception('Must match character set'); |
219 | 219 | } |
220 | 220 | |
221 | 221 | // Convert the base32 string back to a binary string |
222 | - $str = array_map(function ($chr){ |
|
222 | + $str = array_map(function($chr) { |
|
223 | 223 | return sprintf('%08b', strpos(self::$charset, $chr)); |
224 | 224 | }, str_split($str)); |
225 | 225 | |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | $length = strlen($str); |
231 | 231 | $rbits = $length&7; |
232 | 232 | |
233 | - if($rbits > 0){ |
|
233 | + if ($rbits > 0) { |
|
234 | 234 | $str = substr($str, 0, $length - $rbits); |
235 | 235 | } |
236 | 236 | |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | * |
248 | 248 | * @return string The converted base32 string |
249 | 249 | */ |
250 | - public static function fromString($str){ |
|
250 | + public static function fromString($str) { |
|
251 | 251 | return self::fromBin(self::str2bin($str)); |
252 | 252 | } |
253 | 253 | |
@@ -261,11 +261,11 @@ discard block |
||
261 | 261 | * |
262 | 262 | * @return string The normal string |
263 | 263 | */ |
264 | - public static function toString($str){ |
|
264 | + public static function toString($str) { |
|
265 | 265 | $str = strtoupper($str); |
266 | 266 | |
267 | 267 | // csSave actually has to be able to consider extra characters |
268 | - if(self::$charset === self::csSafe){ |
|
268 | + if (self::$charset === self::csSafe) { |
|
269 | 269 | $str = str_replace('O', '0', $str); |
270 | 270 | $str = str_replace(['I', 'L'], '1', $str); |
271 | 271 | } |
@@ -113,8 +113,7 @@ |
||
113 | 113 | public static function setCharset($charset = self::RFC3548){ |
114 | 114 | if(strlen($charset) === 32){ |
115 | 115 | self::$charset = strtoupper($charset); |
116 | - } |
|
117 | - else{ |
|
116 | + } else{ |
|
118 | 117 | throw new Exception('Length must be exactly 32'); |
119 | 118 | } |
120 | 119 | } |