예제 #1
0
 /**
  * Decode a string using Crockford base-32
  *
  * {@see http://www.crockford.com/wrmg/base32.html}
  *
  * @param mixed   $number   The crockford number to convert
  * @param mixed   $base     Desired output base for number
  * @param boolean $checksum Crockford number includes checksum
  *
  * @return string The number decoded to desired base
  */
 public static function decode($number, $base = 10, $checksum = false)
 {
     // Clean and check number
     $crockford = strtoupper(trim(str_replace('-', '', $number)));
     if ($invalid = self::check($crockford)) {
         throw new \RuntimeException(sprintf('Found invalid characters "%s" for crockford number "%s"', implode('', $invalid), $number));
     }
     // Recode symbols (base-32) and convert to base-10
     $base32 = strtr($checksum ? substr($crockford, 0, -1) : $crockford, self::CROCKFORD_SYMBOLS, self::CROCKFORD_FLIPPED);
     $base10 = Base::convert($base32, 32, 10);
     // Verify checksum
     if ($checksum && substr($crockford, -1) != self::checksum($base10)) {
         throw new RuntimeException(sprintf('Invalid crockford checksum for {%s}: found "%s" must be "%s"', $number, substr($crockford, -1), self::checksum($base10)));
     }
     // Convert to desired base
     return Base::convert($base10, 10, $base);
 }
예제 #2
0
 /**
  * Empty numbers must return 0 value
  */
 public function testEmptyNumberMustReturnZero()
 {
     $this->assertEquals('0', Base::convert('', 32, 10), 'Empty string');
     $this->assertEquals('0', Base::convert(null, 32, 16), 'Null value');
 }